Unit 4A - Free Response - Number Cube A
From CS Awesome 2CC BY-NC-SA 4.0Original page
Java1 class period1 exercise4 graded tests
Files
Unit 4A - Free Response - Number Cube A
Each coding exercise has its own folder. Open its .java file and press Run to try it. Run Tests checks every graded exercise in this lesson at once.
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 Past AP CSA Exams.
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.
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.
Check your understanding: Which Java expression correctly generates a random number between 1 and 6? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/numberCubeA.html#numbercubea_1
Check your understanding: Which of the following correctly creates an array of size numTosses? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/numberCubeA.html#numbercubea_2
Check your understanding: Which of the following correctly loops numTosses number of times? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/numberCubeA.html#numbercubea_3
Mixed Up Code
Check your understanding: The method
getCubeTossesbelow contains the correct code for one solution to this problem, but it is mixed up. — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/numberCubeA.html#numcubeA
Try and Solve Part A
Exercise 1
FRQ Number Cube 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.
Open the
01-FRQNumberCubeAfolder and editNumberCube.java. Press Run to try it; Run Tests checks it (4 tests).
For teachers
Unit 4A - Free Response - Number Cube A
Students read CSAwesome2's “Unit 4A - Free Response - Number Cube A” in CS Room and work through its 1 Java coding exercise.
- Time: 1 class period
- Tests: 4 in 1 graded exercise, set up for you when you import the lesson
Goals
- Practice for Unit 4: Data Collections.
Before class
- Import the lesson into your class from the Lessons page, or with New assignment → Use a lesson. It arrives as a draft with its tests already set up.
- Publish it.
- The original page, with its mixed-up code and multiple-choice questions, is on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/numberCubeA.html
- CS Awesome's own teacher materials: the CS Awesome teacher site
In class
- Students read
README.md. Questions that need CS Awesome's interactive widgets link back to it. - Students do exercise 01 in its folder, pressing Run to try their code.
- Run Tests checks every graded exercise at once; scores appear in the Gradebook. Grading uses your copy of the tests, so a student who edits theirs does not change the grade.
Exercises
| Folder | File | Grading | Tests |
|---|---|---|---|
01-FRQNumberCubeA | NumberCube.java | Autograded | 4 |
Teachers can download a reference solution after signing in.