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

Java: Programming Challenge (Netbeans JDK) Please provide instructions on how ti

ID: 3767058 • Letter: J

Question

Java: Programming Challenge (Netbeans JDK)

Please provide instructions on how ti compile multiple .java files (public classes) into one program.

Ship, CruiseShip, and CargoShip Classes

10. 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 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 Class:

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 += (" BuiltDate: " + builtDate);

return str;

}

}

CruiseShip Class:

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() +

" Ship's Name: " + n +

" Maximum Passengers: " + maxPass;

return str;

}

}

CargoShip class:

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() +

" Ship's Name: " + n +

" Cargo Capacity; " + cargoCap;

return str;

}

}

ShipDemo class:

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

}

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote