Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I use DR. JAVA Hi, thanks for the help. There are some instructions listed befor

ID: 3688786 • Letter: I

Question

I use DR. JAVA

Hi, thanks for the help. There are some instructions listed before the problem. I wasn't able to get my code to compile so i ended up starting from scratch and it still didn't compile, so I decided to ask this question to see what I did wrong.

Save all of the source code and compiled code in a folder called Inheritance

write and name your test program as PolymorphismApp. the data for each object can be hard coded. So you don’t need to ask the user for entering these object data.

Ship, CruiseShip, and CargoShip Classes Design a Ship class that the following members:
o A field for the name of the ship (a string).
o A field for the year that the ship was built (a string).
o A constructor and appropriate accessors and mutators.
o 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:
o A field for the maximum number of passengers (an int).
o A constructor and appropriate accessors and mutators.
o 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:
o A field for the cargo capacity in tonnage (an int).
o A constructor and appropriate accessors and mutators.
o 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 Shiparray. Assign various Ship, CruiseShip, and CargoShip objects to the array elements. The program should then step through the array, calling each object's toString method.

Explanation / Answer

CruiseShip.java

public class CruiseShip extends Ship{

   private int maxShipPassengers;
  
   //
   public CruiseShip(String shipName, String shipBirth, int maxShipPassengers) {
       super(shipName, shipBirth);
       this.maxShipPassengers = maxShipPassengers;
      
   }

   public int getMaxShipPassengers() {
       return maxShipPassengers;
   }

   public void setMaxShipPassengers(int maxShipPassengers) {
       this.maxShipPassengers = maxShipPassengers;
   }

   @Override
   public String toString() {
       return "Ship name: " + shipName + " Maximum number of passengers: " + maxShipPassengers;
   }

}
Ship.java

public class Ship {

   //local variables
   String shipName;
   String shipYear;
  
   //Constructor that accepts ship name and shipyear as arguments
   public Ship(String shipName, String shipYear) {
       this.shipName = shipName;
       this.shipYear = shipYear;
   }

   public String getShipName() {
       return shipName;
   }
  
   public void setShipName(String shipName) {
       this.shipName = shipName;
   }
  
   public String getShipBirth() {
       return shipYear;
   }
  
   public void setShipBirth(String shipBirth) {
       this.shipYear = shipBirth;
   }

   //toString method for ship name and year
   @Override
   public String toString() {
       return "Ship name: " + shipName + " Built in: " + shipYear;
   }
}


CargoShip.java
public class CargoShip extends Ship{

   int shipCargoCapacity;

   public CargoShip(String shipName, String shipBirth, int cargo) {
       super(shipName, shipBirth);
       this.shipCargoCapacity = cargo;
   }
  
   public String toString()
   {
       return "Ship name: " + shipName + " Ship cargo capacity: " + shipCargoCapacity + " tons";
   }
}


ShipDemo.java

public class ShipDemo {
   public static void main(String[] args) {
       Ship s1 = new Ship("Alligator" , "1862");
       CruiseShip s2 = new CruiseShip("Oasis", "2009", 1500);
       CargoShip s3 = new CargoShip("Altmark", "1937", 2000);
      
       Ship[] ships = new Ship[] {s1, s2, s3};
      
       for(Ship output: ships)
       {
           System.out.println(output.toString());
       }

   }

}

sample output


Ship name: Alligator    Built in: 1862                                                                                                                      
Ship name: Oasis        Maximum number of passengers: 1500                                                                                                  
Ship name: Altmark      Ship cargo capacity: 2000 tons