|
|||||
Home |
Resources | Homeworks |
Exams |
ObjectiveThe 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.
You need to submit a Parser class in a Parser.java file, for which we are not supplying the skeleton code. Parser VariablesThe 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).
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 MethodsThe Parser class should also provide the following three static methods:
ParserUsageTo 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:
SubmissionWhen 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. |