2024 AP CSA FRQ 2 Solution – Scoreboard Class (Java OOP)

2024 AP CSA FRQ 2 – Scoreboard (Solution)

This solution implements the complete Scoreboard class, including instance variables, constructor, recordPlay, and getScore, consistent with the sample behavior in the prompt.

Full Scoreboard Class

public class Scoreboard
{
    /** Names of team 1 and team 2 */
    private String team1Name;
    private String team2Name;

    /** Scores of each team */
    private int team1Score;
    private int team2Score;

    /** true if team 1 is currently active; false if team 2 is currently active */
    private boolean team1Active;

    /**
     * Constructs a Scoreboard with the given team names.
     * The game starts with team 1 active and both scores 0.
     */
    public Scoreboard(String name1, String name2)
    {
        team1Name = name1;
        team2Name = name2;
        team1Score = 0;
        team2Score = 0;
        team1Active = true;    // team 1 starts as the active team
    }

    /**
     * Records the result of one play.
     * If points > 0, add points to the active team's score.
     * If points == 0, the active team's turn ends and the other team becomes active.
     */
    public void recordPlay(int points)
    {
        if (points > 0)
        {
            if (team1Active)
            {
                team1Score += points;
            }
            else
            {
                team2Score += points;
            }
        }
        else    // points == 0
        {
            // switch the active team
            team1Active = !team1Active;
        }
    }

    /**
     * Returns a String of the form "score1-score2-activeTeamName".
     */
    public String getScore()
    {
        String activeName;
        if (team1Active)
        {
            activeName = team1Name;
        }
        else
        {
            activeName = team2Name;
        }

        return team1Score + "-" + team2Score + "-" + activeName;
    }
}

Why this matches the examples:

  • Scores start at 0-0 and team 1 is active.
  • recordPlay(points > 0) adds to the active team’s score without changing who is active.
  • recordPlay(0) switches the active team without changing scores.
  • getScore() returns the exact string format shown in the sample table.
  • Multiple Scoreboard objects (like game and match) stay independent because all state is instance-based.
Common mistakes: forgetting to switch the active team only when points are 0, mixing up which team is active when updating scores, or returning the wrong string format (missing hyphens or using team numbers instead of names).

Contact form