Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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

ID: 3827825 • Letter: W

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 and an instance variable of type int, called the number_of_cylinders, that specifies the number of cylinders in the car’s 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.

Explanation / Answer

Part A:

Vehicle.java

public class Vehicle {
   //Declaring variables
   private String name;
   private int max_speed;
  
   //Parameterized constructor
   public Vehicle(String name, int max_speed) {
       super();
       this.name = name;
       this.max_speed = max_speed;
   }
  
   //Setters and getters
   public String getName() {
       return name;
   }
   public void setName(String name) {
       this.name = name;
   }
   public int getMax_speed() {
       return max_speed;
   }
   public void setMax_speed(int max_speed) {
       this.max_speed = max_speed;
   }
   @Override
   public String toString() {
       return "Name=" + name + " Max Speed=" + max_speed;
   }
  
   }

______________________

Car.java

public class Car extends Vehicle {

   //Declaring instance variables
   int no_of_cylinders;

  
   //parameterized constructor
   public Car(String name, int max_speed, int no_of_cylinders) {
       super(name, max_speed);
       this.no_of_cylinders = no_of_cylinders;
   }

   //getters and setters
   public int getNo_of_cylinders() {
       return no_of_cylinders;
   }

   public void setNo_of_cylinders(int no_of_cylinders) {
       this.no_of_cylinders = no_of_cylinders;
   }

   //toString() method is used to display the contents of an object inside it
   @Override
   public String toString() {
       System.out.println(" ** CAR **");
       System.out.println(super.toString());
       return "No of cylinders=" + no_of_cylinders;
   }


}

_________________________

Airplane.java

public class Airplane extends Vehicle {
   //Declaring instance variables
   int no_of_engines;

   //parameterized constructor
   public Airplane(String name, int max_speed, int no_of_engines) {
       super(name, max_speed);
       this.no_of_engines = no_of_engines;
   }

   //getters and setters
   public int getNo_of_engines() {
       return no_of_engines;
   }

   public void setNo_of_engines(int no_of_engines) {
       this.no_of_engines = no_of_engines;
   }

   //toString() method is used to display the contents of an object inside it
   @Override
   public String toString() {
       System.out.println(" ** AIRPLANE **");
       System.out.println(super.toString());
       return "No of engines=" + no_of_engines;
   }
  

}

______________________

VehicleDemo.java

public class VehicleDemo {

   public static void main(String[] args) {
       //Creating the references of Vehicle class
       Vehicle car,airplane;
      
       //creating the Car class object
       car=new Car("Ferrari",300,12);
      
       //creating the Airplane class object
       airplane=new Airplane("Boeing 747-8",700,4);
      
      
       //Calling the toString() method on the Car class object
       System.out.println(car.toString());
      
       //Calling the toString() method on the Airplane class object
       System.out.println(airplane.toString());

//Comparing the speed of Car and Airplane
       if(car.getMax_speed()>airplane.getMax_speed())
           System.out.println("Car goes faster than Airplane");
       else
           System.out.println("Airplane goes faster than Car");
      
       System.out.println(car instanceof Car);
       System.out.println(airplane instanceof Airplane);

   }

}

____________________________

output:


** CAR **
Name=Ferrari
Max Speed=300
No of cylinders=12

** AIRPLANE **
Name=Boeing 747-8
Max Speed=700
No of engines=4

Airplane goes faster than Car
true
true

________________Thank You

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote