JAVA Design a Ship CargoShip and CruiseShip class being mind
[JAVA]
Design a Ship, CargoShip and CruiseShip class being mindful of behavior of each. Demonstrate the classes in a program that has a Ship array. Assign various Ships, CruiseShip and CargoShip to the array elements.CargoShip and CruiseShip are a child of Ship class.
Ship -
boolean deck;
int size;
int capacity;
String manufacturer;
String name;
String shiptype
int weight;
Extends Ship
CargoShip
int containercount;
String exhaustport;
int totalcontainerweight;
String typeofCargo;
String cargoshiptype;
CruiseShip
int numberofguests;
int numberofpools;
String nationality;
int numofrestaurants;
int numberofcasino;
Demo polymorphism --> write two or three overridden methods to demonstrate polymorphism.
Solution
Hi.
Please see below the java classes. Please comment for any queries/ feedbacks.
Thanks,
Anita
Ship.java
 public class Ship {
    private boolean deck;
    private int size;
    private int capacity;
    private String manufacturer;
    private String name;
    private String shiptype;
    private int weight;
   
    public Ship (   int size,   int capacity,   String manufacturer,   String name,
            String shiptype,   int weight ){
        this.size = size;
        this.capacity = capacity;
        this.manufacturer = manufacturer;
        this.name = name;
        this.shiptype = shiptype;
        this.weight = weight;
    }
    public boolean isDeck() {
        return deck;
    }
    public void setDeck(boolean deck) {
        this.deck = deck;
    }
    public int getSize() {
        return size;
    }
    public void setSize(int size) {
        this.size = size;
    }
    public int getCapacity() {
        return capacity;
    }
    public void setCapacity(int capacity) {
        this.capacity = capacity;
    }
    public String getManufacturer() {
        return manufacturer;
    }
    public void setManufacturer(String manufacturer) {
        this.manufacturer = manufacturer;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getShiptype() {
        return shiptype;
    }
    public void setShiptype(String shiptype) {
        this.shiptype = shiptype;
    }
    public int getWeight() {
        return weight;
    }
    public void setWeight(int weight) {
        this.weight = weight;
    }
   //parent class method displayDetails
    public void displayDetails(){
        System.out.println(\"This is Ship Class details: \");
        System.out.println(\"deck : \"+this.isDeck());
        System.out.println(\"size : \"+this.getSize());
        System.out.println(\"Capacity : \"+this.getCapacity());
        System.out.println(\"Manufacturer : \"+this.getManufacturer());
        System.out.println(\"Name : \"+this.getName());
        System.out.println(\"Shiptype : \"+this.getShiptype());
        System.out.println(\"Weigth : \"+this.getWeight());
    }
    //parent class method displaySize
    public void displaySize(){
        System.out.println(\"Ship size: \" +this.getSize());
    }
    //parent class method displayType
    public void displayType(){
        System.out.println(\"Ship Type : \"+this.getShiptype());
    }
   
 }
CargoShip.java
  public class CargoShip extends Ship {
   
   
   private int containercount;
    private String exhaustport;
    private int totalcontainerweight;
    private String typeofCargo;
    private String cargoshiptype;
   
   
    public CargoShip(int size, int capacity, String manufacturer, String name,
            String shiptype, int weight, int containercount, String exhaustport,
            int totalcontainerweight,
            String typeofCargo,String cargoshiptype) {
        super(size, capacity, manufacturer, name, shiptype, weight);
        this.containercount = containercount;
        this.exhaustport = exhaustport;
        this.totalcontainerweight = totalcontainerweight;
        this.typeofCargo = typeofCargo;
        this.cargoshiptype = cargoshiptype;
    }
    public int getContainercount() {
        return containercount;
    }
    public void setContainercount(int containercount) {
        this.containercount = containercount;
    }
    public String getExhaustport() {
        return exhaustport;
    }
    public void setExhaustport(String exhaustport) {
        this.exhaustport = exhaustport;
    }
    public int getTotalcontainerweight() {
        return totalcontainerweight;
    }
    public void setTotalcontainerweight(int totalcontainerweight) {
        this.totalcontainerweight = totalcontainerweight;
    }
    public String getTypeofCargo() {
        return typeofCargo;
    }
    public void setTypeofCargo(String typeofCargo) {
        this.typeofCargo = typeofCargo;
    }
    public String getCargoshiptype() {
        return cargoshiptype;
    }
    public void setCargoshiptype(String cargoshiptype) {
        this.cargoshiptype = cargoshiptype;
    }
   
  
    //Overriden class method displayDetails
    public void displayDetails(){
        System.out.println(\"This is CargoShip details : \");
        super.displayDetails();
        System.out.println(\"Containercount : \"+this.getContainercount());
        System.out.println(\"Exhaustport : \"+this.getExhaustport());
        System.out.println(\"Totalcontainerweight : \"+totalcontainerweight);
        System.out.println(\"TypeofCargo : \"+this.getTypeofCargo());
        System.out.println(\"Cargoshiptype : \"+this.getCargoshiptype());
    }
    //Overriden class method displayType
    public void displaySize(){
        System.out.println(\"CargoShip size: \" +this.getSize());
    }
   
    //Overriden class method displayType
    public void displayType(){
        System.out.println(\"CargoShip Type : \"+this.getShiptype());
    }
}
CruiseShip.java
 public class CruiseShip extends Ship {
   
   
   private int numberofguests;
    private int numberofpools;
    private String nationality;
    private int numofrestaurants;
    private int numberofcasino;
   
    public CruiseShip(int size, int capacity, String manufacturer, String name,
            String shiptype, int weight,int numberofguests,int numberofpools,
                String nationality,int numofrestaurants,int numberofcasino) {
        super(size, capacity, manufacturer, name, shiptype, weight);
        this.numberofguests = numberofguests;
        this.numberofpools = numberofpools;
        this.nationality = nationality;
        this.numofrestaurants = numofrestaurants;
        this.numberofcasino = numberofcasino;
    }
    public int getNumberofguests() {
        return numberofguests;
    }
    public void setNumberofguests(int numberofguests) {
        this.numberofguests = numberofguests;
    }
    public int getNumberofpools() {
        return numberofpools;
    }
    public void setNumberofpools(int numberofpools) {
        this.numberofpools = numberofpools;
    }
    public String getNationality() {
        return nationality;
    }
    public void setNationality(String nationality) {
        this.nationality = nationality;
    }
    public int getNumofrestaurants() {
        return numofrestaurants;
    }
    public void setNumofrestaurants(int numofrestaurants) {
        this.numofrestaurants = numofrestaurants;
    }
    public int getNumberofcasino() {
        return numberofcasino;
    }
    public void setNumberofcasino(int numberofcasino) {
        this.numberofcasino = numberofcasino;
    }
   
    //Overriden class method displayDetails
    public void displayDetails(){
        super.displayDetails();
        System.out.println(\"This is CruiseShip details : \");
        System.out.println(\"numberofguests : \"+this.getNumberofguests());
        System.out.println(\"numberofpools : \"+this.getNumberofpools());
        System.out.println(\"nationality : \"+this.getNationality());
        System.out.println(\"numofrestaurants : \"+this.getNumofrestaurants());
        System.out.println(\"numberofcasino : \"+this.getNumberofcasino());
    }
    //Overriden class method displaySize
    public void displaySize(){
        System.out.println(\"CruiseShip size: \" +this.getSize());
    }
    //Overriden class method displayType
    public void displayType(){
        System.out.println(\"CruiseShip Type : \"+this.getShiptype());
    }
 }
ShipLoader.java
 public class ShipLoader {
   
    public static void main(String [] args){
        Ship [] shipArr = new Ship [5];
        Ship ship1 = new CargoShip(100, 100, \"AAA\", \"SHIP1\", \"CARGO\", 100, 100, \"100\", 1000, \"CARGO1\", \"CARGOTYPE1\");
        Ship ship2 = new CruiseShip(200, 200, \"BBB\", \"SHIP2\", \"CRUISE\", 200, 100, 20, \"US\", 3, 4);
        Ship ship3 = new CargoShip(300, 300, \"CCC\", \"SHIP3\", \"CARGO\", 300, 300, \"300\", 3000, \"CARGO3\", \"CARGOTYPE3\");
        Ship ship4 = new CruiseShip(400, 400, \"DDD\", \"SHIP4\", \"CRUISE\", 200, 100, 40, \"US\", 5, 6);
        Ship ship5 = new CargoShip(500, 500, \"EEE\", \"SHIP5\", \"CARGO\", 500, 500, \"500\", 5000, \"CARGO5\", \"CARGOTYPE5\");
       
        //Populating ship objects top the array
       
        shipArr[0] = ship1;
        shipArr[1] = ship2;
        shipArr[2] = ship3;
        shipArr[3] = ship4;
        shipArr[4] = ship5;
       
        //Displaying details:
        for(int i=0; i<shipArr.length ; i++){
            Ship shipObj = (Ship) shipArr[i];
            shipObj.displayDetails();
            shipObj.displaySize();
            shipObj.displayType();
        }
       
    }
}
Sample Output:
This is CargoShip details :
 This is Ship Class details:
 deck : false
 size : 100
 Capacity : 100
 Manufacturer : AAA
 Name : SHIP1
 Shiptype : CARGO
 Weigth : 100
 Containercount : 100
 Exhaustport : 100
 Totalcontainerweight : 1000
 TypeofCargo : CARGO1
 Cargoshiptype : CARGOTYPE1
 CargoShip size: 100
 CargoShip Type : CARGO
 This is Ship Class details:
 deck : false
 size : 200
 Capacity : 200
 Manufacturer : BBB
 Name : SHIP2
 Shiptype : CRUISE
 Weigth : 200
 This is CruiseShip details :
 numberofguests : 100
 numberofpools : 20
 nationality : US
 numofrestaurants : 3
 numberofcasino : 4
 CruiseShip size: 200
 CruiseShip Type : CRUISE
 This is CargoShip details :
 This is Ship Class details:
 deck : false
 size : 300
 Capacity : 300
 Manufacturer : CCC
 Name : SHIP3
 Shiptype : CARGO
 Weigth : 300
 Containercount : 300
 Exhaustport : 300
 Totalcontainerweight : 3000
 TypeofCargo : CARGO3
 Cargoshiptype : CARGOTYPE3
 CargoShip size: 300
 CargoShip Type : CARGO
 This is Ship Class details:
 deck : false
 size : 400
 Capacity : 400
 Manufacturer : DDD
 Name : SHIP4
 Shiptype : CRUISE
 Weigth : 200
 This is CruiseShip details :
 numberofguests : 100
 numberofpools : 40
 nationality : US
 numofrestaurants : 5
 numberofcasino : 6
 CruiseShip size: 400
 CruiseShip Type : CRUISE
 This is CargoShip details :
 This is Ship Class details:
 deck : false
 size : 500
 Capacity : 500
 Manufacturer : EEE
 Name : SHIP5
 Shiptype : CARGO
 Weigth : 500
 Containercount : 500
 Exhaustport : 500
 Totalcontainerweight : 5000
 TypeofCargo : CARGO5
 Cargoshiptype : CARGOTYPE5
 CargoShip size: 500
 CargoShip Type : CARGO
![[JAVA] Design a Ship, CargoShip and CruiseShip class being mindful of behavior of each. Demonstrate the classes in a program that has a Ship array. Assign vario [JAVA] Design a Ship, CargoShip and CruiseShip class being mindful of behavior of each. Demonstrate the classes in a program that has a Ship array. Assign vario](/WebImages/39/java-design-a-ship-cargoship-and-cruiseship-class-being-mind-1117448-1761593863-0.webp)
![[JAVA] Design a Ship, CargoShip and CruiseShip class being mindful of behavior of each. Demonstrate the classes in a program that has a Ship array. Assign vario [JAVA] Design a Ship, CargoShip and CruiseShip class being mindful of behavior of each. Demonstrate the classes in a program that has a Ship array. Assign vario](/WebImages/39/java-design-a-ship-cargoship-and-cruiseship-class-being-mind-1117448-1761593863-1.webp)
![[JAVA] Design a Ship, CargoShip and CruiseShip class being mindful of behavior of each. Demonstrate the classes in a program that has a Ship array. Assign vario [JAVA] Design a Ship, CargoShip and CruiseShip class being mindful of behavior of each. Demonstrate the classes in a program that has a Ship array. Assign vario](/WebImages/39/java-design-a-ship-cargoship-and-cruiseship-class-being-mind-1117448-1761593863-2.webp)
![[JAVA] Design a Ship, CargoShip and CruiseShip class being mindful of behavior of each. Demonstrate the classes in a program that has a Ship array. Assign vario [JAVA] Design a Ship, CargoShip and CruiseShip class being mindful of behavior of each. Demonstrate the classes in a program that has a Ship array. Assign vario](/WebImages/39/java-design-a-ship-cargoship-and-cruiseship-class-being-mind-1117448-1761593863-3.webp)
![[JAVA] Design a Ship, CargoShip and CruiseShip class being mindful of behavior of each. Demonstrate the classes in a program that has a Ship array. Assign vario [JAVA] Design a Ship, CargoShip and CruiseShip class being mindful of behavior of each. Demonstrate the classes in a program that has a Ship array. Assign vario](/WebImages/39/java-design-a-ship-cargoship-and-cruiseship-class-being-mind-1117448-1761593863-4.webp)
![[JAVA] Design a Ship, CargoShip and CruiseShip class being mindful of behavior of each. Demonstrate the classes in a program that has a Ship array. Assign vario [JAVA] Design a Ship, CargoShip and CruiseShip class being mindful of behavior of each. Demonstrate the classes in a program that has a Ship array. Assign vario](/WebImages/39/java-design-a-ship-cargoship-and-cruiseship-class-being-mind-1117448-1761593863-5.webp)
![[JAVA] Design a Ship, CargoShip and CruiseShip class being mindful of behavior of each. Demonstrate the classes in a program that has a Ship array. Assign vario [JAVA] Design a Ship, CargoShip and CruiseShip class being mindful of behavior of each. Demonstrate the classes in a program that has a Ship array. Assign vario](/WebImages/39/java-design-a-ship-cargoship-and-cruiseship-class-being-mind-1117448-1761593863-6.webp)
