The TokenPass problem from the 2013 AP Computer Science Exam is typical of free response problems that test arrays.

TokenPass is #2 from the from the 2013 AP Computer Science A Free Response problems.

https://secure-media.collegeboard.org/digitalServices/pdf/ap/apcentral/ap13_frq_comp_sci.pdf

Part (a) TokenPass constructor

public TokenPass(int playerCount)
{
    board = new int[playerCount];

    for(int i = 0; i < board.length; i++)
        board[i] = (int) (Math.random() * 10) + 1;

    currentPlayer = (int) (Math.random() * board.length);
}

A regular for loop through board is necessary for initialization of each element. An enhanced for loop won’t work since changes will not affect the array itself. See Enhanced for loops for additional discussion.

Math.random returns a value in the range of 0.0 <= board[i] < 1.0. The steps to convert this to the desired range of 1 <= board[i] <= 10 are below.

Statement Range
board[i] = Math.random(); 0.0 <= board[i] < 1.0
board[i] = Math.random() * 10; 0.0 <= board[i] < 10.0
board[i] = (int) (Math.random() * 10); 0 <= board[i] <= 9
board[i] = (int) (Math.random() * 10) + 1; 1 <= board[i] <= 10

currentPlayer must be set to a random valid index in board. This is a very common task that can be accomplished by multiplying the return value of Math.random by the length/size of the array/list and casting the result to an int.

For more on working with Math.random() see Generating numbers with Math.random().

Part (b) distributeCurrentPlayerTokens method

public void distributeCurrentPlayerTokens()
{
    int tokens = board[currentPlayer];
    board[currentPlayer] = 0;
    int targetIndex = currentPlayer + 1;

    while(tokens > 0)
    {
        if(targetIndex == board.length)
            targetIndex = 0;

        board[targetIndex]++;
        tokens--;
        targetIndex++;
    }
}

This problem requires traversal of an array with modification of each element, which precludes an enhanced for loop. This solution loops while there is at least one token remaining to distribute. A for loop that runs for the number of tokens is also possible.

There are 2 common approaches to ensuring that targetIndex remains a valid index in board. The solution above explicitly checks for validity and resets targetIndex to 0 when it exceeds the array bounds.

An alternative is to use modular division (%) to constrain targetIndex. See Division operations for more discussion of using % to constrain values.

It is important that the original value of board[currentPlayer] is used as the number of tokens to distribute. board[currentPlayer] can be modified during token distribution.

Part (b) distributeCurrentPlayerTokens method (alternate solution)

public void distributeCurrentPlayerTokens()
{
	int tokens = board[currentPlayer];
	board[currentPlayer] = 0;
    int targetIndex = currentPlayer + 1;
    
    for(int t = 1; t <= tokens; t++)
    {
    	board[targetIndex % board.length]++;
    	targetIndex++;
    }
}

2013 AP CS Exam Free Response Solutions

Help & comments

Get help from AP CS Tutor Brandon Horn

Comment on TokenPass