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

//Create a New Java Project called YourLastNameShip . //Design a Ship class that

ID: 3814497 • Letter: #

Question

//Create a New Java Project called YourLastNameShip.

//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 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 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, calling each object's toString method. (See Code Listing 10-25 as an example.)

Explanation / Answer

Ship.java

public class Ship {

//Declaring variables
private String name;
private int built_year;

//Parameterized constructor
public Ship(String name, int built_year) {
   this.name = name;
   this.built_year = built_year;
  
}
//Setters and Getters.
public String getName() {
   return name;
}
public void setName(String name) {
   this.name = name;
}
public int getBuilt_year() {
   return built_year;
}
public void setBuilt_year(int built_year) {
   this.built_year = built_year;
}

//toString() method which displays the contents of the object inside it
@Override
public String toString() {
   return " Ship # Name=" + name + ", Build Year=" + built_year;
}

}

_________________

CruiseShip.java

public class CruiseShip extends Ship {
  
   //Declaring variables
   private int capacity;
  
   //Parameterized constructor
   public CruiseShip(String name, int built_year,int capacity) {
       super(name, built_year);
       this.capacity=capacity;
   }
  
   //Setters and Getters.
   public int getCapacity() {
       return capacity;
   }
   public void setCapacity(int capacity) {
       this.capacity = capacity;
   }
  
   //toString() method which displays the contents of the object inside it
   @Override
   public String toString() {
       return " This is CruiseShip# Name="+super.getName()+" Capacity=" + capacity;
   }

  
}

___________________

CargoShip.java

public class CargoShip extends Ship {
  
   //Declaring variables
   private int capacity_tonnage;
  
   //Parameterized constructor
   public CargoShip(String name, int built_year,int capacity) {
       super(name, built_year);
       this.capacity_tonnage=capacity;
      
   }
  
   //Setters and Getters.
   public int getCapacity_tonnage() {
       return capacity_tonnage;
   }
   public void setCapacity_tonnage(int capacity_tonnage) {
       this.capacity_tonnage = capacity_tonnage;
   }
  
   //toString() method which displays the contents of the object inside it
   @Override
   public String toString() {
      
       return " This is CargoShip# "+super.getName()+" Capacity Tonnage=" + capacity_tonnage;
   }
  

}

__________________

Test.java

public class Test {

   public static void main(String[] args) {
      
       //Creating an array of 10 ship objects and storing it into an array.
       Ship ship[]={new CruiseShip("Caribbean Princess", 2004,3245),
               new CruiseShip("Carnival Breeze",2012,3000),
               new CruiseShip("Carnival Conquest",2002,2974 ),
               new CargoShip("MV Delight", 1985, 25768),
               new CargoShip("SS Desabla", 1913,6047),
               new CargoShip("MV Gadila",1934,7999),
               new CargoShip("TSS Golfito", 1949,8687),
               new Ship("Brig",2006),
               new Ship("HMAV Bounty",2009),
               new Ship("HMS Victory",2010)} ;
  
   //Displaying the Ship objects  
   for(Ship s:ship)
   {
       System.out.println(s.toString());
   }

   }

}

________________

Output:


This is CruiseShip#
Name=Caribbean Princess
Capacity=3245

This is CruiseShip#
Name=Carnival Breeze
Capacity=3000

This is CruiseShip#
Name=Carnival Conquest
Capacity=2974

This is CargoShip#
MV Delight
Capacity Tonnage=25768

This is CargoShip#
SS Desabla
Capacity Tonnage=6047

This is CargoShip#
MV Gadila
Capacity Tonnage=7999

This is CargoShip#
TSS Golfito
Capacity Tonnage=8687

Ship #
Name=Brig,
Build Year=2006

Ship #
Name=HMAV Bounty,
Build Year=2009

Ship #
Name=HMS Victory,
Build Year=2010

_________Thank You