Respuesta :
Answer:
See Explaination
Explanation:
// Ship.java
public class Ship {
//field to store the name of the ship
private String name;
// filed to store the built on date as string
private String builtOn;
// constructor for ship takes name and built on date as argument
public Ship(String name, String builtOn) {
this.name = name;
this.builtOn = builtOn;
}
// getter to get the name of the ship
public String getName() {
return name;
}
// setter to set the name of the ship
public void setName(String name) {
this.name = name;
}
// getter method to get the built on date of the ship
public String getBuiltOn() {
return builtOn;
}
// setter method to set the built on date of the ship
public void setBuiltOn(String builtOn) {
this.builtOn = builtOn;
}
// to string method to print the ship as as string
public String toString() {
return "Ship [name=" + name + ", builtOn=" + builtOn + "]";
}
}
// CruiseShip.java
public class CruiseShip extends Ship {
// field to store the max number of passenger in the ship
private int maxNumberOfPassenger;
// constructor for cruise ship takes name , builton date and max number of passenger as argument
public CruiseShip(String name, String builtOn, int maxNumberOfPassenger) {
super(name, builtOn);
this.maxNumberOfPassenger = maxNumberOfPassenger;
}
// getter method to get the maximum number of passenger in the ship
public int getMaxNumberOfPassenger() {
return maxNumberOfPassenger;
}
// setter mehtod to set the maximum number of passenger in the ship
public void setMaxNumberOfPassenger(int maxNumberOfPassenger) {
this.maxNumberOfPassenger = maxNumberOfPassenger;
}
// to string method to print the cruise ship as string
atOverride //replace the at with at symbol
public String toString() {
return (super.toString() + ", " + "CruiseShip [maxNumberOfPassenger=" + maxNumberOfPassenger + "]");
}
}
// CargoShip.java
public class CargoShip extends Ship{
// field to store the capacity of cargo ship
private int cargoCapacity;
// constructor for cargo ship takes name , builton date and capacity as argumnet
public CargoShip(String name, String builtOn, int cargoCapacity) {
super(name, builtOn);
this.cargoCapacity = cargoCapacity;
}
// getter method to get the capacity of cargo ship
public int getCargoCapacity() {
return cargoCapacity;
}
// setter method to set the capacity of cargo ship
public void setCargoCapacity(int cargoCapacity) {
this.cargoCapacity = cargoCapacity;
}
// to string mehtod to print the cargo ship object as string
atOverride // replace at with its symbol
public String toString() {
return (super.toString() + ", " + "CargoShip [cargoCapacity=" + cargoCapacity + " tonnage]");
}
}
// Test.java
import java.util.ArrayList;
import java.util.List;
public class Test {
// driver mehtod to test declared class
public static void main(String args[]){
// declare arraylist of ship to store the ships
List<Ship> ships = new ArrayList<Ship>();
// adding the ship into the arraylist
ships.add(new Ship("ShipA", "04/04/2018"));
ships.add(new Ship("ShipB","05/04/2018"));
ships.add(new CruiseShip("CruiseShipA", "03/04/2018", 14));
ships.add(new CruiseShip("CruiseShipB", "04/04/2018", 24));
ships.add(new CargoShip("CargoShipA", "02/04/2018", 40));
ships.add(new CargoShip("CargoShipB", "04/04/2018", 12));
// printing the ships by iterating over the array list of ships
for (int i = 0; i < ships.size(); i++){
System.out.println(ships.get(i));
}
}
In this exercise we have to use the knowledge of programming in the Java language, in this way we find the code as:
The code can be found in the figure attached below.
So to make it easier the code will be rewritten below as:
// Ship.java
public class Ship {
//field to store the name of the ship
private String name;
// filed to store the built on date as string
private String builtOn;
// constructor for ship takes name and built on date as argument
public Ship(String name, String builtOn) {
this.name = name;
this.builtOn = builtOn;
}
// getter to get the name of the ship
public String getName() {
return name;
}
// setter to set the name of the ship
public void setName(String name) {
this.name = name;
}
// getter method to get the built on date of the ship
public String getBuiltOn() {
return builtOn;
}
// setter method to set the built on date of the ship
public void setBuiltOn(String builtOn) {
this.builtOn = builtOn;
}
// to string method to print the ship as as string
public String toString() {
return "Ship [name=" + name + ", builtOn=" + builtOn + "]";
}
// CruiseShip.java
public class CruiseShip extends Ship {
// field to store the max number of passenger in the ship
private int maxNumberOfPassenger;
// constructor for cruise ship takes name , builton date and max number of passenger as argument
public CruiseShip(String name, String builtOn, int
axNumberOfPassenger) {
super(name, builtOn);
this.maxNumberOfPassenger = maxNumberOfPassenger;
}
// getter method to get the maximum number of passenger in the ship
public int getMaxNumberOfPassenger() {
return maxNumberOfPassenger;
}
// setter mehtod to set the maximum number of passenger in the ship
public void setMaxNumberOfPassenger(int maxNumberOfPassenger) {
this.maxNumberOfPassenger = maxNumberOfPassenger;
}
// to string method to print the cruise ship as string
atOverride //replace the at with at symbol
public String toString() {
return (super.toString() + ", " + "CruiseShip [maxNumberOfPassenger=" + maxNumberOfPassenger + "]");
}
// CargoShip.java
public class CargoShip extends Ship{
// field to store the capacity of cargo ship
private int cargoCapacity;
// constructor for cargo ship takes name , builton date and capacity as argumnet
public CargoShip(String name, String builtOn, int cargoCapacity) {
super(name, builtOn);
this.cargoCapacity = cargoCapacity;
}
// getter method to get the capacity of cargo ship
public int getCargoCapacity() {
return cargoCapacity;
}
// setter method to set the capacity of cargo ship
public void setCargoCapacity(int cargoCapacity) {
this.cargoCapacity = cargoCapacity;
}
// to string mehtod to print the cargo ship object as string
atOverride // replace at with its symbol
public String toString() {
return (super.toString() + ", " + "CargoShip [cargoCapacity=" + cargoCapacity + " tonnage]");
}
// Test.java
import java.util.ArrayList;
import java.util.List;
public class Test {
// driver mehtod to test declared class
public static void main(String args[]){
// declare arraylist of ship to store the ships
List<Ship> ships = new ArrayList<Ship>();
// adding the ship into the arraylist
ships.add(new Ship("ShipA", "04/04/2018"));
ships.add(new Ship("ShipB","05/04/2018"));
ships.add(new CruiseShip("CruiseShipA", "03/04/2018", 14));
ships.add(new CruiseShip("CruiseShipB", "04/04/2018", 24));
ships.add(new CargoShip("CargoShipA", "02/04/2018", 40));
ships.add(new CargoShip("CargoShipB", "04/04/2018", 12));
// printing the ships by iterating over the array list of ships
for (int i = 0; i < ships.size(); i++){
System.out.println(ships.get(i));
}
See more about Java at brainly.com/question/2266606
