JAVA Exercise 2 Design a Ship CargoShip and CruiseShip class

[JAVA]

Exercise 2

Design a Ship, CargoShip and CruiseShip class being mindful of behavior of each. Demonstrate the classes in a program that has a Ship array. Assign various Ships, CruiseShip and CargoShip to the array elements.

CargoShip and CruiseShip are a child of Ship class.

Pl. submit a class diagram for each exercise, along with code and test runs.

Solution

shipdriver.java

public class ShipDriver

{

public static void main(String[] args)

{

Ship[] ships = new Ship[3];

ships[0]= new Ship(\"Destroyer\",1886);

ships[1] = new CruiseShip(\"Small\",\"Arethusa\",2007,50,26,18,0,1);

ships[2] = new CargoShip(\"Mærsk Mc-Kinney Møller\",2013,1306,190,174500);

System.out.println(\"SHIP LIST\ \");

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

{

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

}

}

}

Ship.java

public class Ship

{

String ship;

int yearBuilt;

public Ship(String ship, int yearBuilt)

{

this.ship = ship;

this.yearBuilt = yearBuilt;

}

public String toString(){

return \"SHIP\ Ship Name: \" + ship +\"\ Built: \" + yearBuilt;

}

}

Cruiseship.java

import java.text.DecimalFormat;

public class CruiseShip extends Ship

{

String category;

int passengers;

int cabins;

int cabinsWithBalconies;

int swimmingPools;

int restaurants;

CruiseShip(String category, String ship, int yearBuilt, int passengers, int cabins, int cabinsWithBalconies,

int swimmingPools, int restaurants)

{

super(ship, yearBuilt);

this.category = category;

this.passengers = passengers;

this.cabins = cabins;

this.cabinsWithBalconies = cabinsWithBalconies;

this.swimmingPools = swimmingPools;

this.restaurants = restaurants;

}

public String toString(){

DecimalFormat df = new DecimalFormat(\"#,###\");

return \"\ CRUISE SHIP\ Category: \" + category +\"\ Ship Name: \" + ship +\"\ Built: \" + yearBuilt + \"\ Passengers: \" + df.format(passengers) + \"\ Number of Cabins: \" + cabins +\"\ Number of Swimming Pools: \" + swimmingPools +\"\ Number of Restaurants: \" + restaurants;

}

}

CargoShip.java

public String toString(){

DecimalFormat df = new DecimalFormat(\"#,###\");

return \"\ CARGO SHIP\ Ship Name: \" + ship +\"\ Built: \" + yearBuilt +\"\ Length Overall (ft): \" + df.format(length) +\"\ Beam (ft): \" + beam +\"\ Gross Tonnage: \" + df.format(maxCapacity);

}

}

/** OUTPUT

SHIP LIST

SHIP

Ship Name: Destroyer

Built: 1886

CRUISE SHIP

Category: Small

Ship Name: Arethusa

Built: 2007

Passengers: 50

Number of Cabins: 26

Number of Swimming Pools: 0

Number of Restaurants: 1

CARGO SHIP

Ship Name: Mærsk Mc-Kinney Møller

Built: 2013

Length Overall (ft): 1,306

Beam (ft): 190

Gross Tonnage: 174,500

[JAVA] Exercise 2 Design a Ship, CargoShip and CruiseShip class being mindful of behavior of each. Demonstrate the classes in a program that has a Ship array. A
[JAVA] Exercise 2 Design a Ship, CargoShip and CruiseShip class being mindful of behavior of each. Demonstrate the classes in a program that has a Ship array. A
[JAVA] Exercise 2 Design a Ship, CargoShip and CruiseShip class being mindful of behavior of each. Demonstrate the classes in a program that has a Ship array. A

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site