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

2024-06-27 comment

Anonymous

Former teacher do problems for fun each year. Used arrays and a teamIndex to reduce decisions required. Note in your solution if around scores is unnecessary. if points is 0 you can just add it, condition is only needed for changing active team.

public class Scoreboard{
    String[] teams;
    int[] scores;
    int currentTeam;

    public Scoreboard(String team1, String team2){
        teams = new String[2];
        scores = new int[2];
        currentTeam = 0;
        teams[0] = team1;
        teams[1] = team2;
    }

    public void recordPlay(int points){
        scores[currentTeam] += points;
        if(points == 0){
            currentTeam = (currentTeam + 1) % 2;
        }
    }

    public String getScore(){
        return scores[0] + "-" + scores[1] + "-" + teams[currentTeam];
    }
}

Note: The class in the comment above has been edited for formatting and the name of recordPlay has been corrected to avoid reader confusion.

Response

Skylit has something very similar to this as one of their alternate solutions. The solution above is missing the private modifier for the instance variables, which often makes the College Board cranky.

This usage of an array here is clearly appropriate. In general, I prefer to avoid introducing collections into this question unless clearly required (as in RandomStringChooser). The issue is that the collection can easily overcomplicate the solution (see the alternate solution to StepTracker). Parallel arrays also make me cringe.