Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Variation of Programming Challenge #10 on pages 678-9. Need to implement Ship, C

ID: 3721684 • Letter: V

Question

Variation of Programming Challenge #10 on pages 678-9. Need to implement Ship, CargoShip, and CruiseShip.

Design a ship class that the following members:

A field for the name of the ship (a string)

A field for the year that the ship was built ( a string)

A constructior and appropriate accessors and mutators

A toString method that displays the ships name and the year it was built

Design a CruiseShip class that exends the ship class. The CruiseShip class should have the following.

A field for the maximum number of passengers (an int)

A constuctor and appropiate accessors and mutators.

A toString method that overrides the toString method in the base class. The CruiseShip class's toString method should display only the ship's name and maximum number of passengers.

Design a CargoShip class that extends the Ships class. The CargoShip class should have the following.

A field for the cargo capacity in tonnage (an int)

A constructior and appropriate accessors and mutators

A toString method that overrides the toString method in the base class. The CargoShip class's toString method should display only the ship's name and ships cargo capacity.

Write a new program that will create an array of 5 Ships. Should be a total of four programs one main and the Ship CargoShip and CruiseShip.

Instantiate each of the 5 elements of the array with some of them being Ship, some being CargoShip, and some being CruiseShip. You don’t have to use the same ships shown here.

Use a loop to print all of the elements in the array.

Sample Output

0 Cruise Ship: SS Minnow - # Passengers: 7

1 Cruise Ship: Titanic - # Passengers: 3327

2 Cargo Ship: Maersk Alabama - tonnage: 14120

3 Cruise Ship: Costa Concordia - # Passengers: 3780

4 Ship: Black Pearl - Built: 1725

Press any key to continue . . .

Explanation / Answer

ShipDemo.java

public class ShipDemo
{
public static void main(String[] args)
{
// Constant for the number of ships.
final int NUM_SHIPS = 5;
  
// Create an array of Ship references.
Ship[] ships = new Ship[NUM_SHIPS];
  
// Assign Ship objects to the array elements.
ships[0] = new CruiseShip("SS Minnow", "1960",7);
ships[1] = new CruiseShip("Titanic", "1998", 3327);
ships[2] = new CargoShip("Maersk Alabama", "1800", 14120);
ships[3] = new CruiseShip("Costa Concordia", "1800", 3780);
ships[4] = new Ship("Black Pearl", "1725");
// Call each object's toString method.
for (int index = 0; index < NUM_SHIPS; index++)
{
System.out.println(index+" "+ships[index]);
}
}
}

Ship.java

public class Ship {

private String name, year;

public Ship(String name, String year) {

super();

this.name = name;

this.year = year;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getYear() {

return year;

}

public void setYear(String year) {

this.year = year;

}

public String toString() {

return "Ship "+getName()+" - Built: "+year;

}

}

CargoShip.java

public class CargoShip extends Ship{

private int capacity ;

public CargoShip(String name, String year , int c) {

super(name, year);

capacity = c;

}

public int getCapacity() {

return capacity;

}

public void setCapacity(int capacity) {

this.capacity = capacity;

}

@Override

public String toString() {

return "Cargo Ship: "+getName()+" - tonnage: "+capacity;

}

}

CruiseShip.java

public class CruiseShip extends Ship{

private int numberOfPassengers;

public CruiseShip(String name, String year , int n) {

super(name, year);

numberOfPassengers = n;

}

public String toString() {

return "Cruise Ship: "+getName()+" - # Passengers: "+numberOfPassengers;

}

public int getNumberOfPassengers() {

return numberOfPassengers;

}

public void setNumberOfPassengers(int numberOfPassengers) {

this.numberOfPassengers = numberOfPassengers;

}

}

Output:

0 Cruise Ship: SS Minnow - # Passengers: 7
1 Cruise Ship: Titanic - # Passengers: 3327
2 Cargo Ship: Maersk Alabama - tonnage: 14120
3 Cruise Ship: Costa Concordia - # Passengers: 3780
4 Ship Black Pearl - Built: 1725