The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Jul 2017 |
| Last Sign in: | 313 Weeks Ago, 6 Days Ago |
| Questions Answered: | 15833 |
| Tutorials Posted: | 15827 |
MBA,PHD, Juris Doctor
Strayer,Devery,Harvard University
Mar-1995 - Mar-2002
Manager Planning
WalMart
Mar-2001 - Feb-2009
Can you help me? I want to know how to write the four .java files code.
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, CargoShipand 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 - numPropellars : int |
|
+ Ship(name : String, year : int) setter and getter methods not shown
+ toString() : String |
|
CargoShip |
|
- tonnage : int - isDoubleHull : boolean |
|
+ CargoShip(name : String, year : int, tonnage : int, doubleHull : boolean)
setter and getter methods, and remaining toString method, not shown |
|
CruiseShip |
|
- passengers : int - numPets : int |
|
+ CruiseShip(name : String, year : int, passengers : int, numPets : int)
setter and getter methods, and remaining toString method, not shown |
To complete this part of the lab:
1. 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. You'll 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.
2. Write the CargoShipsubclass.
• 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 need to call the superclass's 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.
3. Write the CruiseShipsubclass.
• 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 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.
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 myAutoand 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:
1. Create a file, ShipDemo.java
2. In the main method, create a final int variable, NUM_SHIPS, and assign it the value of at least 3.
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];
4. Populate the ships array, with objects of type CruiseShipand CargoShip. You'll need to have as many lines of code to instantiate these objects as you have space in the array ships.
5. Write a for loop, that iterates over the array ships, and invokes the toString() method of each object.
6. Compile and debug your program. A sample invocation is shown in Figure 1.
|
Name: Disney Goofy Type: Cruise Ship Maximum passengers: 2400 Year build: 1998 Num Pets Allowed: 23 ---------------------------- Name: Too Small Type: Cargo Cargo capacity: 500 tons Year built: 1800 Double Hull: false ---------------------------- Name: I Wish I Had a Bigger Boat Type: Cruise Ship Maximum passengers: 4 Year build: 2001 Num Pets Allowed: 3 |
|
Figure 1: Sample invocation of program ShipDemo |
----------- ----------- H-----------ell-----------o S-----------ir/-----------Mad-----------am ----------- Th-----------ank----------- yo-----------u f-----------or -----------you-----------r i-----------nte-----------res-----------t a-----------nd -----------buy-----------ing----------- my----------- po-----------ste-----------d s-----------olu-----------tio-----------n. -----------Ple-----------ase----------- pi-----------ng -----------me -----------on -----------cha-----------t I----------- am----------- on-----------lin-----------e o-----------r i-----------nbo-----------x m-----------e a----------- me-----------ssa-----------ge -----------I w-----------ill----------- be----------- qu-----------ick-----------ly