Scoreboard is #2 from the from the 2024 AP Computer Science A Free Response problems.

https://apcentral.collegeboard.org/media/pdf/ap24-frq-comp-sci-a.pdf

Scoreboard class

public class Scoreboard
{
    private String team1Name, team2Name;
    private int team1Points, team2Points;
    private boolean team1Active;
    
    public Scoreboard(String team1Name, String team2Name)
    {
        this.team1Name = team1Name;
        this.team2Name = team2Name;
        team1Points = 0;
        team2Points = 0;
        team1Active = true;
    }
    
    public void recordPlay(int points)
    {
        if(points != 0)
        {
            if(team1Active)
                team1Points += points;
            else
                team2Points += points;
        }
        else
        {
            team1Active = ! team1Active;
        }
    }
    
    public String getScore()
    {
        String activeTeamName = team1Name;
        if( ! team1Active )
            activeTeamName = team2Name;
        
        return team1Points + "-" + team2Points + "-" + activeTeamName;
    }
}

See Class writing order for a technique to respond to AP CS FR that request an entire class.

Java files with test code

TwoTest.java includes JUnit 5 test code with the examples from the problem. See Running JUnit 5 tests.

Scoreboard.java
TwoTest.java

2024 AP CS Exam Free Response Solutions

Help & comments

Get help from AP CS Tutor Brandon Horn

See an error? Question? Please comment below.

Comment on Scoreboard