Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1.0 Design a Ship class that has the following members: 1.1 A field for the name

ID: 3591025 • Letter: 1

Question

1.0 Design a Ship class that has the following members:

1.1 A field for the name of a ship (a string)

1.2 A field for the year that ship was build (a string)

1.3 A constructor and appropriate accessors and mutators

1.4 A toString method that displays the ship's name and the year it was built

2.0 Design a CruiseShip class that extends the Ship class. The CruiseShip class should have the following members:

2.1 A field for the maximum number of passengers (an int)

2.2 A constructor and appropriate accessors and mutators

2.3 A toString method that overrides thetoString method in the same base class.

2.3-i The CruiseShip class's toString method should display only the ship's name and the maximum number of

passengers.

3.0 Design a CargoShip class that extends the Ship class. The CargoShip class should have the followin gmembers:

3.1 A field for the cargo capacity in tonnage (an int)

3.2 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

Shipdemo Java

Explanation / Answer

Ship .java

public class Ship {

private String name, year;

public Ship(String name, String year) {

super();

this.name = name;

this.year = year;

}

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;

}

@Override

public String toString() {

return "Ship [name=" + name + ", year=" + year + "]";

}

}

public class CruiseShip extends Ship{

private int numberOfPassengers;

public CruiseShip(String name, String year , int n) {

super(name, year);

numberOfPassengers = n;

}

@Override

public String toString() {

return "CruiseShip [name=" + super.getName() + "] [numberOfPassengers=" + numberOfPassengers + "]";

}

public int getNumberOfPassengers() {

return numberOfPassengers;

}

public void setNumberOfPassengers(int numberOfPassengers) {

this.numberOfPassengers = numberOfPassengers;

}

}

public class CargoShip extends Ship{

private int capacity ;

public CargoShip(String name, String year , int c) {

super(name, year);

capacity = c;

}

public int getCapacity() {

return capacity;

}

public void setCapacity(int capacity) {

this.capacity = capacity;

}

@Override

public String toString() {

return "CargoShip [capacity=" + capacity + "]";

}

}

Shipdemo Java

Output::

Ship [name=Lolipop, year=1960]
----------------------------
CruiseShip [name=Disney Magic] [numberOfPassengers=2400]
----------------------------
CargoShip [capacity=50000]
----------------------------

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote