ulrail.gif

Lab 9: Creating classes II

  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

The purpose of this lab is to give you additional experience creating methods.  You will develop a Parser class, which will be used to read in user input from the keyboard for use in a computer game.  A parser is the part of a computer program that takes in user input and translates it into another form.  In this lab, our methods will transform the user input into an int value, for example.  This will be used in the final game to obtain user input, and based on that input, do the specified action within the game.  We are not providing skeleton code for the Parser class, but we are providing a ParseUsage.java file to test your Parser class.

There are a number of files that you will need for this lab.  Each of these files has the minimum code necessary to ensure that the Parser.java file works properly.  You should make a separate directory for this lab, as if you do not want the files for this lab to overwrite any other files of the same name for other assignments.  Note that the main file you will use is ParserUsage.java -- this file is used to test your Parser.  The other files are just skeleton code so that your lab will compile.

  • Game.java: This is a stripped-down version of the Game.java files from the homeworks.
  • Inventory.java: This class represents an inventory (in a vehicle or a market), and you will be developing it in HW J7.
  • Location.java: This is a striped-down version of the Location class from HW J5.  You are free to use your own Location.java if you would prefer.
  • Universe.java: This class will represent the entire universe, and you will be developing this in HW J8.
  • ParserUsage.java: This is the class that you will be running to test your Parser.java file.

You need to submit a Parser class in a Parser.java file, for which we are not supplying the skeleton code.

Parser Variables

The Parser class should provide only class methods and class variables; in other words, all methods and variables in the Parser method should be declared static.  Note that this means there will never be a need to create a Parser object (i.e., an instance of the Parser class) because all methods and constants can be access as Parser.parse(), Parser.INVENTORY, etc. 

The Parser class should provide the following integer constants, each representing a possible command that the user might type.  These are class variables (a.k.a. static fields).

  • TRAVEL
  • WORLDMAP
  • INVENTORY
  • MARKET
  • QUIT
  • BUY
  • SELL
  • AVAILABLE
  • EXIT_MARKET
  • DO_NOT_UNDERSTAND

Each one should have a distinct int value (meaning no two can have the same value), although it does not matter what values you assign (you might want to start with 1, and go up from there).  Each field MUST have the EXACT same name as shown above!  The code will test the return value of the parse() method against Parser.SELL, for example, so each one can have any value.  The only exeception is that DO_NOT_UNDERSTAND should have value -1.  Each of the constants should be public final static.  For example, the first one is defined as:

public static final int TRAVEL = 1;

Lastly, the parser should also define a Scanner object as follows:

public static Scanner stdin = new Scanner(System.in);

You should use this Scanner whenever reading in user input -- defining your own Scanner in the methods below will cause your program to not work properly with our grading scripts.

Parser Methods

The Parser class should also provide the following three static methods:

  • public static Location getDestination(): This method is used to determine where the player wants to travel to.  This method takes no parameters.

    This method prints a prompt to enter a destination, reads in that destination (via stdin.nextLine()), and returns the appropriate Location object for that destination.  To find the appropriate Location object, first get the Universe object via Game.getUniverse().  From that object, call the findLocationByName() method, passing in the name (a String) as the parameter.  This method will return the appropriate Location object.  You will be writing this method in HW J8; for now, we have provided a stripped-down version of that method so that this lab will compile.
     

  • public static int getItemToTrade (Inventory inventory, double marketCostFactor): This method is used to print the items that one can buy or sell at a market.  The first parameter is the inventory to print (this can be the inventory of the Vehicle, or of the Market).  The second parameter is the cost factor of the market in which the player is trading goods.

    This method performs a number of steps:

    • Print out the inventory (with an appropriate legend).  The inventory is printed out by calling the printInventoryWithNumbers() method on the passed Inventory object.  The parameter to that method is the cost factor.

    • Prompt the user to enter the number of the item which s/he wants to trade.

    • Read in an int value, which is the item that the player wants to trade.  This is easiest if done via the line int item = Integer.parseInt(stdin.nextLine());.

    • Return that int value read in.
       

  • public static int parse(): This method reads in a character from the keyboard, and returns an int value depending on what was read in. 

    The method only looks at the first character (ignoring case) of the input, so 't' is the same as 'travel', 'Travel', and 'tortellini'.  The steps of the method are as follows:

    • Prompt the user for input (this can be a simple "enter next command")

    • Read in the next line input from the stdin Scanner (via stdin.nextLine()).

    • Convert that line to lower case, and extract the first character (via the charAt() method in the String class).  If the string is empty, you can just return DO_NOT_UNDERSTAND.

    • Use the first character of the input to determine what the user wants to do.  If that character is 'q', then the method should return QUIT, 't' will return TRAVEL, etc.  Note that each of the above commands has a distinct initial character (this is why you have to type 'w' to see the map -- 'm' is to enter the market)

    • If the user input does not match any known command, the parse() method should return DO_NOT_UNDERSTAND.
       

ParserUsage

To help you test your Parser class, we have provided a ParserUsage.java file.  You do not need to modify this file, but you should understand how it works.  This file shows how to use your Parser class, and tests your various methods.  Below is an example of what a sample execution run of ParserUsage.  Line numbers have been added so that it is easier to explain what is going on -- they are not output by the program.  The text in red is what was entered by the user.

1	This program will test the Parser class
2
3	First, the parse() method will be tested
4	Enter q to quit
5	
6	Enter one of the following commands:
7	travel, worldmap, inventory, market, buy, sell, available, exit, or quit
8	
9	Enter next command: t
10	You entered travel
11	Enter next command: T
12	You entered travel
13	Enter next command: TRAVEL
14	You entered travel
15	Enter next command: tRaVeL
16	You entered travel
17	Enter next command: tortellini
18	You entered travel
19	Enter next command: foo
20	You entered an unknown command
21	Enter next command: quit
22	You entered quit
23	
24	Next, the getItemToTrade() method will be tested
25	Possible items to trade:
26	0       Java textbooks
27	1       C++  textbooks
28	2       Perl textbooks
29	Which one do you want to trade? 1
30	Result returned was 1
31	
32	Lastly, the getDestination() method will be tested
33	Enter the name of the desination you want to travel to: Mars
34	You are searching for Mars
35	Location searched for was Mars

What's going on in this output:

  • Lines 9-22: testing of the Parser.parse() method.  Note that it only looked at the first letter of the input, and used that to determine what command was entered; it also ignored the case

  • Lines 21-22: the user entered quit, so this loop ended.

  • Lines 24-30: testing of the getItemToTrade() method

  • Line 30: the value returned from the getItemToTrade() method

  • Lines 32-35: testing of the getDestination() method

Submission

When you are finished, you just need to submit the Parser.java file.  As the ParserUsage.java file was not modified, it does not need to be submitted.

If you are done early....

If you finish this lab early, use any remaining time to work on HW J6.

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