Question

QUESTION 5 (15 Marks) Inheritance and Polymorphism Design a Ship class that has the following members: • A private String dat• A constructor that creates a cargo ship with the specified name, the specified year that the ship was built, and the specif

0 0
Add a comment Improve this question Transcribed image text
✔ Recommended Answer
Answer #1

Ship.java

/** Definition of Ship class */
public class Ship
{

/** private member variables of Ship class */
private String name;
private String yearBuilt;

/** Constructor to initialize the member variables */
public Ship(String na, String year)
{
name = na;
yearBuilt = year;
}
  
/** Setter methods for the private member variables */
public void setName(String na)
{
name = na;
}
  
public void setYearBuilt(String year)
{
yearBuilt = year;
}
  
/** Getter methods for the private member variables */
public String getName()
{
return name;
}
  
public String getYearBuilt()
{
return yearBuilt;
}
  
/** toString method of Object class is overridden to return a string
* with the details of the Ship
*/
@Override
public String toString()
{
String s="";
s = "Ship \n";
s = s + "Name: " + getName() + "\n";
s = s + "Year Built: " + getYearBuilt() + "\n";
return s;
}
}

/** Definition of Ship class */ public class Ship /** private member variables of Ship class */ private String name; privatepublic String getYearBuilt() return yearBuilt; /** toString method of Object class is overridden to return a string * with th

=======================================================================================

CargoShip.java

/** Definition of subclass CargoShip from superclass Ship */
public class CargoShip extends Ship
{
/** private member variable of CargoShip class */
private int tonnage;

/** Constructor to initialize the member variables of subclass */
public CargoShip(String na, String year, int weight)
{
/** super() keyword is used to call the constructor of
* the superclass before initializing the member variables of
* subclass
*/
super(na,year);
tonnage = weight;
}
  
/** Setter and getter */
public void setTonnage(int weight)
{
tonnage = weight;
}
  
public int getTonnage()
{
return tonnage;
}

/** The toString() method of superclass Ship is overridden by the subclass */
@Override
public String toString()
{
String s="";
s = "CargoShip \n";
s = s + "Name: " + getName() + "\n";
s = s + "Cargo Capacity: " + getTonnage() + " tons\n";
return s;
}
}

/ ** Definition of subclass CargoShip from superclass Ship public class CargoShip extends Ship /** private member variable of/** The toString() method of superclass Ship is overridden by the subclass */ @Override public String toString() String s=;

=======================================================================================

CruiseShip.java

/** Definition of subclass CruiseShip from superclass Ship */
public class CruiseShip extends Ship
{
/** private member variable of CruiseShip class */
private int passengerCapacity;

/** Constructor to initialize the member variables of subclass */
public CruiseShip(String na, String year, int capacity)
{
/** super() keyword is used to call the constructor of
* the superclass before initializing the member variables of
* subclass
*/
super(na,year);
passengerCapacity = capacity;
}
  
/** Setter and getter */
public void setPassengerCapacity(int capacity)
{
passengerCapacity = capacity;
}
  
public int getPassengerCapacity()
{
return passengerCapacity;
}

/** The toString() method of superclass Ship is overridden by the subclass */
@Override
public String toString()
{
String s="";
s = "CruiseShip \n";
s = s + "Name: " + getName() + "\n";
s = s + "Passenger Capacity: " + getPassengerCapacity() + " persons\n";
return s;
}
}

/** Definition of subclass CruiseShip from superclass Ship */ public class Cruise Ship extends Ship /** private member variab/** The toString() method of superclass Ship is overridden by the subclass */ @Override public String toString() String s=;

===================================================================================

TestShips.java

/** Test Class */
public class TestShips
{
/** Driver method to create objects of all three ships
* and print their details
*/
public static void main(String[] args)
{
/** Create an array of type Ship, to hold Ship objects */
Ship ShipArray[] = new Ship[3];

/** Create an object of super class Ship */
Ship ship1 = new Ship("Lolipop","1960");
/** Create an object of subclass CruiseShip and assign the object
to Ship reference variable */
Ship ship2 = new CruiseShip("Disney Magic","1960",2400);
/** Create an object of subclass CargoShip and assign the object
to Ship reference variable */
Ship ship3 = new CargoShip("Black Pearl","1960",50000);
  
/** Store all three objects in the array */
ShipArray[0] = ship1;
ShipArray[1] = ship2;
ShipArray[2] = ship3;
  
/** Loop through the array to print all ships */
for(int i=0;i<ShipArray.length;i++)
{
System.out.println(ShipArray[i]);
System.out.println();
}
}
}

