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.
Short Summary:
**************Please upvote the answer and appreciate our time.************
Source Code:
Ship.java:
public class Ship {
String name;
String year;
public Ship(){
name = "";
year = "";
}
// constructor parameterized
public Ship(String name, String year) {
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;
}
public String toString() {
return this.name + " " +
this.year;
}
}
CruiseShip.java:
class CruiseShip extends Ship {
// attribute of CruiseShip
private int maximumPassenger;
// Constructor parameters
public CruiseShip(String name, String year, int
capacity) {
super(name, year);
maximumPassenger = capacity;
}
// accessor and mutator
public void setPassengerCapacity(int capacity) {
maximumPassenger = capacity;
}
public int getPassengerCapacity() {
return maximumPassenger;
}
//
@Override
public String toString() {
String result = "";
result = "Cruise Ship \n";
result = result + "Name: " +
getName() + "\n";
result = result + "Passenger
Capacity: " + getPassengerCapacity() + " persons\n";
return result;
}
}
CargoShip.java:
//subclass CargoShip from superclass Ship
public class CargoShip extends Ship {
// private member variable of CargoShip class
private int tonnage;
// Constructor parameters
public CargoShip(String name, String year, int weight)
{
super(name, year);
this.tonnage = weight;
}
// accessor and mutator
public void setTonnage(int weight) {
tonnage = weight;
}
public int getTonnage() {
return tonnage;
}
// The toString() method overridden by the
subclass
@Override
public String toString() {
String result = "";
result = "Cargo Ship \n";
result = result + "Name: " +
getName() + "\n";
result = result + "Cargo Capacity:
" + getTonnage() + " tons\n";
return result;
}
}
main.cpp:
public class DemoMain {
// Driver method to create objects of all three ships
and print their details
public static void main(String[] args) {
// Create an array of type Ship, to
hold Ship objects
Ship ShipArray[] = new Ship[3];
// Create an object of super
class Ship
Ship ship1 = new Ship("Ship
Element", "1978");
// Create an object of subclass
CruiseShip
Ship ship2 = new CruiseShip("Cruise
Ship Element", "1978", 5000);
// Create an object of subclass
CargoShip
Ship ship3 = new
CargoShip("CargoShip Element", "1978", 20000);
// Store all three objects in
the array
ShipArray[0] = ship1;
ShipArray[1] = ship2;
ShipArray[2] = ship3;
// Iterate through Loop through
the array to print all ships
for (int index = 0; index <
ShipArray.length; index++) {
System.out.println(ShipArray[index]);
System.out.println();
}
}
}
Sample Run:
**************************************************************************************
Feel free to rate the answer and comment your questions, if you have any.
Please upvote the answer and appreciate our time.
Happy Studying!!!
**************************************************************************************