I am doing a homework problem and am having a hard time understanding exactly how I should be doing this. Any nudge in the right direction would be helpful. :)
The problem is as follows:
Ship, CruiseShip, and CargoShip Classes 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 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 Shiparray. 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.
This is what I have for the Ship Class so far:
public class Ship {
private String name;
private String builtDate;
public Ship(String n, String date)
{
name = n;
builtDate = date;
}
public Ship()
{
name = "";
builtDate = "";
}
public void setName(String n)
{
name = n;
}
public void setBuiltDate(String b)
{
builtDate = b;
}
public String getName()
{
return name;
}
public String getBuiltDate()
{
return builtDate;
}
public String toString()
{
String str = "Name: " + name;
str += ("\nBuiltDate: " + builtDate);
return str;
}
}
Here is what I have for the CruiseShip Class so far:
public class CruiseShip extends Ship
{
private int maxPass;
public CruiseShip(String n, String b)
{
super(n, b);
}
public void setMaxPass(int mp)
{
maxPass = mp;
}
public int getMaxPass()
{
return maxPass;
}
public String toString()
{
String str;
str = super.toString() +
"\nShip's Name: " + n +
"\nMaximum Passengers: " + maxPass;
return str;
}
}
Here is what I have for the CargoShip class so far:
public class CargoShip extends Ship
{
private int cargoCap;
public CargoShip(String n, String b)
{
super(n, b);
}
public void setCargoCap(int cc)
{
cargoCap = cc;
}
public int getCargoCap()
{
return cargoCap;
}
public String toString()
{
String str;
str = super.toString() +
"\nShip's Name: " + n +
"\nCargo Capacity; " + cargoCap;
return str;
}
}
and here is my ShipDemo class so far:
public class ShipDemo
{
public static void main(String[] args)
{
//Create an array of Ship References.
Ship[] types = new Ship[6];
types[0] = new Ship();
types[0].setName("Carnival");
((CruiseShip) types[0]).setMaxPass(500);
types[1] = new Ship();
types[1].setName("Royal Caribbean");
((CruiseShip) types[1]).setMaxPass(1000);
types[2] = new Ship();
types[2].setName("Sony");
((CargoShip) types[2]).setCargoCap(10000);
types[3] = new Ship();
types[3].setName("Playskool");
((CargoShip) types[2]).setCargoCap(25000);
for (int i=0; i < types.length; i++)
{
System.out.println("Ship " + (i + 1) + ": ");
//Need something here to call the toString methods
}
}
}
I could use your feedback on how I have done so far, if I have made errors any where, and what my next step toward completing this may be. Thanks for looking!