ulrail.gif

Lab 9: Writing methods

  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

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 our computer game.  A parser is the part of a computer program that takes in user input and parses, or translates, it into another form.  The methods in the class that you develop in this lab are responsible for all user input in the game.

There are two files that you will need for this lab, in order for it to compile: Game.java and Inventory.java.  Each of these files has the minimum code necessary to ensure that the Parser class file compiles properly.  In particular, notice that the Game.java only has a few constants -- most of the other constants (such as the ones you use in HW 7), as well as the main() method, are removed.  You should make a separate directory for this lab, as you do not want the files for this lab to overwrite any other files of the same name for other assignments.

There are two files that are the main part of this lab: Parser.java and ParserUsage.java.  Note that ParserUsage.java is the file that you should run (none of the other classes have a main() method).  ParserUsage's main() method will test the Parser class -- output from that is shown at the end of this lab.  You will only need to submit the Parser.java file.

Parser Variables

The Parser class will only 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.getInteger(), etc.  Because you will never create a Parser object, the constructor is declared as private -- this prevents any code outside the Parser class from calling the constructor (and thus from creating a Parser object).

This Parser class has a Scanner object as a class variable, named stdin.  This Scanner object will be the only one created in the game (i.e. other classes won't have their own Scanner objects).  Do not declare another Scanner object, or very bad things will happen!  (actually, it just jams up the grading system)

The other class variables in the Parser class are the integer constants that are returned from the parse() method.  Each one corresponds to a given command that the user can enter.  To see what command each constant stands for in the game, you can look at the documentation for the Parser class.

Parser Methods

There are four required methods for this class -- all are public and static; we provide skeleton code for these methods, and you have to implement them.  All of these methods can be tested by calling the main() method in ParserUsage.java.  The output from that appears below.

getInteger()

This method will simply read in an integer, and return that value.  Note that you will probably want to call stdin.nextLine() after your call to stdin.nextInt(), because of the bug in the Scanner class.  This method is used by a number of different parts of the program (how many days to rest, how many of a given item to buy, etc.), so it should not provide any prompt -- it just reads in the value, and then returns that value.

getItemToTrade()

This method will first display the inventory (via a call to the inventory.printInventory() method), and then ask the user which item s/he wants to trade (i.e. buy or sell).  The first parameter to  inventory.printInventory() is the passed in priceFactor; the second parameter to inventory.printInventory() should be false (we'll see why later).  The items are listed with corresponding integer values, so the value read in (and then returned from this method) is an int.  Note that you can call the getInteger() method to handle the reading in of the int value.

getMoveDirection()

This method will ask the user what direction s/he wants to travel in, and then read in that value (as a String using nextLine()).  Once the user enters a value, just the first letter is checked.  Thus, if the user enters 'NORTH', 'north', 'nOrTh', or 'naples', it will still count as entering 'n'.  Thus, we are really just looking at the lower-case version of the first letter of the string that is read in.  There are four directions that the user can enter -- north, south, east, and west.  Once the value is read in, you should return Game.MOVE_NORTH, Game.MOVE_SOUTH, etc.  You should not return equivalent values (i.e., 1, 2, etc.)!  The fact that it is using the constants in the Game class allows those constants to change (i.e. Game.MOVE_NORTH can change from 1 to 10 later on).  If you return equivalent values, you will lose points on the lab (same goes for the parse() method, by the way).  Lastly, if an invalid value is entered, the method should loop around and keep repeating the prompt ('what direction do you want to move') and reading in of the value until a valid value is read in.

parse()

This method will read in a single String from the keyboard (using nextLine()).  The (lower-case version of the) first character of that string is analyzed, and the appropriate Parser constant is returned.  There are 19 Parser constants -- from QUIT to AVAILABLE; these are listed in the beginning of the Parser code.  Thus, 'check', 'CHECK', 'cash', and 'CS101' will all return CHECK.  Note that each of the constants start with a different letter -- so any given letter can only return one constant (this is why printing of the map is 'w' for 'worldmap' -- 'm' is taken by 'move').  If the value read in is not recognized (such as 'e', for example), then DO_NOT_UNDERSTAND is returned (unlike the getMoveDirection() method, the parse() method not loop around).

ParserUsage Output

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.  Your output need not be exact, but it should have similar output.  The text in red is what was entered by the user.  Note that while the ParserUsage class will run before you implement the methods in this lab, it won't do much until the methods are completed.

Test of the Parser class.

Test of getInteger():
27
... returned 27

Test of getItemToTrade():
Possible items to trade:
1: Java textbooks
2: More Java textbooks
3: Clickers
4: An advance copy of the third midterm
Which one do you want to trade? 3
... returned 3

Test of getMoveDirection():
What direction would you like to move (n/s/e/w)?
a
What direction would you like to move (n/s/e/w)?
s
... returned Game.MOVE_SOUTH

Test of parse():
Enter next command: cs101 rulz!!!!111oneoneoneONEONEONE
... returned Parser.CHECK

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.  Use any remaining time to work on HW 7.

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