More on class creation
Week of Wednesday, 2 November, 2005
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 or a boolean value. 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.
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.NORTH, 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. class fields).
The first one (NORTH) needs to have integer value 1, the second has 2, etc. (thus, QUIT has value 8). Each of the constants should be public final static. For example, the first one is defined as:
public static final int NORTH = 1;
Note that the last constant (DO_NOT_UNDERSTAND) has value -1, not value 9. Also, 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.
The Parser class should also provide the following three static methods:
prompt): The parser should print prompt, check
stdin for "yes", "y", "no", or "n" answers, and return true
false for yes or no answers, respectively. This method should
not care about capitalization, so "YES" and "yes" are both valid inputs. If the method does not
understand the input, it should print "Please answer yes or no.",
and wait for user input again. Thus, this method works similar to the
askUser() method from the YesNoExtractor class of
public static void printCommands(): This
method will print out the available commands (north, east, look, attack,
etc.) for the parse() method. This can print out each
of the individual commands, or it can just print out a summary. See
the sample execution run below (lines 6-8) for an example of how this method might work.
int parse(): This method does a few things:
Prompt the user for input (this can be a simple "enter next command")
Read in that input from the stdin Scanner.
Compare the read in input to a series of acceptable strings for each of the above constants. If the input contains
the user's typed command "north" or "n", the parse()
method should return the integer constant NORTH.
If the user types "attack", the method should return ATTACK,
if the user types "look" the method returns LOOK,
"get" for GET, and so
on. We don't care about case, so you should use the
equalsIgnoreCase() method instead of the equals()
methods. Note that if the first letter of the user's input line matches,
then you should still return the appropriate value (so "l" is valid for
"look", as is "large" and "LAB"). If the user
input does not match any known command, the parse() method should return
Both methods in your parser should ignore capitalization, so that for instance "N" returns the same value as "n", and "Attack" returns the same value as "attack". You may want to use the String.equalsIgnoreCase() or String.toLowerCase() methods to accomplish this.
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 Valid commands are any of the four directions (north, south, east, west), 7 look, attack, get, or quit 8 You can abbreviate any command by its first letter 9 10 Enter next command: look 11 You entered look 12 Enter next command: LOOK 13 You entered look 14 Enter next command: l 15 You entered look 16 Enter next command: L 17 You entered look 18 Enter next command: LAB 19 You entered look 20 Enter next command: LaBoRaToRy 21 You entered look 22 Enter next command: q 23 You entered quit 24 25 Next, the askYesNoQuestion() will be tested 26 Do you like green eggs and ham? 27 maybe 28 Please answer yes or no 29 i don't know, sam i am 30 Please answer yes or no 31 perhaps 32 Please answer yes or no 33 YES 34 35 Result was true
What's going on in this output:
Lines 6-8: the result of the Parser.printCommands() method call.
Lines 10-21: read in the user's input. 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 22-23: the user entered quit, so this loop ended.
Line 26: the prompt parameter to the askYesNoQuestion() method.
Lines 27-33: the askYesNoQuestion() method looped until the user entered a valid response ('y', 'yes', 'no', and 'n' being valid responses). This method also ignored the case.
Line 35: the returned value from the askYesNoQuestion() method.
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 finish this lab early, use any remaining time to work on HW J6 or HW J7.