Question: Programming Part: Design a Ship class that contains the following members: A field for the name of the ship (a string) A field for the
Programming Part:
Design a Ship class that contains 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 no-argument constructor that assigns default values to name (No Name) and year (1776).
An overloaded constructor that accepts name and year
Appropriate accessors and mutators
A toString method that displays the ships 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 to hold the number of passengers (int) with default value of 1000.
A no-argument constructor.
An overloaded constructor that accepts name and number of passengers.
An overloaded constructor that accepts name, year, and number of passengers
Appropriate accessors and mutators
A toString method that overrides the toString method in the base class. The CruiseShip classs toString method should return all Cruise ship information.
Design a CargoShip class that extends the Ship class. The CargoShip class should have the following members:
A field to hold the ship tonnage (int) with default value of 10000.
A no-argument constructor.
An overloaded constructor that accepts name and weight.
An overloaded constructor that accepts name, year, and weight
Appropriate accessors and mutators
A toString method that overrides the toString method in the base class. The CargoShip classs toString method should return all CargoShip information.
Demonstrate the classes in a program that has a Ship array. Complete the class MyShip4.
Implement the Comparable interface to be able to sort the objects by the year.
Use the instanceof to compute the total tonnage and total number of passengers.
Sample output
Basic Ship
Ship Name Basic
Ship Year 1955
Cargo Ship
Cargo Name No Name
Cargo Year 1776
Cargo Capacity 10000
Cargo Ship
Cargo Name Panama
Cargo Year 1776
Cargo Capacity 250
Cargo Ship
Cargo Name Atlanta
Cargo Year 1996
Cargo Capacity 3700
Basic Ship
Ship Name No Name
Ship Year 1776
Cruise Ship
Cruise Name Dream
Ship Year 1776
7).
Basic Ship
Ship Name George Washington
Ship Year 1732
Sorted by year built
Basic Ship
Ship Name George Washington
Ship Year 1732
Cargo Ship
Cargo Name No Name
Cargo Year 1776
Cargo Capacity 10000
Cargo Ship
Cargo Name Panama
Cargo Year 1776
Cargo Capacity 250
Basic Ship
Ship Name No Name
Ship Year 1776
Cruise Ship
Cruise Name Dream
Ship Year 1776
Basic Ship
Ship Name Basic
Ship Year 1955
Cargo Ship
Cargo Name Atlanta
Cargo Year 1996
Cargo Capacity 3700
Total Tonnage for Cargo Ships 13950
Total Number of Passengers 2000
and tester be like
import java.util.Collections;
import java.util.ArrayList;
public class MyShip4{
public static void main(String[] args){
//Creates an ArrayList to hold all of the ships
ArrayList
//Populate ArrayList with a number of ship types
ships.add(new Ship("Basic", 1955));
ships.add(new CargoShip());
ships.add(new CruiseShip());
ships.add(new CargoShip("Panama", 250));
ships.add(new CargoShip("Valdosta", 2001, 500));
ships.add(new CargoShip("Atlanta", 1996, 3700));
ships.add(new CruiseShip("Paradise", 3000));
ships.add(new Ship());
ships.add(new CargoShip("Rio", 250));
ships.add(new CruiseShip("Dream", 2000));
ships.add(new CruiseShip("Future", 4500));
ships.add(new CargoShip("Cuba", 432));
ships.add(new Ship("George Washington", 1732));
//1. Traverse the arrayList and display all ship informtion.
//2. Sort all ships by year built on assending order
//3. Traverse the arrayList and display all ship informtion.
//4. Compute the total number of passengers in all cruise ships
//5. Compute the total tonnage of al Cargo ships
//6. Display both total number of passengers and total tonnage
// You should get the following totals:
// Total Tonnage for Cargo Ships 15132
// Total Number of Passengers 10500
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
