Design a Ship class that has the following members: A field for the name of the
ID: 3725741 • Letter: D
Question
Design a Ship class that has 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 constructor and appropriate accessors and mutators.
A toString method that displays the ship's name and the year it was built.
Design a CruiseShip class that extends the Ship class. The CruiseShip class should have the following members:
A field for the maximum number of passengers (an int).
A constructor and appropriate 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 the maximum number of passengers.
Design a CargoShip class that extends the Ship class. The CargoShip class should have the following members:
A field for the cargo capacity in tonnage (an int).
A constructor 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 the ship's cargo capacity.
Demonstrate the classes in a program that has a Ship array. Assign various Ship, CruiseShip, and CargoShip objects to the array elements. The program should then step through the array, calling each object's toString method.
Explanation / Answer
ShipDemo.java
public class ShipDemo
{
public static void main(String[] args)
{
// Constant for the number of ships.
final int NUM_SHIPS = 3;
// Create an array of Ship references.
Ship[] ships = new Ship[NUM_SHIPS];
// Assign Ship objects to the array elements.
ships[0] = new Ship("Lolipop", "1960");
ships[1] = new CruiseShip("Disney Magic", "1998", 2400);
ships[2] = new CargoShip("Black Pearl", "1800", 50000);
// Call each object's toString method.
for (int index = 0; index < 3; index++)
{
System.out.println(ships[index]);
System.out.println("----------------------------");
}
}
}
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;
}
@Override
public String toString() {
return "Ship [name=" + name + ", year=" + year + "]";
}
}
CruiseShip.java
public class CruiseShip extends Ship{
private int numberOfPassengers;
public CruiseShip(String name, String year , int n) {
super(name, year);
numberOfPassengers = n;
}
@Override
public String toString() {
return "CruiseShip [name=" + super.getName() + "] [numberOfPassengers=" + numberOfPassengers + "]";
}
public int getNumberOfPassengers() {
return numberOfPassengers;
}
public void setNumberOfPassengers(int numberOfPassengers) {
this.numberOfPassengers = numberOfPassengers;
}
}
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 "CargoShip [capacity=" + capacity + "]";
}
}
Output:
Ship [name=Lolipop, year=1960]
----------------------------
CruiseShip [name=Disney Magic] [numberOfPassengers=2400]
----------------------------
CargoShip [capacity=50000]
----------------------------