Banking-style operations with guards.
Deposit must reject negative amounts; withdraw must reject overdrafts. Both follow the same guarded-mutator shape.
The BankAccount FRQ is one of the most common AP class designs. The class holds a balance; deposits add to it; withdrawals subtract from it. Both methods guard against invalid input — and return a boolean indicating whether the transaction succeeded.
On the AP exam, this design appears in BankAccount, Wallet, and ResourceTracker FRQs.
Validate amount; check balance; update.
Three guards plus a status return.
BankAccount class
public class BankAccount {
private double balance;
public BankAccount(double startingBalance) {
balance = startingBalance;
}
public boolean deposit(double amount) {
if (amount <= 0) return false;
balance += amount;
return true;
}
public boolean withdraw(double amount) {
if (amount <= 0) return false;
if (amount > balance) return false;
balance -= amount;
return true;
}
public double getBalance() { return balance; }
}
Why two checks on withdraw
- Amount must be positive (no "withdrawing" negative money).
- Amount must not exceed balance (no overdraft).
Order of checks
Cheap, simple guards first (amount <= 0); then the more contextual one (amount > balance). Both must pass before the balance updates.
Banking vocabulary.
Used in financial-style FRQs.
- Deposit — add to the balance.
- Withdraw — subtract from the balance.
- Overdraft — withdrawal that exceeds balance.
- Guard — check that protects state.
- Status return — boolean reporting success.
How banking classes are tested.
Both guards plus status return.
- Reject non-positive amounts. Both methods.
- Reject overdrafts. Withdraw only.
- Return boolean. True on success.
- Update balance only after all checks pass.
Trace transactions.
Successes and rejections.
Setup. BankAccount acc = new BankAccount(100);
- acc.deposit(50) → true; balance=150.
- acc.deposit(-20) → false; balance stays 150.
- acc.withdraw(30) → true; balance=120.
- acc.withdraw(200) → false (overdraft); balance stays 120.
- acc.withdraw(0) → false (non-positive).
Banking-class pitfalls.
Missing guards or wrong order.
- No positive check on deposit. Negative deposit reduces balance.
- No overdraft check. Balance goes negative.
- Updating before checking. Invalid state on rejection.
- Returning void instead of boolean. Caller can't tell.
Banking method shape.
Guards then action.
Banking Reference
AP Quick Reference| Method | Guards | Action |
|---|---|---|
| deposit | amount > 0 | balance += amount |
| withdraw | amount > 0; amount <= balance | balance -= amount |
| Return | boolean | true on success. |
How to master banking classes.
Two guards on withdraw; one on deposit.
- MCQ Practice — for each option, verify both guards on withdraw.
- Java Lab — build BankAccount; test negative deposits and overdrafts.
- FRQ Practice — checks first, action second, return true at the end.
Practice — attempt these now.
AP-style assessments aligned to this lesson. Time them.
AP CSA Unit 13 Topic 12 Guarded Deposit and Withdraw FRQ Practice
AP-style topic practice assessment