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

In Java Ship Inheritance Design a Ship class that the following members: A field

ID: 3698171 • Letter: I

Question

In Java

Ship Inheritance 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 ships 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.

Explanation / Answer

###############################

public class Ship {
  
   private String name;
   private int year;
   /**
   * @param name
   * @param year
   */
   public Ship(String name, int year) {
       this.name = name;
       this.year = year;
   }
   /**
   * @return the name
   */
   public String getName() {
       return name;
   }
   /**
   * @return the year
   */
   public int getYear() {
       return year;
   }
   /**
   * @param name the name to set
   */
   public void setName(String name) {
       this.name = name;
   }
   /**
   * @param year the year to set
   */
   public void setYear(int year) {
       this.year = year;
   }
  
   @Override
   public String toString() {
       return "Name: "+name+", Year: "+year;
   }

}


####################################

public class CruiseShip extends Ship {

   private int passengers;
  
   public CruiseShip(String name, int year, int passengers) {
       super(name, year);
       this.passengers = passengers;
   }

   /**
   * @return the passengers
   */
   public int getPassengers() {
       return passengers;
   }

   /**
   * @param passengers the passengers to set
   */
   public void setPassengers(int passengers) {
       this.passengers = passengers;
   }
  
   @Override
   public String toString() {
       return "Name: "+getName()+", Number of Passengers: "+passengers;
   }
  
}


#########################################

public class CargoShip extends Ship{

   private int capacity;

   /**
   * @param name
   * @param year
   * @param capacity
   */
   public CargoShip(String name, int 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;
   }
  
   @Override
   public String toString() {
       return "Name: "+getName()+", Capacity: "+capacity;
   }
}

####################################

public class Hw6_q2_code {
  
   public static void main(String[] args) {
      
       Ship ships[] = {
               new Ship("Acds", 2009),
               new CruiseShip("Erty", 2003, 34),
               new CargoShip("Loiu", 2016, 45)
       };
      
       for(Ship ship: ships){
           System.out.println(ship.toString());
       }
   }  
}

###################

Output:

Name: Acds, Year: 2009
Name: Erty, Number of Passengers: 34
Name: Loiu, Capacity: 45

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote