Question: Create a Ship class for a battleship program. Include a Ship.java, and a ShipTester.java. Make sure to create the program using java, add comments, and
Create a Ship class for a battleship program. Include a Ship.java, and a ShipTester.java. Make sure to create the program using java, add comments, and Javadoc. Use grade 12 level coding.
The first part of writing our Battleship game is to write the Ship.java class. The Ship represents a Ship in the game, or a piece on the board. The Ship has several defining characteristics (think instance variables) including a position, a length and a direction.
Youll need a few instance variables
row - What row location is the ship at? col - What column location is the ship at? length - How long is this ship? direction - Is this ship vertical or horizontal?
To keep track of the direction you should use a few constants:
// Direction constants public static final int UNSET = -1; public static final int HORIZONTAL = 0; public static final int VERTICAL = 1;
The Ship class should have one constructor, taking a parameter of the length of the ship.
// Constructor. Create a ship and set the length. public Ship(int length) // Has the location been initialized public boolean isLocationSet() // Has the direction been initialized public boolean isDirectionSet() // Set the location of the ship public void setLocation(int row, int col) // Set the direction of the ship public void setDirection(int direction) // Getter for the row value public int getRow() // Getter for the column value public int getCol() // Getter for the length of the ship public int getLength() // Getter for the direction public int getDirection() // Helper method to get a string value from the direction private String directionToString() // Helper method to get a (row, col) string value from the location private String locationToString() // toString value for this Ship public String toString()
Thank you.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
