Question

In: Computer Science

This question demonstrates the use of inheritance and polymorphism. Design a Ship class that has the...

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.

Solutions

Expert Solution

Short Summary:

  • Provided the source code and sample output as per the requirements.

**************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!!!

**************************************************************************************


Related Solutions

Inheritance - Polymorphism One advantage of using subclasses is the ability to use polymorphism. The idea...
Inheritance - Polymorphism One advantage of using subclasses is the ability to use polymorphism. The idea behind polymorphism is that several different types of objects can have the same methods, and be treated in the same way. For example, have a look at the code we’ve included for this problem. We’ve defined Shape as an abstract base class. It doesn’t provide any functionality by itself, but it does supply an interface (in the form of .area() and .vertices() methods) which...
Design a Ship class that has the following members: • A member variable for the name...
Design a Ship class that has the following members: • A member variable for the name of the ship (a string) • A member variable for the year that the ship was built (a string) • A constructor and appropriate accessors and mutators • A virtual print function that displays the ship’s name and the year it was built. Design a CruiseShip class that is derived from the Ship class. The CruiseShip class should have the following members: • A...
Write a simple short Java program of your choice which illustrates inheritance, polymorphism and the use...
Write a simple short Java program of your choice which illustrates inheritance, polymorphism and the use of interfaces. The program should not take up more than half a page of size A4.
This is for a java program. Payroll System Using Inheritance and Polymorphism 1. Implement an interface...
This is for a java program. Payroll System Using Inheritance and Polymorphism 1. Implement an interface called EmployeeInfo with the following constant variables: FACULTY_MONTHLY_SALARY = 6000.00 STAFF_MONTHLY_HOURS_WORKED = 160 2. Implement an abstract class Employee with the following requirements: Attributes last name (String) first name (String) ID number (String) Sex - M or F Birth date - Use the Calendar Java class to create a date object Default argument constructor and argument constructors. Public methods toString - returning a string...
In c++, when dealing with inheritance in a class hierarchy, a derived class often has the...
In c++, when dealing with inheritance in a class hierarchy, a derived class often has the opportunity to overload or override an inherited member function. What is the difference? and which one is the better?
Given the following specification, design a class diagram using PlantUML. To design the class diagram, use...
Given the following specification, design a class diagram using PlantUML. To design the class diagram, use abstract, static, package, namespace, association, and generalization on PlantUML Specification: A school has a principal, many students, and many teachers. Each of these persons has a name, birth date, and may borrow and return books. The book class must contain a title, abstract, and when it is available. Teachers and the principal are both paid a salary. A school has many playgrounds and rooms....
The purpose of this lab is to practice using inheritance and polymorphism. We need a set of classes for an Insurance company.
The purpose of this lab is to practice using inheritance and polymorphism.    We need a set of classes for an Insurance company.   Insurance companies offer many different types of policies: car, homeowners, flood, earthquake, health, etc.    Insurance policies have a lot of data and behavior in common.   But they also have differences.   You want to build each insurance policy class so that you can reuse as much code as possible.   Avoid duplicating code.   You’ll save time, have less code to...
Which Feature of OOP illustrates code reusability? Abstraction, Encapsulation, Inheritance, Polymorphism or All of them.
Which Feature of OOP illustrates code reusability? Abstraction, Encapsulation, Inheritance, Polymorphism or All of them.
explain the principle of the inheritance give an example( CS related) of class inheritance
explain the principle of the inheritance give an example( CS related) of class inheritance
Use inheritance to implement the following classes: A: A Car that is a Vehicle and has...
Use inheritance to implement the following classes: A: A Car that is a Vehicle and has a name, a max_speed value and an instance variable called the number_of_cylinders in its engine. Add public methods to set and get the values of these variables. When a car is printed (using the toString method), its name, max_speed and number_of_cylinders are shown. B: An Airplane that is also a vehicle and has a name, a max_speed value and an instance variable called the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT