Parser class assignment

From WLCS
Revision as of 10:26, 29 April 2010 by Admin (talk | contribs)

Objective

  • You will learn to create a static class comprised of static variables and methods

Purpose

  • The Parser class allows you to use its internal static methods to obtain input commands for your game.

Attributes

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

  • NORTH = 1
  • EAST = 2
  • SOUTH = 3
  • WEST = 4
  • ATTACK = 5
  • LOOK = 6
  • GET = 7
  • QUIT = 8
  • DO_NOT_UNDERSTAND = -1

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.

Methods

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

  • boolean askYesNoQuestion(String prompt): The parser should print prompt, check stdin for "yes", "y", "no", or "n" answers, and return true or 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 HW J3.
  • 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:
    1. Prompt the user for input (this can be a simple "enter next command") using ConsoleInput.input("enter command");
    2. 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 DO_NOT_UNDERSTAND.

Note: In order to compare Strings and whether or not they match (equal), you must use the equals() method:

String choice = "look";
if (s.equals("look"))
{
    System.out.println("Hey...you chose look!");
}

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

Testing

To help you test your Parser class, we have provided a Media: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.

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