|
|||||
Home |
Resources | Homeworks |
Exams |
Course ProjectThis homework is the first 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. There are a number of classes that will be required for this project, split among the next few homeworks and labs.
PurposeFor this homework, you are responsible for implementing the Ship and Cell classes. You must submit these two, along with the Game.java file. We are not providing skeleton code for Ship.java and Cell.java.
Getting StartedYou will need to download two files: Game.java and Descriptions.class (right click on the links and select "save as..."). Their usage is described below. You will need to implement (and submit) a Ship class (in Ship.java) and a Cell class (in Cell.java), as well as develop the main() method in Game.java. Note that the ONLY thing in this homework that should be static is your main() method. If anything else is static, you are doing something wrong.
Game classWe provide a Game.java file, which includes a number of things that you will need for the homework. Random ValuesTo choose a random value, you will need to use the Random class. We have provided a object of class Random for you to use. YOU MUST USE THIS ONE, as we will need that for testing and grading. In order to create a random integer, you would call Game.rand.nextInt(x), where x is any integer value. This will return a random number between 0 and x-1. So if you call Game.rand.nextInt(15), it will return a random number between 0 and 14. You can choose a random boolean via Game.rand.nextBoolean(), etc. Game constantsThe Game class also defines a few constants that will be used in this homework. In particular, it defines the size of the board. BOARD_X_COORDINATE is the width of the board, and BOARD_Y_COORDINATE is the height of the board. Although they are both set to 15, you should not assume that they will be set to those values during testing. Thus, you should use Game.BOARD_X_COORDINATE as the width of the board, and not 15. main() methodYou will have to modify the main() method, as described below. You should read the homework through before you begin, as completing the main() method will be easier if done while developing your Cell and Ship classes.
Descriptions classThis class is responsible for generating the names and lengths of the ships, which are used in the Ship's default constructor, below. You will be implementing it in lab 10. In the meantime, the one we provide has two methods that are needed for this assignment:
Because the methods are static, you need to call them by the class name, not the object name. Thus, to find the name of a ship, you call Descriptions.getNextShipName(). This returns a String. To find the length of that ship, you call Descriptions.getShipLength(), passing in the name of the ship that you just got via the previous method. Once you finish your Descriptions.java file from lab 9, you can use that instead of the Descriptions.class file that we provide.
Cell classThe first class you must implement represents a single spot on the Battleship board. As with the Ship class, nothing should be static, all fields must be private, and all methods public. Note that because this class uses the Ship class (developed next), it will not compile unless you have a Ship.java file. It doesn't have to have much in it yet -- it can just be public class Ship { }. Nothing should be static. All fields must be private. Unless specified otherwise, all methods must be public. PropertiesThe Cell class only has two properties:
ConstructorsThere is only a need for the default constructor in this class. Your constructors should call the mutators instead of setting the fields directly! &nbps;You may explicitly initialize the instance variables when they are declared, of course.
Accessors and MutatorsYour class needs to have an accessor and mutator for EACH of the properties listed above. The methods must following the standard naming scheme (i.e. getIsHit(), setShipOnCell(), etc.) Other MethodsNo other methods are required for this class.
Ship ClassThe second class you must implement is the Ship class. Your Ship class needs to be declared public, and in a Ship.java file. Nothing should be static. All fields must be private. Unless specified otherwise, all methods must be public. PropertiesIt should have the following properties.
Please make sure you understand what each of those properties does -- if you don't, then you will have trouble with the rest of the assignment. ConstructorsYour ship class needs to have two constructors: a default constructor and a specific constructor. Your constructors should call the mutators instead of setting the fields directly! You may explicitly initialize the instance variables when they are declared, of course.
Accessors and MutatorsYour class needs to have an accessor and mutator for EACH of the properties listed above. The methods must following the standard naming scheme (i.e. getIsHorizontal(), setName(), getYPosition(), etc.). Your mutators should check to make sure that the variables are not set to invalid values! For example, you can't set the x-coordinate to less than zero, or greater than or equal to the board width. We didn't mention this in the Cell class, as there aren't really any invalid values that one can set the variables to. But in the Ship class, there are a bunch (length, x and y positions, in particular). Other MethodsNote that two of these methods are private (placeHorizontally() and placeVertically()).
Your main() method in the Game.java file should test all of the methods that you implement. You will notice that it currently tests all of the Cell methods. The basic idea is to call a method, and verify that it does what it was supposed to do by calling an accessor method. For example, by calling the Ship's specific constructor, it is supposed to set a number of values. You need to verify this by calling the accessor method, and checking to see that they return the right values. You can use specific values for these tests -- i.e., you can create a Ship called 'Catalan', and then check to see that the name of the ship is indeed 'Catalan'.
Where to StartWhile the main() method may seem like a lot of work, there are a couple of things that will make it much easier. If you add the code to Game.java as you go along, it will make it much easier. The idea is to implement a method or two, and then add the test code to Game.java. Then make sure it compiles and works properly. Then go onto adding more methods. If you get stuck in a method (i.e. can't get it to work, it won't compile, etc.), then move onto another one, and come back to the one that is giving you problems. Also note that the code for the testing of the accessors and mutators is going to be very similar -- you can just copy-and-paste the code, then modify it to use the other accessors/mutators. Lastly, this homework will be much easier if you work through it one method at a time. Write the first method, and then sufficiently test it in your main() method. Then write the second method, and sufficiently test it in your main() method. This will make the entire homework much easier, and it will take much less time to complete.
Good Programming PracticesAll the good programming practices from HW J1 should be included here as well. Since you are providing code in the main() method of the Game class to test your other code, you don't need to include execution runs as comments.
SubmissionWhen you are finished, submit the Ship.java, Cell.java, and Game.java files. |