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...
Polymorphism and Inheritance Shape.h class Shape {             string color;             bool filled; public:      &n
Polymorphism and Inheritance Shape.h class Shape {             string color;             bool filled; public:             Shape();             Shape(string, bool);             string getColor();             bool isFilled();             void setColor(string);             void setFilled(bool);             string toString(); //virtual string toString(); }; Rectangle.h class Rectangle : public Shape {             double length;             double width; public:             Rectangle();             Rectangle(double, double);             Rectangle(double, double, string, bool);             double getLength();             double getWidth();             void setLength(double);             void setWidth(double);             double getArea();             double getPerimeter();...
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?
Objectives: Use class inheritance to create new classes. Separate class definition and implementation in different files....
Objectives: Use class inheritance to create new classes. Separate class definition and implementation in different files. Use include guard in class header files to avoid multiple inclusion of a header. Tasks: In our lecture, we wrote a program that defines and implements a class Rectangle. The source code can be found on Blackboard > Course Content > Classes and Objects > Demo Program 2: class Rectangle in three files. In this lab, we will use class inheritance to write a...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT