Design a Ship class that the following members: * A field for the name of the sh
ID: 3555733 • Letter: D
Question
Design a Ship class that the following members:
* A field for the name of the ship (a string).
* A field for the year the 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.
Posted what I have below. Need help with an array list in the ShipDemo calling each object's toString method. and just a general overview of making sure everything is in there that is needed or any edits that could make it better.
____________________________________________
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();
}
}
_________________
public class CargoShip extends Ship {
int noOfTonns;
public CargoShip(String name, int year, int noOfTonns) {
super(name, year);
this.noOfTonns = noOfTonns;
}
public int getNoOfTonns() {
return noOfTonns;
}
public String toString() {
return " Ship name :" + getShipName() +
" Ship Capacity :" + getNoOfTonns();
}
}
________________________
public class CruiseShip extends Ship {
int maxPassengers;
public CruiseShip(String ship,
int year, int maxPassengers)
{
super(ship, year);
this.maxPassengers = maxPassengers;
}
public int getMaxPassengers()
{
return maxPassengers;
}
public String toString()
{
return " Ship Name :" + getShipName() +
" Maximum Number of passengers :" +
getMaxPassengers();
}
___________________________________________
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("Britannic", 2002);
ships[1] = new CruiseShip("Admiral", 1978, 900);
ships[2] = new CargoShip("Calypso", 2000, 400);
// print toString methods
for (int i = 0; i < ships.length; i++)
System.out.println(ships[i].toString());
}
}
Explanation / Answer
everything is perfect....neatly coded
you can do also:
ArrayList<Ship> list = new ArrayList<Ship>();
Ship ship1 = new CargoShip();
Ship ship2 = new CruiseShip();
lis.add(ship1);
list.add(ship2);
as CruiseShip, CargoShip are subclasses of ship, you can do it.