01
Overview

Updating state with bounds clamping.

When a class field must stay within a valid range, mutator methods clamp the new value before storing it.

An instance variable often has logical bounds: health between 0 and 100, volume between 0 and 11, position between 0 and length - 1. When a mutator receives a value that would go out of range, the class clamps it instead of storing the invalid value.

The clamp pattern: if too low, set to the minimum; if too high, set to the maximum; otherwise store as-is. This guards the class's invariants from misuse.

On the AP exam, clamping shows up in mutator FRQs that say "if the new value is greater than 100, set it to 100" or similar.

02
Core Concept

Bound, then store.

Two if statements (or Math.max/min) restrict the value to the legal range.

Class with bounded health

public class Player {
    private int health;
    private static final int MIN_HEALTH = 0;
    private static final int MAX_HEALTH = 100;

    public Player() { health = MAX_HEALTH; }

    public void setHealth(int newHealth) {
        if (newHealth < MIN_HEALTH) {
            health = MIN_HEALTH;
        } else if (newHealth > MAX_HEALTH) {
            health = MAX_HEALTH;
        } else {
            health = newHealth;
        }
    }
}

Compact form with Math

public void setHealth(int newHealth) {
    health = Math.max(MIN_HEALTH, Math.min(MAX_HEALTH, newHealth));
}

The inner Math.min caps at the maximum; the outer Math.max raises to the minimum. Equivalent to the if/else version.

Relative updates (damage and heal)

public void takeDamage(int amount) {
    setHealth(health - amount);   // clamp via setter
}

public void heal(int amount) {
    setHealth(health + amount);
}

Delegating to the setter keeps the clamp logic in one place — DRY and easier to maintain.

When to clamp vs reject

  • Clamp: bring out-of-range values into range silently.
  • Reject: ignore the call entirely if the value is invalid.
  • Pick based on the prompt's wording. "If health would go negative, set it to 0" is clamp. "Reject invalid health updates" is reject.
03
Key Vocabulary

Clamping vocabulary.

Used in state-management problems.

Vocabulary
  • Clamp — restrict a value to a range.
  • Floor — minimum allowed value.
  • Ceiling — maximum allowed value.
  • Mutator — method that changes state.
  • Invariant — property the class always maintains.
  • Delegation — one mutator calls another to share logic.
04
AP Exam Focus

How clamping mutators are tested.

Boundary handling and delegation dominate.

Exam Focus
  • Both ends clamped. Not just the maximum.
  • Boundary inclusion. Usually inclusive — 100 stays 100.
  • Relative updates use the setter. Avoids duplicate clamp code.
  • Don't store invalid values temporarily. Clamp before assignment.

MCQ patterns: trace health after several damage/heal calls that would overflow.

05
Worked Example

Trace damage and heal on a Player.

Clamp at both ends.

Worked Example AP-style reasoning

Setup. Player p = new Player(); — health starts at 100.

Operations:

  • p.takeDamage(30): setHealth(70). 70 in range → health = 70.
  • p.takeDamage(100): setHealth(-30). -30 < 0 → health = 0.
  • p.heal(50): setHealth(50). 50 in range → health = 50.
  • p.heal(200): setHealth(250). 250 > 100 → health = 100.
  • p.takeDamage(0): setHealth(100). In range → health = 100 (no change).

Why this matters. The clamp absorbs over-damage and over-heal cleanly. Health never goes negative or above 100, regardless of the size of the operation.

06
Common Mistakes

Clamping pitfalls.

Most miss one boundary or duplicate logic.

Watch Out
  • Clamping only the maximum. Health can still go negative.
  • Clamping only the minimum. Over-healing exceeds the cap.
  • Storing first, clamping second. Temporarily invalid state.
  • Duplicate clamp in takeDamage and heal. Delegate to setHealth instead.
  • Strict vs non-strict comparison. Usually inclusive — 100 should stay 100.
07
Reference Table

Clamping forms at a glance.

Two ways to write the same logic.

Clamping Reference

AP Quick Reference
FormCodeNote
if/elseThree branchesVerbose but clear.
Math nestedMath.max(min, Math.min(max, v))One line.
Delegate to setterRelative ops call setterDRY.
Boundary inclusionInclusive default</> not <=/>=.
08
Practice Tip

How to master clamping.

Clamp at both ends; delegate from relative methods.

Practice Strategy

Use the three practice tools below this lesson in order:

  • MCQ Practice — for each operation, check whether the value would over- or underflow before tracing.
  • Java Lab — build a Player with health clamped to [0, 100]. Test boundary cases — exactly 0, exactly 100, and over both.
  • FRQ Practice — write setHealth once with full clamping; delegate takeDamage and heal to it.

If your reflex is "clamp at both ends" for any bounded field, this pattern is permanent.

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 10 Topic 02 State Updates with Bounds Clamping FRQ Practice

AP-style topic practice assessment

Start