Mutators that respect a capacity limit.
When a collection or counter has a fixed maximum, every add or increment must check before applying.
A capacity limit is the maximum size or value a collection or counter can hold. The class's mutators must guard each addition: if at capacity, refuse; otherwise add.
Examples: a parking lot with N spots; a sign-up sheet with limited slots; a counter that can't exceed some peak.
On the AP exam, capacity-limited classes appear in parking lot, theater seating, and form FRQs.
Check capacity; add only if room exists.
Status-returning mutator is common.
ParkingLot class
public class ParkingLot {
private int capacity;
private int parked;
public ParkingLot(int capacity) {
this.capacity = capacity;
this.parked = 0;
}
public boolean park() {
if (parked < capacity) {
parked++;
return true;
}
return false;
}
public boolean leave() {
if (parked > 0) {
parked--;
return true;
}
return false;
}
public boolean isFull() { return parked == capacity; }
public boolean isEmpty() { return parked == 0; }
public int spotsAvailable() { return capacity - parked; }
}
Status return
The boolean return tells the caller whether the operation succeeded. true if a car parked; false if the lot was full.
Symmetric methods
park guards against the upper limit; leave guards against the lower limit (0). Both follow the same shape.
Capacity vocabulary.
Used in bounded-collection classes.
- Capacity — maximum allowed.
- Available — capacity - current.
- Full — current equals capacity.
- Empty — current is 0.
- Status mutator — boolean-returning success report.
How capacity classes are tested.
Guard direction and status return dominate.
- Guard before incrementing. Don't exceed capacity.
- Guard before decrementing. Don't go negative.
- Return status. boolean success/failure.
- Available = capacity - current. Standard derived value.
Park and leave from a 3-spot lot.
Track parked count.
Setup. ParkingLot lot = new ParkingLot(3);
- lot.park() → true; parked=1.
- lot.park() → true; parked=2.
- lot.park() → true; parked=3 (full).
- lot.park() → false; parked stays 3.
- lot.leave() → true; parked=2.
- lot.spotsAvailable() → 1.
Capacity-class pitfalls.
Most go off-by-one.
- Off-by-one in capacity check.
<vs<=. - Incrementing before guard. Exceeds capacity then maybe undoes.
- Forgetting empty check on leave. Counter goes negative.
- Returning void from a status method. Caller can't tell if it succeeded.
Capacity-method shapes.
Add vs remove.
Capacity Reference
AP Quick Reference| Method | Guard | Action |
|---|---|---|
| add | current < capacity | current++ |
| remove | current > 0 | current-- |
| isFull | — | current == capacity |
| isEmpty | — | current == 0 |
| available | — | capacity - current |
How to master capacity-limited classes.
Guard before; return boolean.
- MCQ Practice — verify boundary conditions; check that guards use the right operator.
- Java Lab — build ParkingLot, Theater, Queue. Test at capacity and empty.
- FRQ Practice — boolean return; guard before increment.
Practice — attempt these now.
AP-style assessments aligned to this lesson. Time them.
AP CSA Unit 13 Topic 07 Capacity-Limited State Updates FRQ Practice
AP-style topic practice assessment