In: Computer Science
This question demonstrates the use of inheritance and
polymorphism.
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 overrides the toString method in the
Object class. The Ship class
toString method should display 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 and print
each object.
PLEASE DO THE QUESTION IN A SIMPLER WAY
Code
Ship class
public class Ship {
private String name;
private String year;
/**
* @param name
* @param year
*/
public Ship(String name, String year) {
super();
this.name = name;
this.year = year;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the year
*/
public String getYear() {
return year;
}
/**
* @param year the year to set
*/
public void setYear(String year) {
this.year = year;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Ship\nName:" + name +
"\nYear Built: " + year;
}
}
CruiseShip class
public class CruiseShip extends Ship{
private int numPasanger;
/**
* @param name
* @param year
* @param numPasanger
*/
public CruiseShip(String name, String year, int
numPasanger) {
super(name, year);
this.numPasanger =
numPasanger;
}
/**
* @return the numPasanger
*/
public int getNumPasanger() {
return numPasanger;
}
/**
* @param numPasanger the numPasanger to set
*/
public void setNumPasanger(int numPasanger) {
this.numPasanger =
numPasanger;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Cruise
"+super.toString()+"\nMaximum number of passengers: " +
numPasanger;
}
}
CargoShip class
public class CargoShip extends Ship{
private int capacity;
/**
* @param name
* @param year
* @param capacity
*/
public CargoShip(String name, String year, int
capacity) {
super(name, year);
this.capacity = capacity;
}
/**
* @return the capacity
*/
public int getCapacity() {
return capacity;
}
/**
* @param capacity the capacity to set
*/
public void setCapacity(int capacity) {
this.capacity = capacity;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Cargo
"+super.toString()+"\nCapacity: " + capacity + " tones";
}
}
ShipTest.java contains main method
public class ShipTest {
public static void main(String[] args) {
Ship shipArray[]=new Ship[3];
shipArray[0]=new Ship("Samish",
"2000");
shipArray[1]=new
CruiseShip("Titanic", "1954",1400);
shipArray[2]=new CargoShip("HMM
Copenhagen", "2020",1000);
for(int
i=0;i<shipArray.length;i++)
System.out.println(shipArray[i]+"\n");
}
}
output
If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.