I'm new to Java and have been taking a course for a couple of weeks now and have been asked to complete a program with the following information:
Design a ship class that has the following data fields:
A data field for the name of the ship (a string).
A data field for the year that the ship was built (an int).
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 sub class that extends the Ship class. The CruiseShip class should have the following:
An extra data 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 also include the max passenger limit.
Design a CargoShip class that extends the Ship class. The CargoShip class should have the following:
An extra data 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 also include the cargo tonnage.
In the appropriate class include an equals method to test if two ships are equal - they should be considered equal if they have the same name and were built in the same year.
Demonstrate the classes in a program (ShipTester) that has an array of Ship objects (at least 5 of them). Assign various Ship, CruiseShip, and CargoShip objects to the array elements (you can hard-code the objects and data) and print out the initial ship configurations. Show that you can use both the accessor and mutator methods on a sampling of the ships (again, you can hard-code the method arguments here if you like).
I understand the basis of what the question is asking and I have done the following:
Ship.java
public class Ship {
private String name;
private int yearBuilt;
public Ship(String name, int yearBuilt) {
this.name = name;
this.yearBuilt = yearBuilt;
}
public String returnName() {
return this.name;
}
public int returnYear() {
return this.yearBuilt;
}
public boolean equals(Ship other) {
return false;
}
public String toString() {
return "[Name: " + this.name + ". Year built: " + this.yearBuilt + "]";
}
}
CruiseShip.java
public class CruiseShip extends Ship {
private int maxPassengers;
public CruiseShip() {
super("USS Enterprise", 2245);
this.maxPassengers = 2400;
}
public CruiseShip(String name, int yearBuilt, int maxPassengers) {
super(name, yearBuilt);
this.maxPassengers = maxPassengers;
}
public String toString() {
return "Cruise Ship" + super.toString() + ", Passengers: " + this.maxPassengers + "]";
}
}
CargoShip.java
public class CargoShip extends Ship {
private int cargoCapacity;
public CargoShip() {
super("Black Pearl", 1699);
this.cargoCapacity = 50000;
}
public CargoShip(String name, int yearBuilt, int cargoCapacity) {
super(name, yearBuilt);
this.cargoCapacity = cargoCapacity;
}
public String toString() {
return "Cargo Ship" + super.toString() + ", Tonnage: " + this.cargoCapacity + "]";
}
}
and in my tester file - ShipTester.java
public class ShipTester {
public static void main(String []args) {
//I don't know what to put over here...
}
}
I don't know what to put in the main method... I do know I have to create an array of ships but i don't know how to do that with the cargo ship, cruise ship and so on...
Output that i'm supposed to have:
Displaying Ships: Ship[ Name: Queen Annes Revenge Year built: 1701]
Cruise Ship[ Ship[ Name: USS Enterprise Year built: 2245], Passengers: 2400 ]
Cargo Ship[ Ship[ Name: Black Pearl Year built: 1699], Tonnage: 50000 ]
Cruise Ship[ Ship[ Name: USS Voyager Year built: 2371], Passengers: 2800 ]
Cargo Ship[ Ship[ Name: The Victory Year built: 1790], Tonnage: 33100 ]