Please help me out with this: Design a Ship class that has the following members
ID: 3672956 • Letter: P
Question
Please help me out with this:
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 thr 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 thr cargo capacity in tonnage (an int).
- A constructor and approriate 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.
Explanation / Answer
class Ship{
protected String name;
protected String year;
public Ship(){
}
public Ship(String name,String year){
this.name = name;
this.year = year;
}
public String getName(){
return name;
}
public String getYear(){
return year;
}
public String toString(){
System.out.println(name + " was built in year "+ year);
}
}
class CruiseShip extends Ship{
private int maxPassengers;
public CruiseShip(){
}
public CruiseShip(String name,String year,int maxPassengers){
super(name,year);
this.maxPassengers = maxPassengers;
}
public int getMaxPassengers(){
return maxPassengers;
}
public String toString(){
System.out.println("Cruise ship " + name + " was built in year "+ year + " with maximum passenger capacity of " + maxPassengers);
}
}
class CargoShip extends Ship{
private int cargoCapacity;
public CargoShip(){
}
public CargoShip(String name,String year,int cargoCapacity){
super(name,year);
this.cargoCapacity = cargoCapacity;
}
public getCargoCapacity(){
return cargoCapacity;
}
public String toString(){
System.out.println("Cargo ship " + name + " was built in year "+ year + " having capacity of " + cargoCapacity + " tonnes.");
}
}