A class that tracks counts and reports ratios.
Wins and losses, hits and misses, attempts and successes — when the meaningful number is the ratio, the class must compute it correctly.
A ratio compares two counts. A class that tracks both can report their ratio as a percentage or as a decimal fraction.
Two pitfalls drive the design: integer division truncates without a cast, and division by zero crashes when no events have occurred yet.
On the AP exam, ratio-tracking classes appear in sports stats, quiz scoring, and survey FRQs.
Track two counts; cast for the ratio; guard zero.
Three rules.
WinLossTracker class
public class WinLossTracker {
private int wins;
private int losses;
public WinLossTracker() {
wins = 0;
losses = 0;
}
public void recordWin() { wins++; }
public void recordLoss() { losses++; }
public int totalGames() { return wins + losses; }
public double winRate() {
int total = totalGames();
if (total == 0) return 0.0;
return (double) wins / total;
}
public double winPercentage() {
return winRate() * 100;
}
public boolean isWinning() {
return wins > losses;
}
}
Why cast to double
Without the cast, wins / total truncates to 0 for any winRate less than 1. Casting promotes the division to double arithmetic.
Empty case
When no games have been recorded, the win rate is technically undefined. Most specs say return 0.0 (or some other sentinel).
Ratio vocabulary.
Used in stat-tracking classes.
- Ratio — one count divided by another.
- Percentage — ratio × 100.
- Total — sum of both counts.
- Cast — converting int to double for accurate division.
- Empty case — division by zero protection.
How ratio classes are tested.
Cast and zero-guard dominate.
- Cast for accurate division. Otherwise truncates.
- Zero-guard before divide. Avoid crash.
- Total = sum of counts. Helper method.
- Return double for ratio. Decimal value.
Track a season.
Wins, losses, percentage.
- tracker.recordWin(); .recordWin(); .recordLoss(); .recordWin();
- wins=3, losses=1.
- totalGames() → 4.
- winRate() → 3.0 / 4 = 0.75.
- winPercentage() → 75.0.
- isWinning() → true.
Empty case: new tracker, winRate() → 0.0 (zero guard).
Ratio-class pitfalls.
Cast and zero-guard.
- No cast. 3/4 in int math is 0.
- No zero guard. Division by zero crash.
- Cast in wrong place. Must promote before the divide.
- Returning int when double expected.
Ratio computation steps.
Four parts.
Ratio Reference
AP Quick Reference| Step | Code | Purpose |
|---|---|---|
| Total | wins + losses | Denominator. |
| Zero guard | if (total == 0) return 0.0 | Avoid crash. |
| Cast | (double) wins / total | Force double. |
| Scale | × 100 | For percent. |
How to master ratio classes.
Cast; zero-guard; scale.
- MCQ Practice — for each ratio expression, verify cast position and zero guard.
- Java Lab — build WinLossTracker, QuizScorer. Test empty case.
- FRQ Practice — always cast before dividing; always guard zero.
Practice — attempt these now.
AP-style assessments aligned to this lesson. Time them.
AP CSA Unit 13 Topic 10 State Tracking with Ratio Comparison FRQ Practice
AP-style topic practice assessment