Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I. Inheritance This part of the lab is a more involved version of the inheritance programming part of last week's lab. Since that time, you've
I. Inheritance
This part of the lab is a more involved version of the inheritance programming part of last week's lab. Since that time, you've learned about polymorphism, and abstract classes and methods, and you've been told that:
An abstract class cannot be instantiated explicitly. Only objects that are inherited from an
abstract class can be instantiated.
For this part of the lab, you will write an abstract superclass, Ship, and two subclasses,
CargoShip and CruiseShip. The class Ship is abstract so you cannot directly instantiate an object of type Ship. The UML diagrams note that inheritance arrows is NOT shown in this UML diagram
name : String
yearBuilt : int
numEngines : in
Shipname : String, year : int
toString : String
setter and getter methods, and remaining toString method, not shown
tableCruiseShip passengers : intnumPets : int CruiseShipname : String, year : int, passengers : int, numPets : intsetter and getter methods, and remaining toString method, not shown
To complete this part of the lab:
Write the Ship superclass, as described in the above topmost UML diagram. Several things to note:
The Ship class name is italicized, which means that the Ship class is abstract Yo need to use the abstract java keyword, to indicate that the class is abstract The toString method in the Ship class, is italicized, so it is abstract That means, that it cannot have a body, and that all subclasses of Ship MUST have an overriding tostring method.
that corresponds mumEngines field, but the constructor does NOT accept an argumen of numEngines. There numEngines field. You'll need to decide how to set the value to you, and the UML diagram is intentionally vau can do this I am leaving this choice Compile the Ship superclass as soon as you write it Because it is not a child of any other class, you should be able to write code for it that is free of syntax errors, and should compile.
Write the CargoShip subclass.
The Ship superclass does not have a default constructor, so in the CargoShip constructor, you'll need to invoke the superclass constructor, using the super keyword, and pass to it the correct number of arguments as specified in the Cargo class constructor
You MUST write a toString method, whose argument list is identical to the argument list of the abstract method toString in the superclass. Notice that the subclass CargoShip does NOT have the field yearBuilt, so in the toString method, you'll nee to call the su information
Compile the subclass. You can do this ONLY after the Ship superclass has been coded correctly, because the CargoShip class extends the Ship superclass.
Write the CruiseShip subclass.
The Ship superclass does not have a default constructor, so in the CruiseShip constructor, you'll need to invoke the superclass constructor, using the super keyword, and passing in Cargo class constructor
You MUST write a toString method, whose argument list is identical to the argument list of the abstract method toString in the superclass. The same instructions apply to the toString method of this class, as for the CargoShip class.
Just as you did with the CargoShip subclass, compile this CruiseShip subclass.
II Ship Demo; Polymorphism
Recall that polymorphism, which means "many forms" refers to a superclass reference variable being able to reference objects of a subclass. For example, assume that the following superclas is defined:
public class Autol content of class
along with the following two subclasses
public class Convertible extends Autol content of class
public class Truck extends Auto content of class
Polymorphism, then, allows you to do the following:
Auto myAuto new Truck;
onvertible;
in which the reference variables myAuto and notMyAuto, both of type Auto, are referring to Truck and Convertible are subclasses of Auto, so they both are specialized versions of an Auto.
For this part of the lab:
Create a file, ShipDemo.java
In the main method, create a final int variable, NUMSHIPS, and assign it the value of at least
Create an array, ships, that holds NUMSHIPS of Ship references. Thus, you should have something like the following:
Ship ships new Ship NUMSHIPS;
Populate the ships array, with objects of type CruiseShip and CargoShip. You'll need to have as many lines of code to instantiate these objects as you have space in the array ships. array ships.
Write a for loop, that iterates over the array ships, and invokes the toString method of each object.
Compile and debug your program. A sample invocation is shown in Figure Figure : Sample invocation of program ShipDemo
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started