2025 AP® Computer Science A FRQ 3 — Round (ArrayList & Tournament Pairing)

2025 AP® Computer Science A FRQ 3 — Round (ArrayList & Objects)

2025 AP® Computer Science A FRQ 3 — Round (ArrayList & Tournament Pairing)

Topic: ArrayList initialization, object creation, tournament pairing algorithm
2025 Curriculum Alignment: Unit 4 (Data Collection — ArrayList) + Unit 3 (Class Creation)


Question

This question involves pairing competitors in a tournament into one-on-one matches for one round of the tournament. For example, in a chess tournament, the competitors are the individual chess players. A game of chess involving two players is a match. The winner of each match goes on to a match in the next round of the tournament.

Since half of the players are eliminated in each round of the tournament, there is eventually a final round consisting of one match and two competitors. The winner of that match is considered the winner of the tournament.

Competitors, matches, and rounds of the tournament are represented by the Competitor, Match, and Round classes. You will write the constructor and one method of the Round class.

Provided Classes

/** A single competitor in the tournament */
public class Competitor {
    /** The competitor's name and rank */
    private String name;
    private int rank;
    
    /**
     * Assigns n to name and initialRank to rank
     * Precondition: initialRank >= 1
     */
    public Competitor(String n, int initialRank) {
        /* implementation not shown */
    }
    
    /* There may be instance variables, constructors,
       and methods that are not shown. */
}

/** A match between two competitors */
public class Match {
    public Match(Competitor one, Competitor two) {
        /* implementation not shown */
    }
    
    /* There may be instance variables, constructors,
       and methods that are not shown. */
}

/** A single round of the tournament */
public class Round {
    /** The list of competitors participating in this round */
    private ArrayList<Competitor> competitorList;
    
    /** Initializes competitorList, as described in part (a) */
    public Round(String[] names) {
        /* to be implemented in part (a) */
    }
    
    /**
     * Creates an ArrayList of Match objects for the next round
     * of the tournament, as described in part (b)
     * Preconditions: competitorList contains at least one element.
     *                competitorList is ordered from best to worst rank.
     * Postcondition: competitorList is unchanged.
     */
    public ArrayList<Match> buildMatches() {
        /* to be implemented in part (b) */
    }
    
    /* There may be instance variables, constructors,
       and methods that are not shown. */
}

Part (a)

Write the constructor for the Round class. The constructor should initialize competitorList to contain one Competitor object for each name in the String[] names. The order in which Competitor objects appear in competitorList should be the same as the order in which they appear in names, and the rank of each competitor is based on the competitor's position in names.

Names are listed in names in order from the best-ranked competitor with rank 1 to the worst-ranked competitor with rank n, where n is the number of elements in names.

Example: Assume the following code segment is executed:

String[] players = {"Alex", "Ben", "Cara"};
Round r = new Round(players);

The following shows the contents of competitorList in r after the constructor has finished executing:

name: "Alex"
rank: 1
name: "Ben"
rank: 2
name: "Cara"
rank: 3

Complete the Round constructor.

Part (b)

Write the Round method buildMatches. This method should return a new ArrayList<Match> object by pairing competitors from competitorList according to the following rules:

  • If the number of competitors in competitorList is even, the best-ranked competitor is paired with the worst-ranked competitor, the second-best-ranked competitor is paired with the second-worst-ranked competitor, etc.
  • If the number of competitors in competitorList is odd, the competitor with the best rank is ignored and the remaining competitors are paired according to the rule for an even number of competitors.

Each pair of competitors is used to create a Match object that should be added to the ArrayList to return. Competitors may appear in either order in a Match object, and matches may appear in any order in the returned ArrayList.

Example 1: Odd number of competitors

The following shows competitorList in a Round object r1:

name: "Alex"
rank: 1
(ignored)
name: "Ben"
rank: 2
name: "Cara"
rank: 3

The ArrayList returned by r1.buildMatches() contains a single match between Ben and Cara (the second and third-ranked competitors):

Match: Ben vs. Cara

Example 2: Even number of competitors

The following shows competitorList in a Round object r2:

name: "Rei"
rank: 1
name: "Sam"
rank: 2
name: "Vi"
rank: 3
name: "Tim"
rank: 4

