Question: Write a java program Use inheritance to implement the following classes: A: A Car that is a Vehicle and has a name, a max_speed value

Write a java program Use inheritance to implement the following classes:

A: A Car that is a Vehicle and has a name, a max_speed value and an instance variable of type int, called the number_of_cylinders, that specifies the number of cylinders in the cars engine. Add public methods to set and get the values of these variables. When a car is printed (using the toString method), its name, max_speed and number_of_cylinders are shown.

B: An Airplane that is also a vehicle and has a name, a max_speed value and an instance variable of type int, called the number_of_engines, that specifies the number of jet engines the airplane has. Add public methods to set and get the values of these variables. When an airplane is printed (using the toString method), its name, max_speed and number_of_engines are shown.

C: Add a constructor method to the Vehicle class that initializes its instance variables.

D: Write a VehicleDemo.java class that does the following: 1- Creates an instance of a Car and an Airplane class. 2- Assign values to the name, speed, number_of_cylinders (for the Car object) and number_of_engines (for the Airplane object) variables. 3- Compares which vehicle goes faster and prints the result. 4- Prints the instances of the car and airplane classes.

Part 2

Make the following changes to the above assignment:

A: Make the Vehicle an abstract class and add the following abstract method to it: float runningCost(int hour); Which receives the hours of operation as a parameter and returns the running cost of the vehicle. The Car and Airplane classes will implement this method as follows: - For the Car class, define a private float instance variable called cost_per_cylinder_per_hour. The runningCost of a Car will be equal to: hours of operation X cost_per_cylinder_per_hour X number_of_cylinders - For the Airplane class, define a private float instance variable called cost_per_engines_per_hour. The runningCost of an Airplane will be equal to: hours of operation X cost_per_engines_per_hour X number_of_engines

B: Write an interface called maintainable, which has the following method: float maintenanceCost(float costPerUnit); It receives the cost per unit of engine or cylinder and returns maintenance cost. The Car and Airplane classes will implement this interface as follows: - For the Car class, the maintenance cost of a Car will be equal to: costPerUnit X number_of_cylinders - For the Airplane class, the maintenance cost of an Airplane will be equal to: costPerUnit X number_of_engines

C: Add a String toString() method to the Vehicle class. When a Vehicle is printed, its name and max_speed are shown. Also, the String toString() methods of the Car and Airplane classes will show the following: - When a car is printed, its name, max_speed, number_of_cylinders and cost_per_cylinder_per_hour are shown. - When an airplane is printed, its name, max_speed, number_of_engines and cost_per_engines_per_hour are shown.

D: Write a VehicleDemo.java class that does the following: 1- Creates an instance of a Car and an instance of an Airplane class. 2- Assign values to the name, max_speed, number_of_cylinders and cost_per_cylinder_per_hour of the Car object and the name, max_speed, number_of_engines and cost_per_engines_per_hour of the Airplane object instances. 3- Calls all methods that are available to the Car and Airplane classes and prints their results. 4- Defines a reference variable of type Vehicle and assigns it to an instance of a Car class. Example: Vehicle v1 = new Car(); 5- Prints v1. Notice whether the toString() method of the Vehicle class is called or the toString() method of the Car class. Explain why it has been the case.

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!