Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This is my Ship.java, can you make a ShipTester.java for it, please . Make sure to include comments and Javadoc, description is below. Oh, can

This is my Ship.java, can you make a ShipTester.java for it, please. Make sure to include comments and Javadoc, description is below. Oh, can you also make sure the Ship.java is written correctly? Grade 12 level coding only in java.

public class Ship { int row; int col; int length; int direction; public static final int UNSET = -1; public static final int HORIZONTAL = 0; public static final int VERTICAL = 1;

// Constructor. Create a ship and set the length. public Ship(int length) { this.length = length; this.row = UNSET; this.col = UNSET; this.direction = UNSET; }

// Has the location been initialized public boolean isLocationSet() { if(row != UNSET && col != UNSET) { return true; } else { return false; } }

// Has the direction been initialized public boolean isDirectionSet() { if(direction != UNSET) { return true; } else { return false; } }

// Set the location of the ship public void setLocation(int row, int col) { this.row = row; this.col = col; }

// Set the direction of the ship public void setDirection(int direction) { this.direction = direction; }

// Getter for the row value public int getRow() { return row; }

// Getter for the column value public int getCol() { return col; }

// Getter for the length of the ship public int getLength() { return length; }

// Getter for the direction public int getDirection() { return direction; }

// Helper method to get a string value from the direction private String directionToString() { String theDirect = ""; if(direction == HORIZONTAL) { theDirect = "horizontal"; } else if(direction == VERTICAL) { theDirect = "vertical"; } else if(direction == UNSET) { theDirect = "unset direction"; } return theDirect;

}

// Helper method to get a (row, col) string value from the location private String locationToString() { String theLocat = "(" + Integer.toString(row) + ", " + Integer.toString(col) + ")"; if(isLocationSet() == false) { theLocat = "(unset location)"; } return theLocat; }

// toString value for this Ship public String toString() { return directionToString() + " ship of length " + Integer.toString(length) + " at " + locationToString(); } }

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()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Fundamentals Of Database Systems

Authors: Sham Navathe,Ramez Elmasri

5th Edition

B01FGJTE0Q, 978-0805317558

More Books

Students also viewed these Databases questions