Write an abstract superclass encapsulating a vehicle: A vehicle has two attribut
ID: 3812941 • Letter: W
Question
Write an abstract superclass encapsulating a vehicle: A vehicle has two attributes: its owner's name and its number of wheels. This class has two non-abstract subclasses: one encapsulating a bicycle, and the other encapsulating a motorized vehicle. A motorized vehicle has the following additional attributes: its 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
/*
Program:
*/
//abstract superclass for encapsulating vehicle
public abstract class Vehicle {
private String ownerName;
private int numWheels;
public Vehicle() {} // default constructor
public Vehicle(String name, int wheels) { // overloaded constructor
this.ownerName = name;
this.numWheels = wheels;
}
}
//non abstract class bicycle
public class Bicycle extends Vehicle {
private String ownerName;
private int numWheels;
public Bicycle() {}
public Bicycle(String name) {
ownerName = name;
numWheels = 2;
}
public String getOwnerName() {
return this.ownerName;
}
public int getNumWheels() {
return this.numWheels;
}
}
//non abstract class Motorized vehicle
public class MotorizedVehicle extends Vehicle {
private String ownerName; // owner's name
private int numWheels; // number of wheels
private double enginevol; // engine size in litres
public MotorizedVehicle() {}
public MotorizedVehicle(String name, int wheels, double size) {
this.ownerName = name;
this.numWheels = wheels;
this.enginevol = size;
}
public String getOwnerName() {
return this.ownerName;
}
public int getNumWheels() {
return this.numWheels;
}
public double getEngineSize() {
return this.enginevol;
}
public double getHorsePower() {
return (this.enginevol * this.numWheels);
}
}
//client class to test the other two class
public class TestVehicle {
public static void main(String[] args) {
Bicycle racer = new Bicycle("Fred");
System.out.println("Owner = "+racer.getOwnerName());
System.out.println("Wheels = "+racer.getNumWheels());
MotorizedVehicle car = new MotorizedVehicle("Marilyn Monroe", 4, 1.6);
System.out.println("Vehicle owner = "+car.getOwnerName());
System.out.println("Wheels = "+car.getNumWheels());
System.out.println("Engine size (litres) = "+car.getEngineSize());
System.out.println("Horse Power = "+car.getHorsePower());
} // end main()
} // end class
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.