JAVA Ship , CruiseShip , and CargoShip Classes Design a Ship class that the foll
ID: 3587976 • Letter: J
Question
JAVA
Ship , CruiseShip , and CargoShip Classes
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
Programming Challenges 701
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.
Demonstrate the classes in a program that has a Ship array. Assign various Ship , CruiseShip ,
and CargoShip objects to the array elements. The program should then step through the
array, calling each object’s toString method. (See Code Listing 9-25 as an example.)
Explanation / Answer
public class Ship {
private String name;
private String year;
public Ship(){
this.name = "";
this.year = "";
}
public Ship(String n, String y) {
this.name = n;
this.year = y;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
public String toString() {
return "Ship Name "+ this.getName() +""
+ "Year of built "+this.getYear();
}
}
public class CargoShip extends Ship {
private int tonnage;
public int getTonnage() {
return tonnage;
}
public void setTonnage(int tonnage) {
this.tonnage = tonnage;
}
public CargoShip() {
super();
}
public CargoShip(String n, String y, int tonage) {
super(n, y);
this.tonnage = tonage;
}
public String toString() {
return "Ship's name "+ this.getName()+""
+ "and its capacity is "+this.getTonnage();
}
}
public class CruiseShip extends Ship{
int maxPassengers;
public CruiseShip() {
super();
}
public CruiseShip(String n, String t, int maxPerons) {
super(n, t);
this.maxPassengers = maxPerons;
}
public int getMaxPassengers() {
return maxPassengers;
}
public void setMaxPassengers(int maxPassengers) {
this.maxPassengers = maxPassengers;
}
public String toString() {
return "Ship's name "+this.getName()+""
+ "has maximum capacity of "+this.getMaxPassengers();
}
}
main class to test the code
public class TestShip {
public static void main(String args[]) {
Ship[] shipsArray = new Ship[3];
shipsArray[0] = new Ship("Agusta", "1930");
shipsArray[1] = new CruiseShip("Agusta", "1930", 13000);
shipsArray[2] = new CargoShip("Agusta", "1930", 145678);
for(int i=0;i<shipsArray.length;i++) {
System.out.println(shipsArray[i].toString());
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.