The ArrayList returned by r2.buildMatches() contains two matches:

Match: Rei vs. Tim
(1st vs. 4th)
Match: Sam vs. Vi
(2nd vs. 3rd)

Complete the buildMatches method.


Solution & Explanation

Part (a) Solution

public Round(String[] names) {
    competitorList = new ArrayList<Competitor>();
    
    for (int i = 0; i < names.length; i++) {
        competitorList.add(new Competitor(names[i], i + 1));
    }
}

Part (a) Explanation

Algorithm Logic:

  1. Initialize competitorList to a new empty ArrayList
  2. Loop through the names array from index 0 to the end
  3. For each name, create a new Competitor object with:
    • Name: names[i]
    • Rank: i + 1 (since rank 1 is the best, and array indices start at 0)
  4. Add each Competitor object to competitorList

Key Points:

  • Initialize the instance variable: competitorList = new ArrayList<Competitor>(); (NOT ArrayList<Competitor> competitorList = ... which would create a local variable)
  • Rank calculation: i + 1 because the first name (index 0) should have rank 1
  • Object creation: new Competitor(names[i], i + 1) creates the object with proper parameters
  • Add to ArrayList: Use .add() to append each competitor

Common Mistake: Writing ArrayList<Competitor> competitorList = ... instead of just competitorList = ... — this creates a LOCAL variable instead of initializing the INSTANCE variable.

Part (b) Solution

public ArrayList<Match> buildMatches() {
    ArrayList<Match> matches = new ArrayList<Match>();
    
    int bestIndex = competitorList.size() % 2;
    int worstIndex = competitorList.size() - 1;
    
    while (bestIndex < worstIndex) {
        matches.add(new Match(competitorList.get(bestIndex), 
                              competitorList.get(worstIndex)));
        bestIndex++;
        worstIndex--;
    }
    
    return matches;
}

Part (b) Explanation

Algorithm Logic:

  1. Create a new empty ArrayList to store matches
  2. Calculate starting index for best competitor:
    • If size is even: start at index 0
    • If size is odd: start at index 1 (skip the best-ranked player)
    • Formula: competitorList.size() % 2
  3. Set worst index to the last element: competitorList.size() - 1
  4. While the indices haven't crossed:
    • Create a match between the competitor at bestIndex and worstIndex
    • Move bestIndex forward (toward middle)
    • Move worstIndex backward (toward middle)
  5. Return the list of matches

Why size() % 2 Works:

Size size % 2 Starting Index Result
3 (odd) 1 1 Skip index 0 (best player)
4 (even) 0 0 Start at index 0 (best player)

Example Walkthrough

Even Example: ["Rei", "Sam", "Vi", "Tim"] (size = 4)

Iteration bestIndex worstIndex Match Created
Initial 0 3 -
1 0 3 Rei vs. Tim
After increment 1 2 -
2 1 2 Sam vs. Vi
After increment 2 1 Loop ends (2 < 1 is false)

Result: Two matches created ✓

Odd Example: ["Alex", "Ben", "Cara"] (size = 3)

Iteration bestIndex worstIndex Match Created
Initial 1 (skip Alex) 2 -
1 1 2 Ben vs. Cara
After increment 2 1 Loop ends (2 < 1 is false)

Result: One match created, Alex is skipped ✓


Scoring Notes

Part (a) — 4 points:

  • +1: Initializes competitorList to a new ArrayList
  • +1: Loops through all elements of names
  • +1: Creates Competitor objects with correct name and rank
  • +1: Adds competitors to competitorList in correct order

Part (b) — 5 points:

  • +1: Creates and returns an ArrayList<Match>
  • +1: Handles odd/even number of competitors correctly
  • +1: Pairs best with worst, second-best with second-worst, etc.
  • +1: Creates Match objects correctly
  • +1: Adds all matches to the returned ArrayList

Common Mistakes to Avoid:

  • Part (a): Declaring a new local variable instead of initializing the instance variable
  • Part (a): Using rank of i instead of i + 1
  • Part (b): Not handling odd number of competitors (forgetting to skip the best player)
  • Part (b): Using size() / 2 instead of size() % 2 for the starting index
  • Part (b): Modifying competitorList (violates postcondition)

College Board Resources

Contact form