I\'ve got the Ship, CargoShip, and CruiseShip classes but can\'t figure out the
ID: 3692787 • Letter: I
Question
I've got the Ship, CargoShip, and CruiseShip classes but can't figure out the part in bold. Thanks in advance for the help.
Create a Ship class that has the following members:
- A field for the name of the ship ( name – String data type)
- A field for the year that the shop was built ( year – String data type)
- A constructor and appropriate gets (accessors) and sets (mutators)
- A method called toString that displays the ship ’ s name and the year it was built
Create a CruiseShip class that extends the Ship class. The CruiseShip class should have the following members:
- A field for the maximum number of passengers ( passe ngers – Integer data type)
- A constructor and appropriate gets (accessors) and sets (mutators)
- A 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 the ship can carry.
Create a CargoShip class that extends the Ship class. The CargoShip class should have the following members:
- A field for the cargo capacity in tons ( tons – Integer data type)
- A constructor and appropriate gets (accessors) and sets (mutators)
- A method that overrides the toString method in the base class. The Cargo Ship class’s toString method should display only the ship ’ s name and the maximum number of ton s the ship can carry.
Create a ShipDemo program that asks the user to input ship data. The program should display a menu that allow s the user to enter ships until a - 1 is entered. The program should have a static method called menu that displays the menu. After the user enters a - 1, the program should display the ship informati on that was entered by the user using the toString methods created for each type of ship.
********Menu*********
Enter 1 for a Ship
Enter 2 for a Cruise Ship
Enter 3 for a Cargo Ship
Enter - 1 to exit
Please enter your choice:
My Code:
Ship Class
public class Ship {
String Ship;
int year;
public Ship(String ship, int year)
{
this.Ship=ship;
this.year=year;
}
public String getShipName()
{
return Ship;
}
public int getYear()
{
return year;
}
public String toString()
{
return "Ship Name:"+getShipName()+" Built Year:"+getYear();
}
}
CruiseShip Class
public class CruiseShip extends Ship{
int maxPassengers;
public CruiseShip(String ship, int year, int maxPassengers){
super(ship, year);
this.maxPassengers=maxPassengers;
}
public int getMaxPassengers()
{
return maxPassengers;
}
public String toString()
{
return " Ship Name:"+getShipName()+" Maximum Number of Passengers:"+getMaxPassengers();
}
}
CargoShip Class
public class CargoShip extends Ship{
int noTons;
public CargoShip(String name, int year, int noTons){
super(name, year);
this.noTons=noTons;
}
public int getnoTons(){
return noTons;
}
public String toString(){
return" Ship Name:"+getShipName()+" Ship Capacity:"+getnoTons();
}
}
Explanation / Answer
Ship Class
public class Ship {
String Ship;
int year;
public static void main(String[] args){
int i;
System.out.println("Enter choice: 1 for CargoShip, 2 for CruiseShip, -1 to exit ");
Scanner in = new Scanner(System.in);
i = in.nextInt();
if (i=1||i=2)
{
Ship s=new Ship();
CruiseShip cr = new CruiseShip();
CargoShip cs= new CargoShip();
cr.CruiseShip();
cs.CargoShip();
public Ship(String ship, int year)
{
this.Ship=ship;
this.year=year;
}
public String getShipName()
{
return Ship;
}
public int getYear()
{
return year;
}
public String toString()
{
return "Ship Name:"+getShipName()+" Built Year:"+getYear();
}
}
else return 0;
}
}
CruiseShip Class
public class CruiseShip extends Ship{
int maxPassengers;
public CruiseShip(String ship, int year, int maxPassengers){
super(ship, year);
this.maxPassengers=maxPassengers;
}
public int getMaxPassengers()
{
return maxPassengers;
}
public String toString()
{
return " Ship Name:"+getShipName()+" Maximum Number of Passengers:"+getMaxPassengers();
}
}
CargoShip Class
public class CargoShip extends Ship{
int noTons;
public CargoShip(String name, int year, int noTons){
super(name, year);
this.noTons=noTons;
}
public int getnoTons(){
return noTons;
}
public String toString(){
return" Ship Name:"+getShipName()+" Ship Capacity:"+getnoTons();
}
}
note-the above code can help to answer.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.