01
Overview

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.

02
Core Concept

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).

03
Key Vocabulary

Ratio vocabulary.

Used in stat-tracking classes.

Vocabulary
  • 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.
04
AP Exam Focus

How ratio classes are tested.

Cast and zero-guard dominate.

Exam Focus
  • Cast for accurate division. Otherwise truncates.
  • Zero-guard before divide. Avoid crash.
  • Total = sum of counts. Helper method.
  • Return double for ratio. Decimal value.
05
Worked Example

Track a season.

Wins, losses, percentage.

Worked Example AP-style reasoning
  • 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).

06
Common Mistakes

Ratio-class pitfalls.

Cast and zero-guard.

Watch Out
  • 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.
07
Reference Table

Ratio computation steps.

Four parts.

Ratio Reference

AP Quick Reference
StepCodePurpose
Totalwins + lossesDenominator.
Zero guardif (total == 0) return 0.0Avoid crash.
Cast(double) wins / totalForce double.
Scale× 100For percent.
08
Practice Tip

How to master ratio classes.

Cast; zero-guard; scale.

Practice Strategy
  • 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.
09
Section · 09

Practice — attempt these now.

AP-style assessments aligned to this lesson. Time them.

FRQ Practice 30 min 19 marks Pending

AP CSA Unit 13 Topic 10 State Tracking with Ratio Comparison FRQ Practice

AP-style topic practice assessment

Start