ulrail.gif

HW J6: More on Classes

  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

Purpose

In this assignment, we will gain a little more experience writing classes and using objects. Specifically, you will now design the Vehicle class for the trading game.  The Vehicle class represents the player's vehicle as s/he moves from location to location.  This is similar to the design and implementation of the Location class in HW J5, but it's a little more complicated. Plus, the game itself is a bit more interesting, as the game now has "turns", where there are various options that the user can select in each turn.

To get started, create a new directory called HWJ6, and download the following files.  Right click on each link, select "save as", and save them all to the same directory.  A number of the files are "placeholder" classes -- meaning they are mostly not yet implemented, but are included so this homework will compile.  And some of them are .class files -- these are files that you will be developing later.

  • Map.java: This class has a single method, printMap().  You will notice that, as of now, it prints out a fixed universe -- meaning that every time you run the program, the universe will be the exact same.  This will be updated in HW J8 and lab 12.  This method takes in a Vehicle object as the parameter.
  • Market.java: This is a "placeholder" class, and you will be writing this class in HW J7.  It represents a market in a location -- where you buy and sell goods.
  • Parser.class: This class is used to obtain input from the user.  It is a modified form of what will be developed in lab 9.
  • Location.class: This is a compiled version of your Location class from HW J5.  You are free (and even encouraged) to use the Location.java that you developed in HW J5.  To do this, delete Location.class from your HW J6 directory and copy your Location.java from your HW J5 directory to your HW J6 directory.
  • Inventory.java: This is a "placeholder" class, and you will be writing this class in HW J7.  It represents an inventory of a vehicle or a market.
  • Vehicle.java: This is the skeleton code for this homework -- you will be completing (and submitting) this file.
  • Game.java: This class runs the game.  It prints out some introductory information, and starts the game itself.  Note that although we provide the Game.java code, it is important that you understand how it works!

The only file you should modify and submit is Vehicle.java.

Java documentation for all the classes and methods can be found here.  If you forget a method name, or how a method works, you can refer to this documentation.

The Parser Class

The Parser.class file is provided to you so that you can obtain user input.  This class will be developed in lab 9, but until then, you can use the modified version we provide. 

It provides two methods that we will use in this assignment, Parser.parse(). and Parser.getDestination().  Note that they are both static methods, so you should not create a Parser object -- you can just call the method directly. 

Parser.parse()

This method will ask the user to enter one of the commands for the game, and return an int value based on what the user enters.  It takes no parameters.  The method only looks at the first character (ignoring case) of the input, so 't' is the same as 'travel', 'Travel', and 'tortellini'. 

The method will return an int value.  The Parser class has a number of static int fields that it returns, based on the user's input.  For example, if the user enters 't', the method will return Parser.TRAVEL.

Consider the following code:

int input = Parser.parse();
if ( input == Parser.WORLDMAP )
    System.out.println ("You want to see the map");
else if ( input == Parser.TRAVEL )
    System.out.println ("You want to travel somewhere");

This code will read in a value from the user, and print out a different line depending on what the user entered.  There are a number of fields in the Parser class that the method can return, but only five of these are relevant to this homework, and are described below (they are Parser.TRAVEL, Parser.MARKET, Parser.INVENTORY, Parser.WORLDMAP, and Parser.QUIT).  The other fields will be used in HW J7.

Parser.getDestination()

This method will read in, from the keyboard, the user's intended destination.  It takes in no parameters.  To enter a destination, the user must enter the entire name of the destination.  The method will then return the appropriate Location object for the destination that the user entered.  If the user enters an invalid name, the method will return null.

The Vehicle Class

Note that you MUST name your fields and methods exactly as we have specified below (including the proper capitalization) -- otherwise, our test programs will not realize you have the methods written, and you will get zero credit for them.  Same goes for the parameter types (you can change the parameter names, if you want).

Attributes

Having developed the Location class in HW J5, we now turn our attention to the Vehicle class. The Vehicle class should have the following attributes, all of which should be private

  • vehicleType (String): the type of vehicle (e.g., "InterGallactic Thunderbird", "Golfcart", etc.)
  • fuel (double): the amount of fuel available for the vehicle.  Vehicles could start with 500.0 fuel units.
  • fuelUnits (String): the type of fuel it is (e.g., "Gazorkas")
  • fuelBurnRate (double): how quickly (or slowly) the vehicle burns fuel per unit time.  A burn rate of 10.0 is a sample value.
  • money (double): the amount of money you have available to buy new goods. For simplicity, we assume US dollars.
  • inventory (Inventory): the collection of good that you are carrying on the vehicle.  The cost factor for the Vehicle's inventory should be 1.0.
  • location (Location): the current Location where the vehicle is parked.
  • totalDistanceTraveled (double): the total distance that the vehicle has traveled

Constructors