/** Test Class */ public class Test Ships /** Driver method to create objects of all three ships * and print their details */

========================================================================================

Output:

Ship Name: Lolipop Year Built: 1960 Cruise Ship Name: Disney Magic Passenger Capacity: 2400 persons CargoShip Name: Black Pea

Add a comment
Know the answer?
Add Answer to:
QUESTION 5 (15 Marks) Inheritance and Polymorphism Design a Ship class that has the following members:...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • [JAVA] Program: Design a Ship class that the following members: A field for the name of...

    [JAVA] Program: Design a Ship class that the following members: A field for the name of the ship (a string) o A field for the year the the ship was built (a string) o 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...

  • Program Challenge 10 Design a Ship class that the following members: ·         A field for the...

    Program Challenge 10 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...

  • Design a Ship class that has the following class members: A field for the name of the ship...

    Design a Ship class that has the following class members: A field for the name of the ship A field for the year the ship was built A constructor Appropriate accessors and mutators toString method that displays the ships name and the year it was built Design a CruiseShip class the inherits from the Ship class. Include the following members: A field for the max number of passengers A constructor Appropriate accessors and mutators toString method that overrides the Ship toString method. The method...

  • Python: Design a Ship class that has the following members: A member variable for the name...

    Python: Design a Ship class that has the following members: A member variable for the name of the ship. A member variable for the year that the ship was built. An init method and appropriate accessors and mutators. A print function that displays the ship’s name and the year it was built. Design a CruiseShip class that is derived from the Ship class. The CruiseShip class should have the following members: A member variable for the maximum number of passengers....

  • Ship, CruiseShip, and CargoShip Classes (in C++ language i use visual studios to code with) design...

    Ship, CruiseShip, and CargoShip Classes (in C++ language i use visual studios to code with) design a Ship class that has the following members: - A member variable for the name of the ship (a string) - A member variable for the year that the ship was built (a string) - A contsructor and appropriate accessors and mutators - A virtual print function that displays the ship's name and the year it was built (nobody seems to get this part...

  • using java write a code with notepad++ Create the following classes using inheritance and polymorphism Ship...

    using java write a code with notepad++ Create the following classes using inheritance and polymorphism Ship (all attributes private) String attribute for the name of ship float attribute for the maximum speed the of ship int attribute for the year the ship was built Add the following behaviors constructor(default and parameterized) , finalizer, and appropriate accessor and mutator methods Override the toString() method to output in the following format if the attributes were name="Sailboat Sally", speed=35.0f and year built of...

  • Help needed on C++ program: Program Description: Complete the Ship, CruiseShip, and CargoShip program (#12 in...

    Help needed on C++ program: Program Description: Complete the Ship, CruiseShip, and CargoShip program (#12 in the 9th edition of the text). Read the specific method requirements in the text. Specific Requirements: • Create the Ship class. • Create CruiseShip and CargoShip classes that are derived from Ship. • Create a small tester cpp file that has an array of Ship pointers (one each of Ship, CruiseShip, and CargoShip). The program steps through the array, calling each object’s print method....

  • JAVA PROGRAMMING In this final review lab from our intro course, use the Name class you...

    JAVA PROGRAMMING In this final review lab from our intro course, use the Name class you created in the previous lab. In this lab, create a Student class with the following class variable: Student name: Name (Note: Name is a datatype) Student Id: String Create the getter and setter methods. In addition to these methods, create a toString() method, which overrides the object class toString() method. This override method prints the current value of any of this class object. Complete...

  • Inheritance and Polymorphism (use Pet.java) You are given a class named Pet.java.   Create two child classes...

    Inheritance and Polymorphism (use Pet.java) You are given a class named Pet.java.   Create two child classes named Cat.java and Dog.java. Cat.java should add attributes for color and breed. Dog.java should add attributes for breed and size (use String for small, medium, large). Add appropriate constructors, getter/setter methods and toString() methods.   In a separate file named PetDriver.java, create a main. In the main, create an ArrayList of Pet. Add an instance of Cat and an instance of Dog to the ArrayList....

  • In C++, design a class named "Ship" that keeps the following information: Registry (which country the...

    In C++, design a class named "Ship" that keeps the following information: Registry (which country the ship is registered in) Homeport (where the ship normally is when not underway) Name (the ship's name) Length (in feet) Displacement (in tons. Displacement is a measure of how large the ship is) Write a default constructor as well as a constructor that takes all five arguments. Write appropriate accessor and mutator functions for the class. Next, write a class named "Ferry" that is...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT