In: Computer Science
//Java code
//ship class
public class Ship {
/**
* A data field for the name of the ship (a string).*/
private String shipName;
/*
A data field for the year that the ship was built (a String).
*/
private String year;
/**
* A constructor
*/
public Ship(String shipName, String year) {
this.shipName = shipName;
this.year = year;
}
/**
* accessors and mutators.
*/
public String getShipName() {
return shipName;
}
public void setShipName(String shipName) {
this.shipName = shipName;
}
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
/**
* A toString method that displays
* the ship’s name and the year it was built.
*/
@Override
public String toString() {
return "Ship's Name: "+shipName+" and the year "+ shipName+" was built: "+year;
}
}
//==============================
/**
* Design a CruiseShip class that extends the Ship class.
*/
public class CruiseShip extends Ship{
/**
* A field for the maximum number of passengers (an int).
*/
private int maximumPassenger;
//Constructor
public CruiseShip(String shipName, String year, int maximumPassenger) {
super(shipName, year);
this.maximumPassenger = maximumPassenger;
}
//Accessor and mutator
public int getMaximumPassenger() {
return maximumPassenger;
}
public void setMaximumPassenger(int maximumPassenger) {
this.maximumPassenger = maximumPassenger;
}
/**
* A toString method that overrides the toString method in the base class. The CruiseShip class's toString method should
* display only the ships name and the maximum number of passengers.
*/
@Override
public String toString() {
return "Ship's Name: "+getShipName()+"\nMaximum number of passenger: "+maximumPassenger;
}
}
//===================================================
/**
* Design a CargoShip class that extends the Ship class
*/
public class CargoShip extends Ship {
/**
* A field for the cargo capacity in tonnage (an int).
*/
private int cargoCapacity;
//Constructor
public CargoShip(String shipName, String year, int cargoCapacity) {
super(shipName, year);
this.cargoCapacity = cargoCapacity;
}
//Accessor and mutator
public int getCargoCapacity() {
return cargoCapacity;
}
public void setCargoCapacity(int cargoCapacity) {
this.cargoCapacity = cargoCapacity;
}
/**
* A toString method that overrides the toString method in the base class. The CargoShip class's toString method
* should display only the ships name and ship’s cargo capacity.
*/
@Override
public String toString() {
return "Ship's Name: "+getShipName()+"\nCapacity: "+cargoCapacity+" tonnes.";
}
}
//==================================
public class ShipDriver {
public static void main(String[] args)
{
//Create array of object
Ship[] ships = new Ship[3];
ships[0] = new Ship("Common ship","2000");
ships[1] = new CruiseShip("Cruise Ship","2010",150);
ships[2] = new CargoShip("Cargo ship","2015",20000);
//Now print info
for (Ship s:ships
) {
System.out.println(s);
}
}
}
//Output

//If you need any help regarding this solution ......... please leave a comment ....... thanks