Question: JAVA ASSIGNMENT NOTE: PLEASE DO NOT COMBINE INTO ONE CLASS WITH OBJECTS, CLASSES SHOULD BE SEPERATE AND INHERIT FROM SUPER CLASS AND TESTER SHOULD BE

JAVA ASSIGNMENT

NOTE: PLEASE DO NOT COMBINE INTO ONE CLASS WITH OBJECTS, CLASSES SHOULD BE SEPERATE AND INHERIT FROM SUPER CLASS AND TESTER SHOULD BE SEPARATE CLASS.

1) Create 3 classes, Vehicle, Car, and Boat. Make one of them the super class, and make the other two classes extend from this super class. Choose appropriately.

2) Create an interface, Efficiency, that contains a single method, getEfficiency(). This method should return a double.

3) Create a single instance variable, efficiency, in the super class from step 2.

4) Create a constructor for the super class from step two. It should accept a single parameter as a double. This parameter should be used to initialize the instance variable created in step 4.

5) Add constructors for the two subclasses that will also accept a single parameter as a double. They must call the super class constructor with this parameter.

6) Create a printMessage() method in each class that accepts no parameters and returns noth-ing. This method should print I am a Vehicle/Car/Boat, VROOM!!!. (Choose Vehicle, Car, or Boat according to which classs method you are implementing).

7) Create a single getName() method in the super class that returns a String and accepts no pa-rameters. This method will be inherited by the two subclasses. Return a string containing the class name.

8) Make your super class implement the Efficiency interface. (This will also make the subclasses implement the interface because of inheritance).

9) Create a class called Tester with a main() method.

10) Declare a constant NUM_VEHICLES in the Tester class and set its value to 10.

11) Your main method in the Tester class will have a few jobs it must do:

11a) Fill an array of type Vehicle[] of size NUM_VEHICLES with Vehicle objects. You need to generate a random number between 0 and 2. If the number is 0, add a Vehicle to the array. 1, add a Car, 2, add a Boat. The Vehicle/Car/Boat that you add must be initialized with a random efficiency between 0 and 100.0. Do this until you have added NUM_VEHICLES objects to the array.

11b) Loop through the array and call the printMessage() method for each object in the array. Explain in the comments before this loop why this is an example of polymorphism.

11c) Loop through this array and print the class of the object and the efficiency of the object. Your print out should look like this:

Boat: 64.2669

Vehicle: 69.7662

Car: 42.7745

Car: 41.8222

12) Create a static method in your Tester class called getFirstBelowT() that accepts an array of Efficiency objects and a double that represents a threshold value. This method should return an Efficiency object that is the first object in the array with an efficiency below the threshold parameter. Return null if no objects have an efficiency below the thresh-old.

13) From the main method pass the array you created in step 11a and a threshold of 20 to the getFirstBelowT() method. Save the return value in a Vehicle reference variable that you declare. You will have to cast the return value from the method to a Vehicle because the actual return type of the getFirstBelowT() method is an Efficiency reference variable. Use this Vehicle reference variable to print the type of object that had the low ef-ficiency, as well as the efficiency. Your printout should look like this:

The first object with efficiency less than 20 was Boat with effi-ciency of 12.2680

14) If there was no object with efficiency less than 20, you should print an appropriate message. (if the return value is null, then none of the objects in the array had an efficiency less than 20).

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!