?

AP® Computer Science A

Free Version

Upgrade subject to access all content

Difficult

Class Design When One Class uses Another Class

APCSA-XJETTN

A SportsCar is a type of Car. We would like to keep track of the car’s make, model, and miles per gallon. For a sports car, we would also like to keep track of this information as well as the engine type. When comparing a car to another car or a sports car to another sports car, we will consider them equal if the make and model are the same for both.

The following is the Car class:

public class Car
{
     private String make;
     private String model;
     private double milesPerGallon;

     public Car(String mk, String md, int dist, double mpg)
     {
          make = mk;
          model = md;
          milesPerGallon = mpg;
     }

     public void setMake(String mk)
     {
          make = mk;
     }

     public void setModel(String md)
     {
          model = md;
     } 

     public String getMake()
     {
          return make;
     }

     public String getModel()
     {
          return model;
     }

     public double getMPG()
     {
          return milesPerGallon;
     }

     public boolean equals(Object obj)
     {
          Car otherCar = (Car) obj;

          if (model.equals(otherCar.getModel()) &&;
               make.equals(otherCar.getMake()))
               return true;
          return false;
     }

     public String toString()
     {
          return make + " " + model + " gets " + milesPerGallon + "miles per gallon";
     }
}

What would be the BEST strategy for creating the SportsCar class from the information given above?

A

SportsCar should extend Car.
Define instance variables for make, model, miles per gallon, and engine type in SportsCar.
Create a constructor method that accepts model, make, miles per gallon, and engine type as parameters.
Define accessor and modifier methods for all four instance variables in SportsCar.

B

SportsCar should NOT extend Car.
Define instance variables for make, model, miles per gallon, and engine type in SportsCar.
Create a constructor method that accepts model, make, miles per gallon, and engine type as parameters.
Define accessor and modifier methods for all four instance variables in SportsCar.

C

SportsCar should extend Car.
Create a new private instance variable for engine type in SportsCar.
No new constructor method is necessary.
Create accessor and modifier methods in SportsCar for engine type.
Create an equals method in SportsCar so that SportsCars can be compared in if statements.

D

SportsCar should extend Car.
Create a new private instance variable for engine type in SportsCar.
Create a constructor method in SportsCar that accepts model, make, miles per gallon, and engine type as parameters.
Create accessor and modifier methods in SportsCar for engine type.

E

SportsCar should NOT extend Car.
Redefine the instance variables for make, model, miles per gallon in SportsCar, and also create a new private instance variable for engine type.
Create a constructor method in SportsCar that accepts model, make, miles per gallon, and engine type as parameters.
Redefine the accessor and modifier methods from Car in SportsCar.
Create accessor and modifier methods in SportsCar for engine type.