Mutators that update state only when conditions allow.
A setter isn't always a simple assignment — sometimes the new value must pass a check before being stored.
A conditional state update is a mutator method that decides whether (or how) to change a field based on the new value, the current state, or an external input.
Common forms: reject invalid input; apply a value only if a condition is met; transform the input before storing.
On the AP exam, these methods appear in any class that needs to maintain validity or apply rules.
Check the condition; update only if it passes.
Reject, clamp, or transform — three common patterns.
Reject invalid input
public void setAge(int age) {
if (age >= 0) {
this.age = age;
}
// negative ages ignored
}
Clamp to a range
public void setVolume(int v) {
if (v < 0) v = 0;
else if (v > 100) v = 100;
this.volume = v;
}
Update based on current state
public void setTemperature(int t) {
if (t != this.temperature) { // only change if different
this.temperature = t;
}
}
Return a status
Mutators can return a boolean indicating whether the update happened:
public boolean trySetAge(int age) {
if (age >= 0 && age <= 150) {
this.age = age;
return true;
}
return false;
}
Conditional-mutator vocabulary.
Used in guarded class FRQs.
- Conditional update — change only if check passes.
- Reject — ignore invalid input.
- Clamp — restrict to a range.
- Guard — boolean check protecting an action.
- Status return — boolean reporting success.
How conditional mutators are tested.
Guard direction and silent rejection dominate.
- Guard inverts the spec. "Only if positive" →
if (x > 0). - Silent rejection. Mutator returns void; just don't change state.
- Status mutator returns boolean. When the spec asks for success/failure.
- Don't crash on bad input. Reject gracefully.
Trace conditional setAge.
Some calls succeed; some are ignored.
Setup. Person with age field; setAge rejects negatives.
- p.setAge(25) → age = 25.
- p.setAge(-5) → age stays 25.
- p.setAge(0) → age = 0 (zero allowed).
- p.setAge(200) → age = 200 (unrestricted upper bound).
Spec might add a "at most 150" check, in which case 200 would be rejected too.
Conditional-mutator pitfalls.
Most invert the guard.
- Inverted guard. Storing on failure; rejecting on success.
- Storing before checking. Invalid value sticks if rejection path doesn't reset.
- Throwing exceptions when spec wants silent reject.
- Returning the field from a void mutator. Doesn't compile.
Conditional patterns.
Three forms.
Conditional Mutator Reference
AP Quick Reference| Form | Body | Returns |
|---|---|---|
| Reject invalid | if valid, assign | void |
| Clamp | Restrict to range | void |
| Status report | if valid, assign + return true | boolean |
How to master conditional updates.
Check first; update second.
- MCQ Practice — verify the check happens before the assignment.
- Java Lab — write setAge, setVolume, setSpeed. Test rejection and clamping.
- FRQ Practice — read spec for "if valid" or "only when" — these signal conditional logic.
Practice — attempt these now.
AP-style assessments aligned to this lesson. Time them.
AP CSA Unit 13 Topic 03 Conditional State Updates FRQ Practice
AP-style topic practice assessment