ulrail.gif

Lab Quiz 3

  ur-2c.gif urrail.gif
blrail-nonavbar.gif

Home | Resources | Homeworks | Exams
Lectures | Labs | Contacts | Submit | TAs

br-2a.gif   br-2c.gif brrail.gif
spacer.gif spacer.gif

Pledged Lab Quiz

This lab quiz is pledged.  You may use the course textbook, the Java documentation for String and Point, and NOTHING ELSE.  This includes any files in your home directory, previous assignments, slides, etc.  However, you should still save your file into your home directory.

You will need to submit a single file called World.java.  You may download the skeleton code here: LabQuiz3.java and World.java (right click on the link and select "save as" (or "save target as")).

The TAs cannot help you with Java-related issues on this quiz.  If you are having problems getting JCreator to work, can't log in to the machines, etc., then you are welcome to ask the TAs for assistance.  If you do not understand what the lab is asking, the TAs may be able to help you (meaning if it's a problem with our explanation, then they can help you -- if you've been skipping the lectures so far and have no idea what any of this means, you are on your own).

Advice

  • To maximize your points, make sure your program compiles successfully.
     
  • If towards the end things are not going as well as you would want (i.e., the program does not compile), it might be best to try commenting out errant statements (i.e., put a // in front of them)
     
  • If you run out of time, submit what you have -- there will be partial credit for this quiz.  Also remember that you can submit a file as many times as you want -- only the most recent submission is graded.  So once you have it working, submit what you have.

Where in the World is Carmen Sandiego?

This lab quiz is a heavily modified form of the popular game Where in the World is Carmen Sandiego, from many years ago.  The description of the game (from Wikipedia):

The goal of this game is track Carmen's villains around the world and arrest them and ultimately arrest Carmen herself. In order to make an arrest the user must have a warrant and, obviously, it has to be the correct warrant. The player begins the game by first going to the city where the crime took place, and then obtains hints from the bystanders on where the thief went next, leading them on a chase around the world to find the thief before they "get away." In the process, they must also collect clues on their identity in order to determine who the culprit is. When they reach the place where the villain is going to pass off the loot to Carmen they have to arrest the villain.

Our "world" will be a 2-D array of Strings.  Each spot will either be null or contain the name of a city.  For example, our world may look like this:

  New York     Florence     Colombo
               
    Charlottesville          
          London    
Shanghai     Kuala Lumpur        
            Timbuktu  
        Tokyo      
  Hong Kong           Nairobi

Any blank spot in the table above would be null in the array.  In order to find a city in the world, we would have to search through all the spots, while doing String comparisons.

Once in the city (meaning we've found the (x,y) coordinates of the city in the 2-D array), we will also need to determine the next city to travel to.  In the actual game, this is done by looking up clues, which we are ignoring for this lab quiz.  Instead, we will have a second 2-D array that lists destinations.  For example, it could look like this:

  Charlottesville     London     Carmen!
               
    Nairobi          
          Kuala Lumpur    
Timbuktu     Hong Kong        
            Shanghai  
        Florence      
  New York           Colombo

So if we wanted to find New York in the world, we would search the first 2-D array for New York, and it would be found at (1,0).  Note that (0,0) is the upper left, and we are listing the column number first.  To find out where to travel next, we look in the corresponding place in the second array, which tells us Charlottesville.  So we "travel" from New York to Charlottesville.  From Charlottesville, we would next go to Nairobi, Kenya.  Once you eventually get to Colombo, Sri Lanka, you have found Carmen -- you can tell this because the destination is "Carmen!", not another city.  Note that you can't get everywhere from a given city -- for example, Timbuktu (in Mali, Africa) goes to Shanghai, China; and Shanghai goes right back to Timbuktu.  However, you can assume that from the city you start in, you will (eventually) find the destination you are looking for (i.e. "Carmen!").

You can assume that both the arrays are not ragged (i.e. each row has the same number of columns, and each column has the same number of rows).  While this array is 8x8, you should not assume that all arrays will be 8x8.

Your task to complete World.java, which is a class that will search the "world" for Carmen Sandiego.  We provide a break down of the methods that you need to implement.  We also provide a LabQuiz3.java file, which will run the actual game.

Note: If you are confused at this point, you may ask a TA to explain it, as we want you to understand what is going on.

Class LabQuiz3

This class is very simple -- it creates a default world, and searches for Carmen.

public class LabQuiz3 {
    public static void main (String args[]) {
	World theWorld = new World();
	theWorld.find ("London", "Carmen!");
    }
}

A few important things to note:

  • The world created by the default constructor is the world shown above (this was done to help you get your code working)
  • The first parameter passed to find() method is the city to start at (London, in this case)
  • The second parameter is the destination that is being searched for -- in this case, the program will keep traveling throughout the cities until it finds "Carmen!" as the destination.
  • This means that this particular execution will go through the following cities: London -> Kuala Lumpur -> Hong Kong -> New York -> Charlottesville -> Nairobi -> Columbo -> Carmen!
  • You don't have to submit the LabQuiz3.java file, so you can modify it any way you choose.  In particular, you will probably want to add testing code to make sure the methods required below work properly.

Class World

The World class provides two constructors.  The default constructor creates the world shown in the tables above -- this should help with the debugging.  The specific constructor uses the 2-D arrays passed as parameters for the world, and will be used for testing purposes.  Don't modify the constructors!

The methods you need to provide are:

  • public Point findCity (String s): This method will search the 2-D array for the passed city name, and return a Point object with the (x,y) location of the city.  For example, theWorld.findCity("New York") will return a new Point object with x = 1 and y = 0.  If no such city is found, null should be returned.  The Point that this method returns will have the x value be the column, and the y value be the row.
     
  • public String getDestination (Point p): This method will take in a Point object, and return the appropriate destination.  For example, if the parameter is a Point object with x = 1 and y = 0, then this method will return the string "Charlottesville".  If there is no destination at that location, then null should be returned.  And if the coordinates of this Point are outside the bounds of the array, null should be returned.
     
  • public void find (String start, String what): This method will start at the start city, and find the corresponding destination (using the previous two methods).  Then starting at that city, find the next destination.  This will repeat until the destination what is reached.  Note that the method does not know how many "hops" are required (the example we give you has half a dozen hops, but the method can't rely on that).  You can assume that there is a path between the start city and the destination what.  It should print out the steps as it goes as well.

A few important notes and hints:

  • The Point class is in the java.awt library, so include your import line appropriately.
  • For String comparison, remember that using == will not work -- you have to use the .equals() method.
  • If you run into a null pointer exception, it could be because of your .equals() methods on the strings.  Note that many of the cells in the 2-D arrays are null -- so you can't call .equals() on them (you can, however, call .equals() on a valid object and have null be the parameter).
  • While our world is 8x8, don't assume that the world will always be 8x8!  We will probably test with with a different size world.
  • All your methods must be public!  This is needed for testing.  And don't modify the specific constructor!
  • If you have trouble getting the find() method working right, try for partial credit -- a method that does only two hops (rather than doing however many are needed) will get more credit than a method that does nothing.
  • Don't worry about case-insensitive searching.  Meaning if the city is "New York", your method will not ever be given the parameter "new york" (notice the capitalization is different).

Sample Output

The find() method will need to print out the list of cities traveled to.  Your output doesn't have to look exactly like this (i.e. you can format it differently), but it should print the same cities.  The output from LabQuiz3 is:

London -> Kuala Lumpur -> Hong Kong -> New York -> Charlottesville -> Nairobi -> Colombo -> Carmen!
 

How to Proceed

Get the steps working in order!  Although we give you the LabQuiz3 class, you can modify it to test things.  Get the findCity() method working first, and test it with a few cities.  Then code up getDestination(), and test it with a few Points to make sure the correct destinations are being returned.  Then get find() working.  You don't have to submit LabQuiz3.java, so modify away!

Other Requirements

You are only required to follow a few of the good programming practices discussed in HW J1 (yes, you may follow that link during the lab quiz).  If you have time, feel free to do the others -- but due to the time constraints of the quiz, you don't have to do them all.  The ones you must do are listed below.

  • Whitespace: A line or two between separate elements of the code (methods, code segments, etc.) and good indentation.
     
  • Proper variable names, if you use them in your methods.
     
  • Line length: The lines should not go over about 72 characters. Essentially, when displayed on an 80-character wide screen (what we are using), the line should not go past the end of the screen. You can have a println() statement, for example, use multiple lines (as long as each string starts and ends on the same line). JCreator tells you the column that the cursor is currently in (at the bottom on the status bar), so you can use that to gauge how long your lines are.

Submission

When you are finished, submit your World.java file.  You will have to "sign" a pledge on the submission.

 

spacer.gif
spacer.gif footer-middle.gif spacer.gif