콘텐츠로 이동

Unit 3 Mini Project: Racing Garage

Name: ________

Time: 20 minutes

Create a small Java program that tracks performance upgrades for one race car. Complete the RacingGarage class below, then run Main to test it.

Requirements

  • Use the provided constructor to initialize both instance variables.
  • Complete the void method addHorsepower so it adds the argument to currentHorsepower.
  • Complete the three non-void methods with the correct return statements:
  • int getHorsepower()
  • boolean reachedTarget()
  • String getSummary()
  • Do not change the method headers or the code in Main.

Skeleton Code

class RacingGarage {
    private String carName;
    private int targetHorsepower;
    private int currentHorsepower;

    // Constructor: initialize carName and targetHorsepower.
    public RacingGarage(String carName, int targetHorsepower) {
        // TODO
    }

    // Mutator: add horsepower from a completed upgrade.
    public void addHorsepower(int horsepower) {
        // TODO
    }

    // Accessor: return the car's current horsepower.
    public int getHorsepower() {
        // TODO
        return 0;
    }

    // Return true when the car has reached its target horsepower.
    public boolean reachedTarget() {
        // TODO
        return false;
    }

    // Return one sentence describing this race car.
    public String getSummary() {
        // TODO
        return "";
    }
}

public class Main {
    public static void main(String[] args) {
        RacingGarage garage = new RacingGarage("Falcon GT", 500);

        garage.addHorsepower(220);
        garage.addHorsepower(310);

        System.out.println(garage.getHorsepower());
        System.out.println(garage.reachedTarget());
        System.out.println(garage.getSummary());
    }
}

Expected Output

530
true
Falcon GT: 530 / 500 HP

Before You Submit

  • Your constructor assigns values using this.carName and this.targetHorsepower.
  • addHorsepower changes the object's state and has no return value.
  • Each non-void method returns a value with the declared type.

Reference: Runestone Academy, CSAwesome2 Unit 3: Class Creation.