In: Computer Science
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.
I need user input aka asking the user to input a ship name, the number of passengers, and the year.
Hi,
Please find the answer below:
-------------------------------
Java Classes :
Ship.java
public class Ship {
protected String shipName;
protected String year;
// Ship Constructor
public Ship(String ship, String year) {
this.shipName = ship;
this.year = year;
}
// Ship get methods
public String getShipName() {
return shipName;
}
public String getYear(){
return year;
}
// Override toString method
public String toString(){
return "Ship Name : " +
getShipName() +
"\nBuilt Year : " + getYear();
}
}
////////////////////
CruiseShip.java
public class CruiseShip extends Ship {
private int maxPassengers;
//CruiseShip Constructor
public CruiseShip(String ship,String year, int
maxPassengers){
super(ship, year);
this.maxPassengers =
maxPassengers;
}
//get method
public int getMaxPassengers(){
return maxPassengers;
}
//overridden toString
public String toString(){
return "CruiseShip Name : " +
getShipName() +
"\nMaximum Number of Passengers : " +
getMaxPassengers();
}
}
//////////////////
CargoShip.java
public class CargoShip extends Ship {
private int cargoCapacityTons;
//CargoShip Constructor
public CargoShip(String name, String year, int tons) {
super(name, year);
this.cargoCapacityTons = tons;
}
//cargo get method
public int getCargoCapacityTons() {
return cargoCapacityTons;
}
//Overridden toString method
public String toString() {
return "CargoShip Name : " + getShipName() +
"\nShip Cargo
Capacity : " + getCargoCapacityTons();
}
}
////////////////////
ShipMain.java
import java.util.Scanner;
public class ShipMain {
public static void main(String[] args) {
String name;
int maxNoPass;
String year;
try {
//Take Input
from user
Scanner console
= new Scanner(System.in);
System.out.println("Enter the name of the ship: ");
name=console.nextLine();
System.out.println("Enter the number of passengers: ");
maxNoPass=Integer.parseInt(console.nextLine());
System.out.println("Enter the year the ship was built: ");
year=console.nextLine();
//Build a ship
object
CruiseShip
cruiseShipObj= new CruiseShip(name,year,maxNoPass);
System.out.println(cruiseShipObj.toString());
}catch(Exception e) {
e.printStackTrace();
}
}
}
///////////////////////////////
Sample Program Output:
Enter the name of the ship:
Pacific Liner
Enter the number of passengers:
1000
Enter the year the ship was built:
1999
CruiseShip Name : Pacific Liner
Maximum Number of Passengers : 1000
Screenshot:
-------------------------------
Hope this helps.
Let me know if you need more help with this.
Kindly, like the solution if you find it useful
Thanks.