# 6.4.6. Free Response - Number Cube A¶

The following is a free response question from 2009. It was question 1 on the exam. You can see all the free response questions from past exams at https://apstudent.collegeboard.org/apcourse/ap-computer-science-a/exam-practice.

**Question 1.** A statistician is studying sequences of numbers obtained by repeatedly tossing a six-sided number cube. On each side of the number cube is a single number in the range of 1 to 6, inclusive, and no number is repeated on the cube. The statistician is particularly interested in runs of numbers. A run occurs when two or more consecutive tosses of the cube produce the same value. For example, in the following sequence of cube tosses, there are runs starting at positions 1, 6, 12, and 14.

```
public class NumberCube
{
/** @return an integer value between 1 and 6, inclusive
*/
public int toss()
{ /* implementation not shown */ }
// There may be instance variables, constructors, and methods not shown.
}
```

**Part a.** Write the method `getCubeTosses`

that takes a number cube and a number of tosses as parameters. The
method should return an array of the values produced by tossing the number cube the given number of times.

## 6.4.6.1. How to Solve¶

You will need to create an array to hold the results of each cube toss. The size of the array should be the passed number of times you will call `toss`

. You will need to loop that number of times and each time set the value of the array at that index to the result of the `toss`

. Return the array.

- (int) (Math.random() * 6) + 1)
- This expression correctly generates a random number between 1 and 6.
- (int) (Math.random() * 6)
- This expression generates a random number from 0 to 5.
- Math.random(6);
- This isn't valid

6-4-6-1: Which Java expression correctly generates a random number between 1 and 6?

- int[] tossArray = new int[];
- You need to specify the size of the array when you create it.
- int[] tossArray = new int(numTosses);
- It should be new int[numTosses].
- int[] tossArray = new int[numTosses];
- This will create an array of size numTosses.

6-4-6-2: Which of the following correctly creates an array of size numTosses?

- for (int i = 0; i <= numTosses; i++)
- This will execute numTosses + 1 times.
- for (int i = 1; i < numTosses; i++)
- This will execute numTosses - 1 times.
- for (int i = 0; i < numTosses; i++)
- This will execute numTosses times.

6-4-6-3: Which of the following correctly loops numTosses number of times?

## 6.4.6.2. Mixed Up Code¶

```
6-4-6-4: The method getCubeTosses below contains the correct code for one solution to this problem, but it is mixed up. Drag the needed code from the left to the right and put them in order with the correct indention so that the code would work correctly.public static int[] getCubeTosses(NumberCube cube,
int numTosses)
{
---
int[] cubeTosses = new int[numTosses];
---
for (int i = 0; i < numTosses; i++)
{
---
cubeTosses[i] = cube.toss();
---
} // end for
---
return cubeTosses;
---
} // end method
```

## 6.4.6.3. Try and Solve Part A¶

Write the method `getCubeTosses`

that takes a number cube and a number of tosses as parameters. The method should return an array of the values produced by tossing the number cube the given number of times.