View the step-by-step solution to:

I am learning the java abstract and Polymorphis. I wrote four java files and they can run. RunName: null Type: Cruise Ship Maximum passengers: 0 Year...

I am learning the java abstract and Polymorphis. I wrote four java files and they can run.

Run:Name: null

Type: Cruise Ship

Maximum passengers: 0

Year build: 1998

Num Pets Allowed: 0


Name: null

Type: Cargo

Cargo capacity 0

Year build: 1800

Double Hull: false


Name: null

Type: Cruise Ship

Maximum passengers: 0

Year build: 2001

Num Pets Allowed: 0


I want to change these null and 0 to be the information I set in the last demo java. Can someone help me to find my mistake? Thanks!




1.public abstract class Ship{

  private String name;

  private int yearBuilt;

  private int numPropellars = 0;

  public Ship(String name,int year){

     name = this.name;

     yearBuilt = year;

  }

  public String getName(){

     return name;

  }

  //public void setName(String name){

     //name = this.name;

  //}

  public int getYearBuilt(){

     return yearBuilt;

  }

  //public void setYearBuilt(int year){

     //yearBuilt = year;

  //}

  public abstract String toString();

}


public class CruiseShip extends Ship{

  private int passengers;

  private int numPets;




 2. public CruiseShip(String name,int year, int passengers,

                    int numPets){

     super(name,year);

     passengers = this.passengers;

     numPets = this.numPets;

  }


  public void setNumPets(int numPets){

  numPets = this.numPets;

  }


  public void setPassengers(int passengers){

  passengers = this.passengers;

  }


  public int getPassengers(){

  return passengers;

  }


  public int getNumPets(){

  return numPets;

  }



  3.public String toString(){

  String str = "Name: "+super.getName()+"nType: Cruise Ship"

              +"nMaximum passengers: "+passengers+"nYear build: "

              +super.getYearBuilt()+"nNum Pets Allowed: "+ numPets+"n";

  return str;

  }


}


public class CargoShip extends Ship{

  private int tonnage;

  private boolean isDoubleHull;


  public CargoShip(String name,int year,int tonnage,boolean doubleHull){

     super(name,year);

     tonnage = this.tonnage;

     isDoubleHull = this.isDoubleHull;

  }

  public int getTonnage(){

  return tonnage;

  }

  public void setTonnage(int tonnage){

     tonnage = this.tonnage;

  }

  public boolean getIsDoubleHull(){

  return isDoubleHull;

  }

  public void setIsDoubleHull(boolean isDoubleHull){

     isDoubleHull = this.isDoubleHull;

  }

  public String toString(){

  String str = "Name: "+super.getName()+"nType: Cargo"

              +"nCargo capacity "+tonnage+"nYear build: "

              +super.getYearBuilt()+"nDouble Hull: "+ isDoubleHull+"n";

  return str;

  }

}


4.public class ShipDemo{


  public static void main(String[] args){

     final int NUM_SHIPS = 3;

     Ship[] ships = new Ship[NUM_SHIPS];

     ships[0] = new CruiseShip("Disney Goofy",1998,2400,23);

     ships[1] = new CargoShip("Too Small",1800,500,false);

     ships[2] = new CruiseShip("I Wish I Had a Bigger Boat",2001,4,3);


     for(int i = 0;i<3;i++){

     System.out.println(ships[i].toString());

     }



  }

}

Top Answer

The output after correction is as follows: Name: Disney Goofy Type: Cruise Ship Maximum... View the full answer

Capture.PNG

Sign up to view the full answer

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

-

Educational Resources
  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question
Ask a homework question - tutors are online