Question: This will be a variation of the program described on the last paragraph on Page 679. Starting out with Java early objects 5th edition Write
This will be a variation of the program described on the last paragraph on Page 679. Starting out with Java early objects 5th edition
Write a new main program that will create an array of 5 Ships. You will implement Ship, CargoShip, and CruiseShip. You will also implement a driver for an array of ships.
Instantiate each of the 5 elements of the array with some of them being Ship, some being CargoShip, and some being CruiseShip. Create the classes in three separate files: Ship.java, CargoShip.java, and CruiseShip.java
Use a loop to print all of the elements in the array.
Sample Output
0 Cruise Ship: SS Minnow - # Passengers: 7
1 Cruise Ship: Titanic - # Passengers: 3327
2 Cargo Ship: Maersk Alabama - tonnage: 14120
3 Cruise Ship: Costa Concordia - # Passengers: 3780
4 Ship: Black Pearl - Built: 1725
Press any key to continue . . .
This is the start of the three classes needed to create a main program.
public class TestShip { public static void main(String[] args) { // create myShip object Ship myShip = new Ship("Maersk Alabama","1984");
// display myShip (implicitly calls toString() System.out.println(myShip);
// change all values myShip.setShipName("Titanic"); myShip.setYearBuilt("1911");
// display using getters System.out.println(" S: " + myShip.getShipName()); System.out.println("Y: " + myShip.getYearBuilt()); } } /* Output should be: Ship: Maersk Alabama - Built: 1984
S: Titanic Y: 1911 Press any key to continue . . . */
public class TestCruiseShip { public static void main(String[] args) { // create myShip object CruiseShip myShip = new CruiseShip("SS Minnow","1964",7);
// display myShip (implicitly calls toString() System.out.println(myShip);
// change all values myShip.setShipName("Titanic"); myShip.setYearBuilt("1911"); myShip.setNumPassengers(3327);
// display using getters System.out.println(" S: " + myShip.getShipName()); System.out.println("Y: " + myShip.getYearBuilt()); System.out.println("P: " + myShip.getNumPassengers()); } } /* Output should be: Cruise Ship: SS Minnow - # Passengers: 7
S: Titanic Y: 1911 P: 3327 Press any key to continue . . . */
public class TestCargoShip { public static void main(String[] args) { // create myShip object CargoShip myShip = new CargoShip("maersk","1998",1200);
// display myShip (implicitly calls toString() System.out.println(myShip);
// change all values myShip.setShipName("hauler"); myShip.setYearBuilt("1999"); myShip.setCargoCapacity(4567);
// display using getters System.out.println(" S: " + myShip.getShipName()); System.out.println("Y: " + myShip.getYearBuilt()); System.out.println("C: " + myShip.getCargoCapacity()); } } /* Output should be: Cargo Ship: maersk - tonnage: 1200
S: hauler Y: 1999 C: 4567 Press any key to continue . . . */
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
