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;
}
}
=======================================================================================
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;
}
}
=======================================================================================
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;
}
}
===================================================================================
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();
}
}
}
========================================================================================
Output:
QUESTION 5 (15 Marks) Inheritance and Polymorphism Design a Ship class that has the following members:...
[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 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...
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 (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 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....
Here is a sample run of the tester program: Make sure your program follows the Java Coding Guidelines. Consider the following class: /** * A store superclass with a Name and Sales Tax Rate */ public class Store { public final double SALES_TAX_RATE = 0.06; private String name; /** * Constructor to create a new store * @param newName */ public Store(String newName) { name = newName; } ...
Use inheritance to create a new class AudioRecording based on Recording class that: it will retain all the members of the Recording class, it will have an additional field bitrate (non-integer numerical; it cannot be modified once set), its constructors (non-parametrized and parametrized) will set all the attributes / fields of this class (reuse code by utilizing superclass / parent class constructors): Non-parametrized constructor should set bitrate to zero, If arguments for the parametrized constructor are illegal or null, it...
Java Create four classes: 1. An Animal class that acts as a superclass for Dog and Bird 2. A Bird class that is a descendant of Animal 3. A Dog class that is a descendant of Animal 4. A “driver” class named driver that instantiates an Animal, a Dog, and a Bird object. The Animal class has: · one instance variable, a private String variable named name · a single constructor that takes one argument, a String, used to set...
USING JAVA, Class Design: Person The Person class is intended to be an abstract and simplified representation of a person. Each person will have an array of "friends" - which are other "Person" instances. Data to Store (2 Points) Name private instance variable with only private setter (2 Points) Friends private instance array with neither getter nor setter Actions Constructor o 1 Point) Take in as argument the name of the person (enforce invariants) o (1 Point) Initialize the array...
College of Winston and Charlotte This program will calculate the amount for a semester bill at The College of Winston and Charlotte. Part 1: Create a class Course.java: The class has non-static instance variables: private String department private int courseNumber private int courseCredits private double courseCost The class must have a default constructor which will set values as follows: department = “unknown” courseNumber = 0 courseCost = 0 courseCredits = 0 The class must have a non-default constructor which will...