01
Overview

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.

02
Core Concept

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.

03
Key Vocabulary

Capacity vocabulary.

Used in bounded-collection classes.

Vocabulary
  • Capacity — maximum allowed.
  • Available — capacity - current.
  • Full — current equals capacity.
  • Empty — current is 0.
  • Status mutator — boolean-returning success report.
04
AP Exam Focus

How capacity classes are tested.

Guard direction and status return dominate.

Exam Focus
  • 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.
05
Worked Example

Park and leave from a 3-spot lot.

Track parked count.

Worked Example AP-style reasoning

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.
06
Common Mistakes

Capacity-class pitfalls.

Most go off-by-one.

Watch Out
  • 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.
07
Reference Table

Capacity-method shapes.

Add vs remove.

Capacity Reference

AP Quick Reference
MethodGuardAction
addcurrent < capacitycurrent++
removecurrent > 0current--
isFullcurrent == capacity
isEmptycurrent == 0
availablecapacity - current
08
Practice Tip

How to master capacity-limited classes.

Guard before; return boolean.

Practice Strategy
  • 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.
09
Section · 09

Practice — attempt these now.

AP-style assessments aligned to this lesson. Time them.

FRQ Practice 30 min 18 marks Pending

AP CSA Unit 13 Topic 07 Capacity-Limited State Updates FRQ Practice

AP-style topic practice assessment

Start