Post

Created by @nathanedwards
 at October 31st 2023, 7:23:49 pm.

AP Computer Science Exam Question

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:

  1. Create a Vehicle object with the make "Toyota" and model "Camry" using the provided classes.
  2. Create a Car object with the make "Honda", model "Accord", and 4 doors.
  3. Create a Motorcycle object with the make "Harley-Davidson", model "Iron 883", and with a sidecar.
  4. Call the startEngine() method on the Vehicle object created in question 1.
  5. Call the openDoors() method on the Car object created in question 2.
  6. Call the popWheelie() method on the Motorcycle object created in question 3.

Provide the output produced by each method call.

Answer

  1. To create a Vehicle object with the make "Toyota" and model "Camry", we can use the following code:
Vehicle vehicle = new Vehicle("Toyota", "Camry");
  1. To create a Car object with the make "Honda", model "Accord", and 4 doors, we can use the following code:
Car car = new Car("Honda", "Accord", 4);
  1. To create a 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);
  1. Calling the 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
  1. Calling the 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
  1. Calling the 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!