Laboratory 9

More on class creation

Week of Wednesday, 2 November, 2005


Home | Resources | Homeworks | Slides | Labs | Contacts | Submit | Forums

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 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.

 

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.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.

 

Parser Methods

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

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.

 

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	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:

 

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 or HW J7.