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

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...
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...
Java assignment, abstract class, inheritance, and polymorphism. these are the following following classes: Animal (abstract) Has...
Java assignment, abstract class, inheritance, and polymorphism. these are the following following classes: Animal (abstract) Has a name property (string) Has a speech property (string) Has a constructor that takes two arguments, the name and the speech It has getSpeech() and setSpeech(speech) It has getName and setName(name) It has a method speak() that prints the name of the animal and the speech. o Example output: Tom says meow. Mouse Inherits the Animal class Has a constructor takes a name and...
Design a Ship class that the following members: A field for the name of the ship...
Design a Ship class that 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 displays 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...
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...
Design a ship class that has the following data fields: A data field for the name...
Design a ship class that has the following data fields: A data field for the name of the ship (a string). A data field for the year that the ship was built (a String). A constructor and appropriate accessors and mutators. A toString method that displays 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 member: A field for the maximum number of passengers...
OOP involves use of the following concepts: inheritance, polymorphism, and encapsulation. What are your definitions of...
OOP involves use of the following concepts: inheritance, polymorphism, and encapsulation. What are your definitions of these terms? Why are they important? Does a program necessarily suffer if one or more of these items are absent?
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT