PurposeIn 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.
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 ClassThe 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(); 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 ClassNote 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.
Constructors The Vehicle class should also support the following two constructors. Both constructors should be public.
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.
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 SimulationWe 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 CodeThe 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. SubmissionThe good programming practices from HW J1 need to be in this homework as well. When you are finished, submit the Vehicle.java file. |