Java Help with Inheritance 1- Use inheritance to implement the following classes
ID: 3662973 • Letter: J
Question
Java Help with Inheritance 1- 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 called the number_of_cylinders in its engine. There should be 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 called the number_of_engines it has. There should be 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: Write a VehicleDemo.java class that does the following: 1- Creates an instance of a Car and Airplane classes. 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.
Explanation / Answer
/*The java Vehicle that sets the name and max speed
* and have methods to set and get methods of
* instance variables.*/
//Vehicle.java
public class Vehicle
{
private String name;
private double maxSpeed;
//default constructor
public Vehicle()
{
name="unknown";
maxSpeed=0;
}
//Parameterized constructor
public Vehicle(String name, double maxSpeed)
{
setName(name);
setMaxSpeed(maxSpeed);
}
//Set name
public void setName(String name)
{
this.name=name;
}
//Set max speed
public void setMaxSpeed(double maxSpeed)
{
this.maxSpeed=maxSpeed;
}
//Returns name
public String getName()
{
return name;
}
//Returns max speed
public double getMaxSpeed()
{
return maxSpeed;
}
//Override toString method
@Override
public String toString() {
return "Name : "+name+" Max Speed : "+maxSpeed;
}
}
----------------------------------------------------------------------------------------------------------------------------
//Car.java
//The class Car extends the vehicle class inherits the
//properties from vehicle class
public class Car extends Vehicle
{
//instance variable
private int number_of_cylinders;
//Parameterized constructor
public Car()
{
super("unknown", 0);
setCylinders(0);
}
//Parameterized constructor
public Car(String name, double maxSpeed,int number_of_cylinders)
{
super(name, maxSpeed);
setCylinders(number_of_cylinders);
}
//Set number of cylinders
public void setCylinders(int number_of_cylinders)
{
this.number_of_cylinders=number_of_cylinders;
}
//Returns number of cylinders
public int getCylinders()
{
return number_of_cylinders;
}
//Override the toString method
@Override
public String toString()
{
return super.toString()+" Number of Cyliders : "+number_of_cylinders;
}
}
------------------------------------------------------------------------------------------------------------------
//Airplane.java
//The class Airplane that extends the Vehicle
//that inherits the properties from the vehicle
public class Airplane extends Vehicle
{
//instance variable
private int number_of_engines;
//Parameterized constructor
public Airplane()
{
super("unknown", 0);
setEngines(0);
}
//Parameterized constructor
public Airplane(String name, double maxSpeed,int number_of_engines)
{
super(name, maxSpeed);
setEngines(number_of_engines);
}
//Set number of cylinders
public void setEngines(int number_of_engines)
{
this.number_of_engines=number_of_engines;
}
//Returns number of cylinders
public int getEngines()
{
return number_of_engines;
}
//Override the toString method
@Override
public String toString()
{
return super.toString()
+" Number of Engines : "+number_of_engines;
}
}
------------------------------------------------------------------------------------------------------------------
//Tester java program
//VehicleDemo.java
public class VehicleDemo
{
public static void main(String[] args)
{
//Create an instance of Car
Car car=new Car();
car.setName("Benz");
car.setMaxSpeed(250);
car.setCylinders(4);
//Create an instance of Airplane
Airplane airplane=new Airplane();
airplane.setName("Benz");
airplane.setMaxSpeed(500);
airplane.setEngines(5);
if(car.getMaxSpeed()>airplane.getMaxSpeed())
System.out.println("Car speed is more than airplane speed");
else
System.out.println("Airplane speed is more than car speed");
System.out.println("Car details");
System.out.println(car.toString());
System.out.println("Airplane details");
System.out.println(airplane.toString());
}//end of the main method
}//end of the class
------------------------------------------------------------------------------------------------------------------
Sample Output:
Airplane speed is more than car speed
Car details
Name : Benz Max Speed : 250.0 Number of Cyliders : 4
Airplane details
Name : Benz Max Speed : 500.0 Number of Engines : 5
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.