A class whose methods branch on conditions.
Combine class structure with if/else logic across multiple methods to build full behavior.
Real classes have multiple methods that make decisions: a setter that validates, a behavior method that branches by state, a calculator that handles tiers. The combination produces interesting, testable behavior.
On the AP exam, conditional-logic class FRQs test both class structure AND the if/else logic inside the methods.
Each method has its own decision structure.
Mutators validate; behaviors classify.
TrafficLight example
public class TrafficLight {
private String color; // "red", "yellow", or "green"
public TrafficLight() { color = "red"; }
public String getColor() { return color; }
public void next() {
if (color.equals("red")) color = "green";
else if (color.equals("green")) color = "yellow";
else color = "red";
}
public boolean canCross() {
return color.equals("green");
}
public int timeToWait() {
if (color.equals("green")) return 0;
else if (color.equals("yellow")) return 5;
else return 30;
}
}
Three behavior methods, three branching structures, all using the same state.
Why methods compose well
Each method does one thing: next changes state; canCross reports a yes/no; timeToWait returns a number. They share the field but stay independent. Test each in isolation.
Conditional class vocabulary.
Used in stateful class FRQs.
- Branching method — uses if/else to choose behavior.
- State-dependent — output depends on current field values.
- Boolean accessor — returns true/false based on state.
- Tiered return — different value per state.
- State transition — moving from one state to another.
How conditional classes are tested.
Each method needs its own logic check.
- Use
.equals()for String comparison. - Cover all branches. Especially the final else.
- Match return types per spec. boolean, int, String, etc.
- State changes only in mutators. Accessors don't modify.
Cycle a TrafficLight.
State transitions and behavior responses.
Setup. TrafficLight t = new TrafficLight(); — starts red.
- t.canCross() → false (color is red).
- t.timeToWait() → 30.
- t.next() → color = green.
- t.canCross() → true.
- t.next() → color = yellow.
- t.timeToWait() → 5.
- t.next() → color = red.
Branching-method pitfalls.
Mostly String comparison and missing branches.
==on Strings. Use.equals().- Missing else. Method may not return on all paths.
- Tied output for different states. Each branch should be distinct.
- Behavior method mutating state. Keep concerns separated.
Method roles.
Mutate vs query.
Conditional Class Reference
AP Quick Reference| Method type | Reads state | Writes state |
|---|---|---|
| Mutator (next) | Yes | Yes |
| Boolean query (canCross) | Yes | No |
| Numeric query (timeToWait) | Yes | No |
How to master conditional classes.
Each method gets its own if/else block.
- MCQ Practice — verify each method covers all states.
- Java Lab — build TrafficLight, ElevatorDoor, Mode classes.
- FRQ Practice — split mutators from queries; one branching block per method.
Practice — attempt these now.
AP-style assessments aligned to this lesson. Time them.
AP CSA Unit 13 Topic 04 Complete Class with Conditional Logic FRQ Practice
AP-style topic practice assessment