[Solved] ShipDemo.java

$25

File Name: ShipDemo.java.zip
File Size: 122.46 KB

SKU: [Solved] ShipDemo.java Category: Tag:
5/5 - (1 vote)

This lab is meant to further your understanding of inheritance, polymorphism, and abstract classes. This lab should further help you (as did last weeks lab) to complete the first programming project.

I. Inheritance 55 points

This part of the lab is a more involved version of the inheritance programming part of last weeks lab. Since that time, youve learned about polymorphism, and abstract classes and methods, and youve 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):

Ship -name String yearBuilt int numProellars: int +Ship(name : String, year: int) setter and getter methods not shown +taString0 String CargoShip tonnage :int isDoubleHull boolean tCargoShip(name : String, year : int, tonnage int, doubleHull : baolean) setter and getter methods, and remaining toString method, not shown CruiseShip passengers int numPets int + CuiseShip(name : String, year int. passengers : int, mPets int) setter and getter methods, and remaining toString method, not shown

Ship -name String yearBuilt int numProellars: int +Ship(name : String, year: int) setter and getter methods not shown +taString0 String CargoShip tonnage :int isDoubleHull boolean tCargoShip(name : String, year : int, tonnage int, doubleHull : baolean) setter and getter methods, and remaining toString method, not shown CruiseShip passengers int numPets int + CuiseShip(name : String, year int. passengers : int, mPets int) setter 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 top-most UML diagram. Several things to note:

The Ship class name is italicized, which means that the Ship class is abstract. You 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.

The class has a numPropellars field, but the constructor does NOT accept an argument that corresponds to the numPropellars field. Youll need to decide how to set the value of numPropellars. There are multiple ways that you can do this (I am leaving this choice to you, and the UML diagram is intentionally vague in this regard).

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 it should compile.

Write the CargoShip subclass.

The Ship superclass does not have a default constructor, so in the CargoShip constructor, youll 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, youll need to call the superclasss getYearBuilt method, when you want to retrieve that 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, youll need to invoke the superclass constructor, using the super keyword, and passing into 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. 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 45 Points

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 superclass is defined:

public class Auto{ // content of class }

along with the following two subclasses

public class Convertible extends Auto{ // content of class}

public class Truck extends Auto{ // content of class}

Polymorphism, then, allows you to do the following:

Auto myAuto = new Truck();

Auto notMyAuto = new Convertible();

in which the reference variables myAuto and notMyAuto, both of type Auto, are referring to objects that are of type Truck and Convertible, respectively. This is allowed, because both 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, NUM_SHIPS, and assign it the value of at least 3.

Create an array, ships, that holds NUM_SHIPS of Ship references. Thus, you should have something like the following:

Ship[] ships = new Ship[NUM_SHIPS];

Populate the ships array, with objects of type CruiseShip and CargoShip. Youll need to have as many lines of code to instantiate these objects as you have space in the 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 1.

III. Rubric and Submission

Item Points
Ship.java written correctly, and it compiles 15
Subclasses CargoShip.java and CruiseShip.java written correctly, and they compile 40
ShipDemo.java written, in which you have an array of at least three Ship objects, for each of which the toString method is invoked 45
Total 100

Zip your java files (Ship.java, CargoShip.java, CruiseShip.java, and ShipDemo.java) and submit the zip file via Canvas.

Reviews

There are no reviews yet.

Only logged in customers who have purchased this product may leave a review.

Shopping Cart
[Solved] ShipDemo.java
$25