Question 171
A downtown parking garage charges a flat rate per hour for each car that parks. The garage has a fixed number of spaces. When a car enters, it occupies one space. When a car exits, the system calculates the charge based on the number of hours parked and adds it to the total revenue. A car cannot enter if no spaces are available. The following table shows an example sequence of operations on a ParkingGarage object.
| STATEMENTS AND EXPRESSIONS | VALUE RETURNED | COMMENT |
|---|---|---|
ParkingGarage g = new ParkingGarage(3, 5); | Three spaces, $5 per hour | |
g.getOpenSpaces() | 3 | |
g.enter() | true | Car enters successfully |
g.enter() | true | Second car enters |
g.getOpenSpaces() | 1 | |
g.enter() | true | Third car enters |
g.enter() | false | Garage is full |
g.exit(3) | 15 | Car parked 3 hours: 3 × 5 = 15 |
g.getOpenSpaces() | 1 | One space freed |
g.exit(1) | 5 | Car parked 1 hour: 1 × 5 = 5 |
g.getRevenue() | 20 | Total revenue: 15 + 5 |
Write the complete ParkingGarage class, including the constructor and any required instance variables and methods. Your implementation must meet all specifications and conform to the example.
public class ParkingGarage {Main Solution
Below is a complete, AP-aligned implementation of the ParkingGarage class.
public class ParkingGarage {
private int totalSpaces; // total number of spaces in the garage
private int openSpaces; // number of currently available spaces
private int hourlyRate; // charge per hour in dollars
private int totalRevenue; // cumulative revenue collected
/**
* Constructs a ParkingGarage with the given number of spaces
* and the given hourly rate.
*
* @param spaces the fixed total number of parking spaces
* @param ratePerHour the charge in dollars per hour
*/
public ParkingGarage(int spaces, int ratePerHour) {
totalSpaces = spaces;
openSpaces = spaces; // initially all spaces are open
hourlyRate = ratePerHour;
totalRevenue = 0;
}
/**
* Returns the number of currently open (available) spaces.
*
* @return number of open spaces
*/
public int getOpenSpaces() {
return openSpaces;
}
/**
* Attempts to admit a car into the garage.
* If at least one space is open, occupies one space and returns true.
* If the garage is full, returns false without changing state.
*
* @return true if the car successfully entered, false otherwise
*/
public boolean enter() {
if (openSpaces > 0) {
openSpaces--;
return true;
}
return false;
}
/**
* Processes a departing car that was parked for the given number of hours.
* Calculates the charge (hours * hourlyRate), adds it to total revenue,
* frees one space, and returns the charge.
*
* @param hours the number of hours the car was parked
* @return the charge for this car's stay
*/
public int exit(int hours) {
int charge = hours * hourlyRate;
totalRevenue += charge;
openSpaces++; // one space is freed when a car exits
return charge;
}
/**
* Returns the total revenue collected so far.
*
* @return cumulative revenue in dollars
*/
public int getRevenue() {
return totalRevenue;
}
}
Trace through the example to verify correctness:
| Statement | Expected | Result with implementation |
|---|---|---|
new ParkingGarage(3, 5) | — | totalSpaces=3, openSpaces=3, hourlyRate=5, totalRevenue=0 |
g.getOpenSpaces() | 3 | openSpaces=3 ✓ |
g.enter() | true | openSpaces becomes 2, returns true ✓ |
g.enter() | true | openSpaces becomes 1, returns true ✓ |
g.getOpenSpaces() | 1 | openSpaces=1 ✓ |
g.enter() | true | openSpaces becomes 0, returns true ✓ |
g.enter() | false | openSpaces=0, returns false ✓ |
g.exit(3) | 15 | charge=3×5=15, totalRevenue=15, openSpaces=1, returns 15 ✓ |
g.getOpenSpaces() | 1 | openSpaces=1 ✓ |
g.exit(1) | 5 | charge=1×5=5, totalRevenue=20, openSpaces=2, returns 5 ✓ |
g.getRevenue() | 20 | totalRevenue=20 ✓ |
Teacher Explanation
This FRQ tests students' ability to design and implement a complete Java class from a behavioral specification. Key concepts assessed:
- Instance variables: Students must identify the four pieces of state needed — total spaces, open spaces, hourly rate, and accumulated revenue.
- Constructor: Must initialize all instance variables;
openSpacesstarts equal tototalSpaces. - enter(): Guard condition (
openSpaces > 0) before decrementing; returns boolean. - exit(int hours): Multiplies hours by rate, accumulates revenue, increments
openSpaces, returns the charge. - getOpenSpaces() / getRevenue(): Simple accessor methods.
Award full credit for any implementation that produces the correct values shown in the table. Minor style issues (e.g., using double instead of int) should not be penalized if the logic is correct.
Education Coach Advice
AP Exam Strategy for class-design FRQs:
- Read the table of operations carefully — each row tells you exactly what method signature and return type you need.
- List all instance variables first before writing any methods; ask yourself "what state does this object need to remember?"
- Trace through the example table with your implementation before finalizing — it is the built-in test case.
- Keep methods short and focused; each method should do exactly one thing.
- You earn partial credit for each correct method, so write all methods even if one is incomplete.
Traps
Common pitfalls on this FRQ:
- Forgetting to increment openSpaces in exit(): Students often calculate and return the charge correctly but forget to free the space, causing
getOpenSpaces()to return the wrong value afterward. - Not accumulating revenue: Returning
hours * hourlyRatewithout adding it tototalRevenuemeansgetRevenue()always returns 0. - Initializing openSpaces to 0: Students may forget that all spaces start open, so
openSpacesshould be initialized tototalSpaces, not 0. - enter() not returning false when full: Omitting the guard condition or always returning true fails the "Garage is full" case.
- Missing instance variables: Storing hourly rate or revenue only as local variables instead of fields causes them to be lost between method calls.

