ulrail.gif

Lab 11: 2-D Arrays

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

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

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

Objective

In this lab, you will gain experience programming using two-dimensional arrays.  In particular, you will be developing a Map class, which will print out a map of the universe in our game.  If you need a refresher on 2-D arrays, you may want to look over the lecture notes on arrays, starting at slide 67.

Files

There are a number files that you need to download for this lab.

  • .class files: Location, Market, Vehicle, Inventory, Cargo
  • .java files: Game, Universe
  • You should use your own Descriptions.java file, as that will allow your Universe to have the names you came up with in the last lab.  If you do not have it (or it doesn't work), you can download ours here. Remember, if you use your Descriptions.java file, then the CORRECT implementation of 'getCargoCostFactor' must use '.equals' instead of '==' for the string comparison.
  • Map.java, which is the skeleton code for what you will be developing in this lab.

To run this lab, execute the Game.java file.  This file creates a default universe, and prints it out -- it is not an interactive game for this lab.  As always, the documentation for the various parts of this project, including the Map class, can be found here.

Map class overview

In this lab, you will be developing a Map class, which will print out a map of the universe.  Note that the 'universe' is the world for your game -- whether it be UVa grounds, the US, the world, or a solar system.

A sample output of the functionality of the Map class is shown below.  The Game.java file that is provided will produce slightly different input , which is shown below.


Printing Universe
-       -       -       -       -       -       -       -
-       1       -       -       -       -       -       -
-       -       -       -       -       -       -       -
-       -       -       2       -       -       -       3
-       -       -       -       -       -       -       -
-       -       -       -       -       -       -       -
-       -       4       -       5       -       -       -
-       -       -       -       -       -       -       -
-       -       -       -       -       -       -       -
-       -       -       -       -       -       -       -

The valid destinations are:
1: Geminorum (your location)
2: Ursae Majoris
3: Andromedae
4: Orionis
5: Cygni

Note that the Unvierse has a height of 10 and a width of 8.  If yours is reversed (meaning yours has a height of 8 and a width of 10), then something is wrong.

There are three methods that you will need to develop for this lab, which are described in more detail below.  One prints the grid (the top half of the output shown above), the second prints the list (the bottom half of the output shown above), and the third ties the two together. 

You will notice that the locations have a very specific numbering.  The numbers start at 1, and increase left-to-right, then top-to-bottom.  In other words, the numbering goes through each row in order, giving increasing values to the locations.  This ordering is used in both methods, and it is how the methods know which location has what number.

Universe class

The Universe class will be developed in HW J8.  However, we have provided you with a Universe.java class so that you can complete this lab.  Note that the code that we provide is NOT the code you need to submit for HW J8 -- it was designed to work for this lab, but works in a very different manner than is required for HW J8 (notice that there are no 2-D arrays in the Universe.java for this lab).

Whenever we specify the size of our universe, we are going to specify the row first, followed by the column.  Thus, in the universe above, the coordinates of Orionis (location number 4) are (6,2), not (2,6).  Also note that the top left-most coordinate is (0,0).  So Geminorum (location 1) is at coordinates (1,1).

The Universe class will provide a number of methods that you can use -- the full list can be found here.  The Universe class that we provide with this lab only has four of those methods implemented, as this lab uses only those methods:

  • Universe(): the default constructor, it creates the Universe shown above.
     

  • int getNumRows(): this method returns the height (number of rows) of the Universe -- 10, in this case.
     

  • int getNumCols(): this method returns the width (number of columns) of the Universe -- 8, in this case.
     

  • Location getLocationAt(int row, int col): this method returns the location at (row, col) in the Universe (or null if there is no Location at that position).

Map class

There are three methods that you need to develop for this lab.  All methods are to be public and static.  It is recommended that you develop them in the order specified.  The Game.java file is set up to allow you to test each of the individual methods as you develop them.  Note that there are no fields in this class.

  • public static void printLocations (Universe g, Vehicle user): This method will first print out a line stating something along the lines of, "The valid destinations are:"  Then it will print out the list of the Locations in the order specified above.  This method prints out the last 6 lines of the output shown above.  This will most likely require nested for loops.  Note that the size of the Universe will not be known ahead of time, and can be obtained via the getNumRows() and getNumCols() methods in the Universe object that is passed in to this method.  The order and numbering for the locations is very specific, and is described above.  When the location of the player's Vehicle is printed, the method should print out "(your location)" next to that location name.  Note that this method does NOT print the actual grid -- that's done by the printGrid() method, below.
     

  • public static void printGrid (Universe g): This method will first print out a line stating something along the lines of, "Printing Universe."  Then prints out the grid map of the Universe.  Thus, it needs to go through each row, and print out a character for each column in a given row.  As was the case for printLocations(), above, the size of the Universe will not be known ahead of time, and can be obtained via the getNumRows() and getNumCols() methods in the Universe object that is passed in to this method.  For each spot in the 2-D array grid, either a dash ('-') is printed if there is no Location object there (i.e. getLocation() in the Universe class returns null), or a number is printed if there is a Location object there.  As with printLocations(), the numbering must start at 1, and follow the numbering scheme described above.  It is easiest to use tabs ("\t") to line up the numbers and dashes.
     

  • public static void printMap (Vehicle user): This method obtains the Universe object (via Game.getUniverse()), and then calls printGrid() and printLocations() methods.

Testing

To run this lab, execute the Game.java file.  This file creates a default universe, and prints it out -- it is not an interactive game for this lab.  As always, the documentation for the various parts of this project, including the Map class, can be found here.  The output from an execution run is shown below.  Note that it tests all three of the methods.  Your location names will probably differ.


Testing of the Map class

----------------------------------------
Test of printGrid():
-       -       -       -       -       -       -       -
-       1       -       -       -       -       -       -
-       -       -       -       -       -       -       -
-       -       -       2       -       -       -       3
-       -       -       -       -       -       -       -
-       -       -       -       -       -       -       -
-       -       4       -       5       -       -       -
-       -       -       -       -       -       -       -
-       -       -       -       -       -       -       -
-       -       -       -       -       -       -       -

----------------------------------------
Test of printLocations():

The valid destinations are:
1: Geminorum (your location)
2: Ursae Majoris
3: Andromedae
4: Orionis
5: Cygni

----------------------------------------
Test of printMap():

Printing Universe
-       -       -       -       -       -       -       -
-       1       -       -       -       -       -       -
-       -       -       -       -       -       -       -
-       -       -       2       -       -       -       3
-       -       -       -       -       -       -       -
-       -       -       -       -       -       -       -
-       -       4       -       5       -       -       -
-       -       -       -       -       -       -       -
-       -       -       -       -       -       -       -
-       -       -       -       -       -       -       -

The valid destinations are:
1: Geminorum (your location)
2: Ursae Majoris
3: Andromedae
4: Orionis
5: Cygni

Submission

When you are finished, you just need to submit the Map.java file.  Note that there are a few more questions that are asked for this lab submission.  As the the other java files were not modified, they do not need to be submitted.

If you finish early

If you finish early, you can work on HW J8.

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