We have provided the class Simulation that runs a simulated battle between two fleets of Battleships. For project 5, you will have to implement a Battleship class that allows the battle to take place.
A Battleship in our simulation has a (String) name, an integer maximum hull strength and integer maximum firepower. Since a battleship can be damaged, a BattleShip class also has to keep track of its *current* hull strength and firepower.
Implement the Battleship class with the following constructors:
public Battleship(String name, int firepower, int hullStrength)
Where name is the name of the ship, firepower is a non-negative integer and hullStrength is a non-negative integer.
When 'commissioned' a Battleship's current hullStrength and firepower are equal to it's maximum possible hullStrength and firepower
public Battleship(Battleship other)
Creates a 'copy' of the battleship. All fields in the new battleship should be equal to the original. Please make this a 'deep' copy (hint: refers to how you copy the name).
By default, create a Battleship with firepower equal to 1, hull strength equal to 1, with the name "Cannon Fodder"
Also implement a number of 'getters' for the various fields within the battleship class:
public int getMaxFirepower()
public int getCurrentFirepower()
public int getMaxHullStrength()
public int getCurrentHullStrength()
public String getName()
as well as the, by now familiar, toString method:
public String toString()
This method must return the information within the battleship as follows: the name of the ship, followed by a space then (currentFirepower/maxFirePower, currentHullStrength/maxHullStrength) as a String.
For example, a ship named "Monitor" with current hull strength = 20, maximum possible hull strength = 25, current firepower = 3 and maximum possible firepower = 5, should read as:
"Monitor (3/5, 20/25)"
(Note the space between the name and the data, as well as the space after the comma.)
You should also implement the following methods that manipulate the battleship data:
public boolean hasSunk()
A ship has sunk if it's HullStrength has been reduced to 0.
public boolean isOperational()
A ship is operational as long as it as not sunk and it's current firepower is not zero.
public void hullHit(int damage)
A hull hit reduces the hull strength of a Battleship by damage. Hull strength cannot be reduced below zero (if a hit would reduce below zero, reduce to zero instead)
public void deckHit(int damage)
A deck hit delivers half of its damage (rounding up!) to the hull strength. The rest of the damage reduces the firepower of the ship by the same amount. For example, if a ship had firepower 5 and hull strength 20, then a deckHit(5) would reduce firepower to 3 and hull strength to 17. Like hull strength, firepower cannot be reduced below zero.
public void attackHull(Battleship target)
If the ship is not operational, do nothing. Otherwise, deliver a hullHit to the target, dealing damage equal to the attacking ship's current firepower.
public void attackDeck(Battleship target)
If the ship is not operational, do nothing. Otherwise, deliver a deckHit to the target, dealing damage equal to the attacking ship's current firepower.
Finally, you must implement an equals method with the prototype:
public boolean equals(Battleship other)
Those of you who want more challenges can change the simulation code to create more interesting simulations. If you do so, please let us know so that we can show your colleagues the results.