Consider the following classes:
public class Vehicle {
private String make;
private String model;
public Vehicle(String make, String model) {
this.make = make;
this.model = model;
}
public void startEngine() {
System.out.println("Starting engine of " + make + " " + model);
}
public void accelerate() {
System.out.println("Accelerating " + make + " " + model);
}
}
public class Car extends Vehicle {
private int numDoors;
public Car(String make, String model, int numDoors) {
super(make, model);
this.numDoors = numDoors;
}
public void openDoors() {
System.out.println("Opening " + numDoors + " doors of " + getMake() + " " + getModel());
}
}
public class Motorcycle extends Vehicle {
private boolean hasSidecar;
public Motorcycle(String make, String model, boolean hasSidecar) {
super(make, model);
this.hasSidecar = hasSidecar;
}
public void popWheelie() {
System.out.println(getMake() + " " + getModel() + " popping a wheelie!");
}
}
Now, answer the following questions:
Vehicle
object with the make "Toyota" and model "Camry" using the provided classes.Car
object with the make "Honda", model "Accord", and 4 doors.Motorcycle
object with the make "Harley-Davidson", model "Iron 883", and with a sidecar.startEngine()
method on the Vehicle
object created in question 1.openDoors()
method on the Car
object created in question 2.popWheelie()
method on the Motorcycle
object created in question 3.Provide the output produced by each method call.
Vehicle
object with the make "Toyota" and model "Camry", we can use the following code:Vehicle vehicle = new Vehicle("Toyota", "Camry");
Car
object with the make "Honda", model "Accord", and 4 doors, we can use the following code:Car car = new Car("Honda", "Accord", 4);
Motorcycle
object with the make "Harley-Davidson", model "Iron 883", and with a sidecar, we can use the following code:Motorcycle motorcycle = new Motorcycle("Harley-Davidson", "Iron 883", true);
startEngine()
method on the Vehicle
object will execute the code inside the method, which prints "Starting engine of Toyota Camry". Therefore, the output is:Starting engine of Toyota Camry
openDoors()
method on the Car
object will execute the code inside the method, which prints "Opening 4 doors of Honda Accord". Therefore, the output is:Opening 4 doors of Honda Accord
popWheelie()
method on the Motorcycle
object will execute the code inside the method, which prints "Harley-Davidson Iron 883 popping a wheelie!". Therefore, the output is:Harley-Davidson Iron 883 popping a wheelie!