ObjectiveArrays are one of the ways that Java allows one to store items in a list. In this lab, we will gain experience creating and manipulating arrays. You will develop the Descriptions class, which will be used to generate ship names, ship adjectives, and ship home ports for the ships used in your game. This lab is a part of the course project, which is to implement a computer-based version of the game Battleship. If you are not familiar with the game, then you should read the article at that link. Once you are done with this lab, you can use this code in your HW J6, instead of the Descriptions.class file that we provide. FilesThere are two files that you need to download for this lab.
You will need to create and submit a Descriptions.java file, for which we are not providing skeleton code. Customize Your GameThere are a few String lists that you will need to generate for this assignment: ship adjectives, ship types, and ship home ports. In addition, you will need to determine the length of each of your ships. Since each player will get 5 ships, you must have at least 5 ship names. 10 is probably better. Feel free to have more, if you want. Ship TypesYou will need to come up with the ship types that you want to use in your game. The ship types used in the traditional Battleship game are: Destroyer, Cruiser, Submarine, Battleship, and Carrier. But be a bit creative! It need not be limited to marine ships. A few ideas:
Each ship type must be a single word! We see why in a bit, but if you use multi-word adjectives (i.e. "big and tall"), your extractShipType() method will not work properly. Location namesThe next task you must choose is what names you would like for the locations. We have a number of ideas below, but feel free to choose your own. You should have about 10 locations names for this game to work properly.
Ship adjectivesEach ship will have an adjective to describe it. You should have about as many adjectives as locations (i.e. around 10). Note that it's not necessary to have all of the adjectives be from a single category, like "color".
Each adjective must be a single word! We see why in a bit, but if you use multi-word adjectives (i.e. "big and tall"), your extractShipType() method will not work properly. Ship LengthsIn a standard Battleship game, there are 5 ships, of lengths 2 through 5, with two ships having length 3. You are welcome to use your own lengths (as long as they are greater than zero), but you might want to stay with those lengths. The Descriptions ClassAll the members of the Descriptions class are to be static -- thus, we need no constructors, accessors, or mutators. Attributes
Methods There are six methods that you need to create for this class. All of the methods should be public and static.
getShipType()The idea is to cycle through all the ship types in the shipTypes[] array, and then start again at the beginning of the list of ship types. To do this, you will need to keep track of the last ship type returned via a static int variable, which holds which element in the array is to be returned next. Each time this method is called, the variable should be incremented (and you don't want to go beyond the bounds of the array). This will allow you to cycle through the elements of the array. It turns out this can be specified in a single line. Assume that nextShip is an integer class variable initially set to zero. Then the following line will accomplish this: return shipTypes[nextShip++ % shipTypes.length]; Do you understand why this works? It's a great exam question! extractShipType()This method is passed in a String such as, "the mighty ocean kayak from Charlottesville". We must then extract the ship type -- "kayak" -- from the passed String. To do this, we will use the split() method in the String class. The idea of this method is to pass in a delimiter -- in our case, a space. This method will then return an array out of the String, splitting it at each space. Thus, if the String desc is "the mighty kayak from Charlottesville", and you call desc.split(" "), this method will return an array that contains five elements: "the", "mighty", "kayak", "from", and "Charlottesville". This is why the adjectives and ship types must be single words -- if you put spaces more in there, you won't know which element in the array is the ship class. DescriptionGeneratorThis class will test all the methods in your Descriptions class. A sample execution output appears below. Don't worry if duplicate entries occur. A test of the Descriptions class 5 ship adjectives: dirty dirty black imposing tiny 5 ship locations: Lilac Dyton Higgin's Moon Heinlein Bernadette 5 ship types: Destroyer Submarine Cruiser Battleship Carrier 5 full fledged ship names: the green Dinghy from Heinlein the tiny Rowboat from Newhall the black Canoe from Beaumonde the broken Kayak from Santo the tiny Raft from Ezra 5 tests of extractShipType(): the dirty Destroyer from Verbena: Destroyer the slow Submarine from Triumph: Submarine the tiny Cruiser from Regina: Cruiser the wimpy Battleship from Beylix: Battleship the black Carrier from Londinium: Carrier 5 tests of everything: the fast Destroyer from Hera has length 2 the mighty Submarine from Santo has length 3 the blue Cruiser from Verbena has length 3 the purple Battleship from Dyton has length 4 the broken Carrier from Bellerophon has length 5 Hints on where to startThe DescriptionGenerator.java can be used to test all of your methods. Focus on one method, such as getShipAdjective(). The bodies of the for loops are commented out -- remove the '//' for each for loop body to test each method. Thus, you should implement one method, un-comment the corresponding for loop body, and make sure it works before moving onto the next one. SubmissionWhen you are finished, you just need to submit the Descriptions.java file. Note that there are a few more questions that are asked for this lab submission. As the DescriptionGenerator.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. |