Write an abstract superclass encupsulating a vehicle: A vehicle has two attribut
ID: 3784457 • Letter: W
Question
Write an abstract superclass encupsulating a vehicle: A vehicle has two attributes it's owner's name and it's number of wheels. This class has two nonabstract subclasses: one encapsulating a bicycle, and the other encapsulating a motorized vehicle. A motorized vehicle has the following additional attributes: it's engine volume displacement, in liters; and a method computing and returning a measure of horsepower, the number of liters times the number of wheels. You also need to include a client class to test these two classes.
Explanation / Answer
Vehicle.java
public abstract class Vehicle {
//Declaring variables
String owner_name;
int no_of_wheels;
//Parameterized constructor
public Vehicle(String owner_name, int no_of_wheels) {
super();
this.owner_name = owner_name;
this.no_of_wheels = no_of_wheels;
}
//Setters and getters
public String getOwner_name() {
return owner_name;
}
public void setOwner_name(String owner_name) {
this.owner_name = owner_name;
}
public int getNo_of_wheels() {
return no_of_wheels;
}
public void setNo_of_wheels(int no_of_wheels) {
this.no_of_wheels = no_of_wheels;
}
//toString() method is used to display the contents of an Vehicle class object
@Override
public String toString() {
return "Owner Name=" + owner_name + " No of wheels="
+ no_of_wheels;
}
}
_________________
Bicycle.java
public class Bicycle extends Vehicle {
//Parameterized constructor
public Bicycle(String owner_name, int no_of_wheels) {
super(owner_name, no_of_wheels);
}
//toString() method is used to display the contents of an Bicycle class object
@Override
public String toString() {
System.out.println("Bicycle#");
System.out.println(super.toString());
return "";
}
}
_____________________
MotorVehicle.java
public class MotorVehicle extends Vehicle {
//Declaring variable
int engine_volume;
//Parameterized constructor
public MotorVehicle(String owner_name, int no_of_wheels, int engine_volume) {
super(owner_name, no_of_wheels);
this.engine_volume = engine_volume;
}
//getters and setters
public int getEngine_volume() {
return engine_volume;
}
public void setEngine_volume(int engine_volume) {
this.engine_volume = engine_volume;
}
//Method which calculates the house power
public double calHorsePower()
{
return engine_volume*no_of_wheels;
}
//toString() method is used to display the contents of an Motorvehicle class object
@Override
public String toString() {
System.out.println("MotorVehicle#");
System.out.println(super.toString());
return "Engine volume=" + engine_volume;
}
}
_____________________
Client.java
public class Client {
public static void main(String[] args) {
//Creating an Bicycle class object by passing the inputs as arguments
Bicycle bicycle=new Bicycle("Williams",2);
//Creating an MotarVehicle class object by passing the inputs as arguments
MotorVehicle motorCar=new MotorVehicle("Johnson",4,20);
//Displaying the contents of an Bicycle class object
System.out.println(bicycle.toString());
//Displaying the contents of an MotorVehicle class object
System.out.println(motorCar.toString());
//Displaying the horse power of the MotorVehicle
System.out.println("Hose Power of Motor car is :"+motorCar.calHorsePower());
}
}
________________
output:
Bicycle#
Owner Name=Williams
No of wheels=2
MotorVehicle#
Owner Name=Johnson
No of wheels=4
Engine volume=20
Hose Power of Motor car is :80.0
__________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.