Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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.

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 nothing. 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 parameters. 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:

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.

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.

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 threshold.

13-From the main method pass the array you created in step 12a 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 efficiency, as well as the efficiency. Your printout should look like this:

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

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).

I have completed up until step 11 where I start on the tester method and here is where I no longer understand what to do. I currently have my superclass: Vehicle and all the subclasses: boat and car as well as the interface: efficiency completed but I do not know how to do the last 4 steps involved the tester class.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Handbook Of Relational Database Design

Authors: Candace C. Fleming, Barbara Von Halle

1st Edition

0201114348, 978-0201114348

More Books

Students also viewed these Databases questions