|
|||||
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 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 (and much of the output). There are a number of files that you will need for this lab, in order for it to compile: Board.java, Game.java, MapPrinter.java, Ship.java, ShipList.java. Each of these files has the minimum code necessary to ensure that the Parser.java file compiles properly. 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 only need to submit the Parser.java file. 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.displayResult(), etc. This Parser class has only one class variable: a Scanner object. This one Scanner object will be the only one created in the game (i.e. other classes won't have their own Scanner objects). Parser MethodsThere are three required methods for this class -- all should be public and static. All of these methods are tested by main() method in ParserUsage.java. The output from that appears below. displayResult()This method will print out the result of an attempted shot. There are four possibilities: the shot is a miss, the shot is a hit, the shot is a hit and the ship is then sunk, or the shot hits a cell (ocean or ship) that has already been shot at. In the Board class, there are integer constants defined for these four possibilities (note that NOT_SHOT_AT will be used later; it's not needed in this lab). This method will take in a single int parameter, and does not return a value. The method just prints out a single line, depending on what the parameter is. This method will print out a line depending on the value of the parameter.
How you implement this is up to you -- you can use a switch statement, if-else-if, etc. Note that if the integer parameter is none of the above, the method should not print out anything. And you should test the passed parameter against the constants in the Board class, not against their values (i.e. don't assume that Board.MISSED will always be 1). placeOwnShips()This method will prompt the user with the question of whether s/he wants to place the ships on the board (as opposed to having the program randomly pick spots for the player's ships). It will return true or false, depending on whether the user wants to place his/her own ships or not. An easy way to implement this method would be the following: public static boolean placeOwnShips() { System.out.println ("Would you like to place your own ships? (true/false)"); return scanner.nextBoolean(); } Copy and paste this into the Parser class, compile, and run ParserUsage. While this will work, notice what happens when you enter something other than true or false (such as, "yes"). Thus, we need a more graceful way to handle this input so the program does not crash. Comment out the old method, as we will be re-writing it. The new method will have the same prototype (i.e., public static boolean placeOwnShips()) as the old method. After prompting the user with the question, this method should read in a String (via nextLine()), instead of a boolean. If the string is 'y', 'yes', 'n', or 'no', then it will return true or false, as appropriate. The method should ignore case! String provides an equalsIgnoreCase() method for this purpose. If an invalid answer is provided, the method will tell the user to give an appropriate answer, and loop around, waiting for more input. The method will continue to read in input until a proper response is received. This method is the basis of the YesNoExtractor method that you used in HW J3. That method had one other type of input it allowed: a 'q' or a 'quit' would immediately call System.exit(0), instead of returning true or false (this was added so we could test your code; we didn't mention it at the time). You are welcome to put that into this method, although it is not required. getLocation()This method will ask the user for input as to an (x,y) location from the keyboard. It will prompt the user to enter the next coordinate of where to shoot, read in two integers, and return a new Point object. Note that the Point class is in the java.awt library, hence the import line at the top of Parser.java and ParserUsage.java. The specific constructor of the Point class takes in two coordinates, which are the int values for the x and y coordinate of the Point. There are a few other things this method will need to do. We need to allow the user to gracefully quit the program. If s/he enters a negative number for either of the x or y coordinates, then the program should immediately exit (via System.exit(0)). Note that if the user enters -1 for x (which is prompted for first), the method should not prompt for y. Returning a value for x or y that is too large will cause problems. Thus, if the x value entered is greater than or equal to Game.BOARD_X_COORDINATE, it should tell the user that s/he entered an invalid value, and re-prompt the user for that particular value. This behavior is similar to what you programmed for the placeOwnShips() method. You will notice that we are specifying what this method must do very differently that we have done for previous methods in the past -- and intentionally so. The best way to proceed is to develop this method incrementally. First, have the method read just return a new Point with (0,0) as the coordinates. Next, have the method read in two integers, and return the new Point with those values rather than (0,0). Next, put the prompt in there. Then call System.exit(0) if the user enters a negative number for x (or for y). Then put in a loop to ask again for a value that is too large. In this way, you will be able to write this entire method in small steps, testing along the way. setupBoard()This method is much more complex, and too difficult to test within the time allotted for during lab. Thus, we are providing you code for the method. However, you need to go through the code and understand how it works. You will need comment the code. There should be enough comments there to show the graders that you understand what is going on (we are looking for succinct comments to show you get the idea -- please don't write paragraphs and paragraphs of comments). 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. 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 placeOwnShips(): Would you like to place your own ships? (y/n) nah Please answer y, n, yes, or no Would you like to place your own ships? (y/n) okay Please answer y, n, yes, or no Would you like to place your own ships? (y/n) yes ... returned true Test of displayResults(): For Board.SHIP_SUNK: hit and sunk a ship! For Board.SHIP_HIT: Hit! For Board.MISSED: Miss For Board.SAME_TARGET: already shot there... First test of getLocation(): Please enter the x coordinate of where to shoot (or a negative number to quit): 100 The x coordinate cannot be greater than or equal to 15; please re-enter 10 Please enter the y-coordinate: 35 The y coordinate cannot be greater than or equal to 15; please re-enter 5 Returned location: java.awt.Point[x=10,y=5] Second test of getLocation(): Please enter the x coordinate of where to shoot (or a negative number to quit): -1 Quitting. Goodbye! 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. |