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

/Create a New Java Project . //Design a Ship class that has the following member

ID: 3819318 • Letter: #

Question

/Create a New Java Project

. //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.

Explanation / Answer

/*Ship.java*/

public class Ship {

String ship;

int year;

//construtor for name and year

public Ship(String ship,int year)

{

this.ship=ship;

this.year=year;

}

//get methods

public String getShipName()

{

return ship;

}

public int getYear()

{

return year;

}

//Override toString method

public String toString()

{

return "Ship Name :"+getShipName()+

" Built Year :"+getYear();

}

}


/*CruiseShip.java*/

public class CruiseShip extends Ship{

int maxPassengers;

//construtor for name and number of passengers

public CruiseShip(String ship,

int year,int maxPassengers)

{

super(ship,year);

this.maxPassengers=maxPassengers;

}

//return maximum passengers

public int getMaxPassengers()

{

return maxPassengers;

}

//Overiride the toString

public String toString()

{

return " Ship Name :"+getShipName()+

" Maximum Number of passengers :"+

getMaxPassengers();

}

}

/*CargoShip.java*/

public class CargoShip extends Ship{

int noOfTonns;

// construtor for name and year ,number of tonns

public CargoShip(String name,int year,int noOfTonns)

{

super(name,year);

this.noOfTonns=noOfTonns;

}

//return number of tonns

public int getNoOfTonns()

{

return noOfTonns;

}

public String toString()

{

return " Ship name :"+getShipName()+

" Ship Capacity :"+getNoOfTonns();

}

}

/*ShipDemo*/

public class ShipDemo {

public static void main(String[] args) {

//create an array of type ship of size

Ship[] ships=new Ship[3];

//initialize constructors

ships[0]=new Ship("Sea-Hawk",2002);

ships[1]=new CruiseShip("Titanic",2002,1336);

ships[2]=new CargoShip("Sharp-Teeth",2002,100);

//print toString methods

for(int i=0;i<ships.length;i++)

System.out.println(ships[i].toString());

}

}



Compile and run the program:

                     javac ShipDemo.java

                     java ShipDemo

Output:

Ship Name :Sea-Hawk

Built Year :2002

Ship Name :Titanic

Maximum Number of passengers :1336

Ship name :Sharp-Teeth

Ship Capacity :100