Unit 4A - Free Response - Number Cube B

From CS Awesome 2CC BY-NC-SA 4.0Original page

Java1 class period1 exercise4 graded tests

Files

README.mdDownload

Unit 4A - Free Response - Number Cube B

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.

Number Line

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 b. Write the method getLongestRun that takes as its parameter an array of integer values representing a series of number cube tosses. The method returns the starting index in the array of a run of maximum size. A run is defined as the repeated occurrence of the same value in two or more consecutive positions in the array. In the example array shown above there are two runs of length 4. One starts at index 6 and one at index 14. The method may return either of those indices.

If there are no runs of any value, the method returns -1.

How to Solve

You are going to need to keep track of the current run length, the maximum run length, the index where the max run started (which should start at -1). You want to compare one value to an adjacent value so you will need to be careful that you don’t go out of bounds. If you find two values that are adjacent that are equal then increment the current run length and set the start index. If the two adjacent values are not equal then reset the current run length to 0. Return the starting index of the maximum length run.

Mixed Up Code

Check your understanding: The method getLongestRun below 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/numberCubeB.html#numcubeB

Try and Solve Part B

Exercise 1

FRQ Number Cube B: Write the method getLongestRun that takes as its parameter an array of integer values representing a series of number cube tosses. The method returns the starting index in the array of a run of maximum size. A run is defined as the repeated occurrence of the same value in two or more consecutive positions in the array.

Open the 01-FRQNumberCubeB folder and edit NumberCube.java. Press Run to try it; Run Tests checks it (4 tests).

For teachers

Unit 4A - Free Response - Number Cube B

Students read CSAwesome2's “Unit 4A - Free Response - Number Cube B” 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

In class

  1. Students read README.md. Questions that need CS Awesome's interactive widgets link back to it.
  2. Students do exercise 01 in its folder, pressing Run to try their code.
  3. 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

FolderFileGradingTests
01-FRQNumberCubeBNumberCube.javaAutograded4

Teachers can download a reference solution after signing in.