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.

PLEASE DO THE QUESTION IN A SIMPLER WAY

Solutions

Expert Solution

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.


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