boolean isSunk Return true if every part of the ship has bee

boolean isSunk() Return true if every part of the ship has been hit, false otherwise. class Battleship extends Ship class Cruiser extends Ship class Destroyer extends Ship class Submarine extends Ship Each of these classes has a constructor, the purpose of which is to set the inherited length variable to the correct value, and to initialize the hit array. String getShipType() Returns one of the strings \"battleship\", \"cruiser\", \"destroyer\", or \"submarine\", as appropriate. public String toString() Returns a single-character String to use in the Ocean\'s print method (see below). This method should return \"x\" if the ship has been sunk, \"S\" if it has not been sunk. This method can be used to print out locations in the ocean that have been shot at; it should not be used to print locations that have not been shot at. Since toString behaves exactly the same for all ship types, it can be moved into the Ship class, and simply inherited by each individual type of ship.

Solution

public class Battleship extends Ship

{

public String getShipType()

{

return \"battleship\";

}

public String toString()

{

int count=0;

for(int i=0;i<hit.length();i++)

{

count++;

}

if(count==hit.length())

return \"x\";

else return \"S\";

}

Battleship(int len,int[] arr)

{

length=len;

hit=arr;

}

}

public class Cruiser extends Ship

{

public String getShipType()

{

return \"cruiser\";

}

public String toString()

{

int count=0;

for(int i=0;i<hit.length();i++)

{

count++;

}

if(count==hit.length())

return \"x\";

else return \"S\";

}

Cruiser(int len,int[] arr)

{

length=len;

hit=arr;

}

}

public class Destroyer extends Ship

{

public String getShipType()

{

return \"destroyer\";

}

public String toString()

{

int count=0;

for(int i=0;i<hit.length();i++)

{

count++;

}

if(count==hit.length())

return \"x\";

else return \"S\";

}

Destroyer(int len,int[] arr)

{

length=len;

hit=arr;

}

}

public class Submarine extends Ship

{

public String getShipType()

{

return \"submarine\";

}

public String toString()

{

int count=0;

for(int i=0;i<hit.length();i++)

{

count++;

}

if(count==hit.length())

return \"x\";

else return \"S\";

}

Submarine(int len,int[] arr)

{

length=len;

hit=arr;

}

}

 boolean isSunk() Return true if every part of the ship has been hit, false otherwise. class Battleship extends Ship class Cruiser extends Ship class Destroyer

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site