The Vehicle class should also support the following two constructors.  Both constructors should be public.

  • Vehicle (): the default constructor.  You should select initial values that make sense.
     
  • Vehicle (String theVehicleType, Location theLocation, Inventory theInventory, double theFuel, String theFuelUnits, double theFuelBurnRate,  double theMoney, double theTotalDistanceTraveled):  the specific constructor, which defines initial values for all of the instance variables.

Accessors/Mutators

As the instance variables in this class are private, they will need to be manipulated via accessor and mutator methods as we have discussed during lecture.  Thus, you must include an accessor and mutator method for each of the above fields in the Vehicle class (we realize this part may be a bit tedious).  The name of the accessor/mutator methods should follow the standard Java naming conventions (i.e. getPosX(), setName(), etc.). Note that your mutators should check for and (thus prevent) nonsense values (such as negative values for fuel or fuel burning rate).  All accessors and mutators should be public. 

Other methods

The Vehicle class should also support the following methods.  All these methods should be public.

  • void addToMoney(double theMoney): add a certain amount of money to the vehicle's money supply.
     
  • void subtractFromMoney(double theMoney): subtract a certain amount of money from the vehicle's money supply.
     
  • void travelToLocation (Location location): This method will do a number of steps:
    1. If that location is null, then print out an appropriate message (and don't travel there, obviously). Note that this should NOT be considered a catastrophic error.
    2. Check to see if there's fuel to make the trip.  To determine if there is enough fuel, first find the distance between the locations (via the getDistance() method that you wrote for HW J5), and multiply that by fuelBurnRate.  If that is less than the fuel available, then there is enough fuel to travel. 
      • If there is enough fuel, then the vehicle should be moved to the new location (i.e. set the Vehicle's location to the new one), the appropriate fuel deducted from the vehicle's fuel reserve, and totalDistanceTraveled updated. 
      • Along the way, you should output the details of the trip (distance it requires, fuel before and after). 
      • Whether you do travel or not, a message should be printed to the screen indicating what happened.
    3. Call Game.advanceTurn().  This method will be used in HW J8.
       
  • void takeTurn (): This method should be a loop, with the following body.  The loop repeats, asking for input each time it iterates.
    1. Print out the current location of the vehicle and the vehicle's current money.
    2. Print the location commands (by invoking the next method, below).
    3. Use the Parser (to be developed in Lab 9 and discussed above) to read and parse the commands.  To do this, you call Parser.parse().  This will return an int value, which will indicate what to do.  The loop keeps repeating until Parser.QUIT is entered.
    4. Act upon the input obtained:
      • Parser.WORLDMAP: This just prints out the map of the universe.  This is done by calling Map.printMap().  The parameter to that method is the current vehicle (i.e. this).
      • Parser.TRAVEL: If the user wants to travel, then three things must happen:
        1. Print the map (via Map.printMap())
        2. Use the parser to get the destination (via Parser.getDestination())
        3. Travel to the location (via travelToLocation())
      • Parser.MARKET: If the user wants to enter the market that the vehicle is currently at, then we must invoke the enterMarket() instance method in the Market class.  The parameter to that method is the current vehicle (i.e. this).  Note that each vehicle has a location, and that each location has a market, and it is this market upon which you should call the enterMarket() method.  Markets don't do very much yet -- this will be changed in HW J7.
      • Parser.INVENTORY: If the user wants to see how much his/her current inventory is worth in the particular market that his/her vehicle is parked in, then we should invoke the printInventoryWithNumbers() instance method in the Inventory class, for the inventory associated with his/her vehicle.  The parameter to this is the market cost factor, which can be determined via the getMarketCostFactor() method in a Market object.
      • Parser.QUIT: if the user wants to quit, then break out of the loop.
      • If the input matches none of the above, then print out an appropriate error message.
     
  • void printLocationCommands(): This method will print out the various commands that the user can type in (see above).  For each of the five possible commands, the user only need enter the first letter (as that is the only one that is checked).  The commands that the user can enter are the same as the field names -- i.e., worldmap, travel, market, inventory, and quit.
     

Please re-read the note in HWJ5 concerning "All-at-once vs. Incremental" design and implementation. This might help you decide how to get started on this assignment.

Game Simulation

We have provided a Game.java file, which is a modification of HWJ5's Game.java. In HWJ6's Game.java, the same five locations are made. Then, a vehicle is created, and the vehicle's takeTurn() method is invoked. When takeTurn() returns, the program prints some summary statements and exits.  A sample invocation of the program is shown below. User input is indicated in red.

Welcome to CS101 Space Trader v2!

Your Intergallactic Thunderbird is starting at Orionis (8.0,2.0).
You burn fuel at the rate of 10.0 gazonkas per million kilometers.

You are at tired Orionis
You have $100.0 on hand
Valid commands are:
	travel (to travel to another location)
	market (to enter the market)
	worldmap (to print out the map)
	inventory (to show your inventory)
	quit (to leave the game)
You can abbreviate any command by its first letter
Enter next command: w
1	-	-	-	-	-	-	-	
-	-	-	-	-	-	-	4	
-	-	2	-	-	-	-	-	
-	-	-	-	-	-	-	-	
-	-	-	-	-	-	-	-	
-	-	-	-	-	-	-	-	
-	-	5	-	-	3	-	-	
-	-	-	-	-	-	-	-	

1 Geminorum
2 Ursae Majoris
3 Cygni
4 Orionis
5 Andromedae


You are at tired Orionis
You have $100.0 on hand
Valid commands are:
	travel (to travel to another location)
	market (to enter the market)
	worldmap (to print out the map)
	inventory (to show your inventory)
	quit (to leave the game)
You can abbreviate any command by its first letter
Enter next command: m
Market not yet enabled...

You are at tired Orionis
You have $100.0 on hand
Valid commands are:
	travel (to travel to another location)
	market (to enter the market)
	worldmap (to print out the map)
	inventory (to show your inventory)
	quit (to leave the game)
You can abbreviate any command by its first letter
Enter next command: i
Inventory not yet enabled

You are at tired Orionis
You have $100.0 on hand
Valid commands are:
	travel (to travel to another location)
	market (to enter the market)
	worldmap (to print out the map)
	inventory (to show your inventory)
	quit (to leave the game)
You can abbreviate any command by its first letter
Enter next command: t
1	-	-	-	-	-	-	-	
-	-	-	-	-	-	-	4	
-	-	2	-	-	-	-	-	
-	-	-	-	-	-	-	-	
-	-	-	-	-	-	-	-	
-	-	-	-	-	-	-	-	
-	-	5	-	-	3	-	-	
-	-	-	-	-	-	-	-	

1 Geminorum
2 Ursae Majoris
3 Cygni
4 Orionis
5 Andromedae

Enter the name (not number!) of the destination you want to travel to: Geminorum 
This is a distance of 7.0710678118654755 million kilometers
This will require 70.71067811865476 gazonkas of fuel
You have 500.0 gazonkas of fuel on board
You travel to Geminorum
You have 429.28932188134524 gazonkas of fuel on board

You are at sunny Geminorum
You have $100.0 on hand
Valid commands are:
	travel (to travel to another location)
	market (to enter the market)
	worldmap (to print out the map)
	inventory (to show your inventory)
	quit (to leave the game)
You can abbreviate any command by its first letter
Enter next command: t
1	-	-	-	-	-	-	-	
-	-	-	-	-	-	-	4	
-	-	2	-	-	-	-	-	
-	-	-	-	-	-	-	-	
-	-	-	-	-	-	-	-	
-	-	-	-	-	-	-	-	
-	-	5	-	-	3	-	-	
-	-	-	-	-	-	-	-	

1 Geminorum
2 Ursae Majoris
3 Cygni
4 Orionis
5 Andromedae

Enter the name (not number!) of the destination you want to travel to: Andromedae
This is a distance of 6.324555320336759 million kilometers
This will require 63.24555320336759 gazonkas of fuel
You have 429.28932188134524 gazonkas of fuel on board
You travel to Andromedae
You have 366.04376867797765 gazonkas of fuel on board

You are at exciting Andromedae
You have $100.0 on hand
Valid commands are:
	travel (to travel to another location)
	market (to enter the market)
	worldmap (to print out the map)
	inventory (to show your inventory)
	quit (to leave the game)
You can abbreviate any command by its first letter
Enter next command: t
1	-	-	-	-	-	-	-	
-	-	-	-	-	-	-	4	
-	-	2	-	-	-	-	-	
-	-	-	-	-	-	-	-	
-	-	-	-	-	-	-	-	
-	-	-	-	-	-	-	-	
-	-	5	-	-	3	-	-	
-	-	-	-	-	-	-	-	

1 Geminorum
2 Ursae Majoris
3 Cygni
4 Orionis
5 Andromedae

Enter the name (not number!) of the destination you want to travel to: home
Can't travel to nowhere!

You are at exciting Andromedae
You have $100.0 on hand
Valid commands are:
	travel (to travel to another location)
	market (to enter the market)
	worldmap (to print out the map)
	inventory (to show your inventory)
	quit (to leave the game)
You can abbreviate any command by its first letter
Enter next command: q

Congratulations, Space Trader! 
Your Intergallactic Thunderbird traveled 13.395623132202235 million kilometers.
You still have 366.04376867797765 gazonkas of fuel available.
Take the rest of the day off, Space Trader -- you deserve it!

Test Code

The Game.java file includes most of what you will need to test your classes.  It is important to confirm that the output is what you expect it to be.

Submission

The good programming practices from HW J1 need to be in this homework as well. 

When you are finished, submit the Vehicle.java file.

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