Write a JavaCode:- Desgin a Ship class that has the following members: A field f
ID: 3910624 • Letter: W
Question
Write a JavaCode:-
Desgin a Ship class that has the following members:
A field for the name of the ship (a string).
A field for the year that the ship was build (an int).
A constructor and appropriate accessors and mutators.
A toString method that displays the ship's name and the year it was built.
An equals method to compare the data members of Ships.
A canTravel method that always returns true.
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 field for the current number of passengers.
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 on the ship's name and the maximum number of passengers.
An equals method to compare the data members of CruiseShips.
A canTravel method that returns true if at least 80% of the passenger spots have been filled.
Designe a CargoShip class that extends the Ship class. The CargoShip class should have the following members:
A field for the cargo capacity in tonnage (a double).
A field for the current amount of cargo in tonnage.
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.
An equals method to compare the data members of CargoShips.
A canTravel method that returns true if at least 90% of the cargo capacity has been filled.
Part II:
Demonstrate the classes in a program that has an array of Ships. Assign various Ship, CruiseShip, and CargoShip objects to the array elements. The program should step through the array and display the information about each array element, calling each object's toString method. The program should then calculate and print the number of ships that are ready to travel by calling the canTravel method for each object in the array.
To think about: If a reference is of type Ship, it can contain a either a CruiseShip, Ship, or CargoShip object. When a method is in more than one class of the inheritance chain, does the system call the method in the reference’s class or the one in the object’s class?
Explanation / Answer
class Ship // base class
{
private String name;
private int yearBuilt;
public Ship(String name,int yearBuilt)//constructor
{
this.name = name;
this.yearBuilt = yearBuilt;
}
//set and get functions
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
public void setYearBuilt(int yearBuilt)
{
this.yearBuilt = yearBuilt;
}
public int getYearBuilt()
{
return yearBuilt;
}
public String toString()
{
return " Name of ship : "+name+" year built : "+yearBuilt;
}
public boolean equals(Ship s)
{
if(this.name == s.name && this.yearBuilt == s.yearBuilt)
return true;
else
return false;
}
public boolean canTravel()
{
return true;
}
}
class CruiseShip extends Ship // derived class
{
private int maxPassengers;
private int currentPassengers;
public CruiseShip(String name,int yearBuilt,int maxPassengers,int currentPassengers)
//passing arguments to base class constructor
{
super(name,yearBuilt);
this.maxPassengers = maxPassengers;
this.currentPassengers = currentPassengers;
}
public void setMaxPassengers(int maxPassengers)
{
this.maxPassengers = maxPassengers;
}
public int getMaxPassengers()
{
return maxPassengers;
}
public String toString()
{
return " Cruise Name : "+getName()+" Maximum Passengers in cruise : "+maxPassengers+" Current Passengers : "+currentPassengers;
}
public boolean equals(CruiseShip s)
{
if(this.getName() == s.getName() && this.maxPassengers == s.maxPassengers && this.currentPassengers == s.currentPassengers)
return true;
else
return false;
}
public boolean canTravel()
{
if((double)currentPassengers/maxPassengers >= 0.8)
return true;
else
return false;
}
}
class CargoShip extends Ship
{
private int capacity;
private int currentAmount;
public CargoShip(String name,int yearBuilt,int capacity,int currentAmount)
//passing arguments to base class constructor
{
super(name,yearBuilt);
this.capacity = capacity;
this.currentAmount = currentAmount;
}
public void setCapacity(int capacity)
{
this.capacity = capacity;
}
public int getCapacity()
{
return capacity;
}
public String toString()
{
return " Cargo Name : "+getName()+" Cargo Capacity : "+capacity +" Current Amount in Tonnage : "+currentAmount;
}
public boolean equals(CargoShip s)
{
if(this.getName() == s.getName() && this.capacity == s.capacity && this.currentAmount == s.currentAmount)
return true;
else
return false;
}
public boolean canTravel()
{
if((double)capacity/ currentAmount >= 0.9)
return true;
else
return false;
}
}
class Test
{
public static void main (String[] args)
{
Ship[] ships = new Ship[3];
ships[0] = new CruiseShip("HMCS Columbia",1985,300,250);
ships[1] = new CargoShip("ThunderBay",1956,200,170);
ships[2] = new Ship("liberty Ship",1981);
for(int i = 0;i<3;i++)
{
System.out.println(ships[i].toString());
System.out.println("Can Travel : "+ships[i].canTravel());
}
}
}
Output:
Cruise Name : HMCS Columbia Maximum Passengers in cruise : 300 Current Passengers : 250
Can Travel : true
Cargo Name : ThunderBay Cargo Capacity : 200 Current Amount in Tonnage : 170
Can Travel : true
Name of ship : liberty Ship year built : 1981
Can Travel : true
Do ask if any doubt. Please upvote.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.