Question Prompt:

Part A

How to Solve

Step 1: Understand the Problem

You need to implement a method getCubeTosses that records the results of tossing a number cube a specified number of times.

Step 2: Identify Inputs and Outputs

Identify what information you’re given and what you need to produce. In this problem, you’re given:

  • A NumberCube object (cube) for simulating cube tosses.
  • The number of tosses to be recorded (numTosses).

You need to produce:

  • An array of integer values representing the results of the cube tosses.

Step 3: Plan the Approach

Think about how you can achieve the desired output using the given inputs.

In this case, you need to create an array to store the results.

You’ll use a loop to toss the cube the specified number of times and record the results in the array.

Finally, you’ll return the array as the output.

class NumberCube {       // numberCube class represents a simulated cube with a toss method
    public int toss() {   
        return (int) (Math.random() * 6) + 1;       // simulates a toss of the cube and returns a random number between 1 and 6
    }
}

public class Main {
    public static void main(String[] args) {          
        NumberCube cube = new NumberCube();            // create a NumberCube object

        int numTosses = 10;

        int[] cubeTosses = getCubeTosses(cube, numTosses);         // call the getCubeTosses function to get an array of toss results

        System.out.println("Results of " + numTosses + " cube tosses:");           // display the results
        for (int tossResult : cubeTosses) {
            System.out.println(tossResult);
        }
    }

    public static int[] getCubeTosses(NumberCube cube, int numTosses) 
{
    int[] cubeTosses = new int[numTosses];   // create an array to store the results of cube tosses    1
    for (int i = 0; i < numTosses; i++)     // loops through the desired number of tosses wanted
    {
        int tossResult = cube.toss();    // simulate a single toss of the cube using the 'toss' method of the 'cube' object
        cubeTosses[i] = tossResult;    // store the result of the toss in the 'cubeTosses' array at the current index 'i'    2 & 3
    }
    return cubeTosses;     // return the array containing the results of all cube tosses   4
}
}

Main.main(null)
Results of 10 cube tosses:
4
5
6
4
1
4
2
1
4
4

Scoring Rubric for Part A

Points Criteria
1 Constructs an Array of int types with size numlosses
1 Calls cube.toss ()
1 Assigns values from cube.toss () to each location in the array
1 Returns array of generated results
Total Points 4

Part B

How to Solve

Step 1: Understand the Problem

You need to find the starting index of the longest run of consecutive repeated values in an array of number cube toss results.

Step 2: Identify Inputs and Outputs

Identify the inputs (parameters) and outputs (return values) of the function/method you need to write.

Input: An array of integer values (values). Output: The starting index of the longest run.

Step 3: Plan the Approach

Plan how to solve the problem. In this case: Initialize variables to keep track of the current run’s length (currentLen), the longest run’s length (maxLen), and the starting index of the longest run (maxStart).

Use a loop to iterate through the array, comparing each element with the next one.

Update currentLen and maxLen as needed based on the comparison results.

Keep track of the starting index of the current run (currentRunStartIndex) to calculate maxStart accurately.

public static int getLongestRun(int[] values) {
    int currentLen = 0;    // the length of the current run
    int maxLen = 0;        // the length of the longest run found
    int maxStart = -1;     // the starting index of the longest run
    for (int i = 0; i < values.length - 1; i++) {      // iterate through the 'values' array, comparing adjacent elements    1
        if (values[i] == values[i + 1]) {      // if the current element is the same as the next element then it's part of a run   2 & 3
            currentLen++;
            if (currentLen > maxLen) {                  // check if the current run is longer than the longest run found so far
                maxLen = currentLen;              // update the length of the longest run      4
                maxStart = i - currentLen + 1;       // update the starting index of the longest run
            }
        } else {
            currentLen = 0;       // reset the length of the current run
        }
    }     
    return maxStart;    // return the starting index of the longest run (or -1 if undefined)      5
}

public class Main{
    public static void main(String[] args){
        System.out.println(getLongestRun(new int[] {1,2,4,5,6,6,6,7,8,9,9,9,9}));    // input the list of numbers into this
    }
}

Main.main(null)
9

Scoring Rubric for Part B

Points Criteria
1 Accesses every element of values.
1 Determines the existence of run of consecutive elements
1 Determines the length of at least one run of consecutive elements
1 Identifies the maximum length run based on all runs
1 Returns index of identified maximum length run or -1 if no run identified
Total Points 5

Overall Blog

One particular challenge I faced was with correctly initializing and updating variables within the code. Specifically, I had difficulty tracking the starting index and length of the longest run of repeated values in the array. This led to incorrect results when I attempted to find the longest run. To help me understand and fix this issue in this FRQ, I was able to find Collegeboard videos to help me as well as people on Youtube analyzing and demonstrating how to solve APCSA FRQs. This helped me understand more about how Java Arrays actually work and helped me fix the concept difficulty I had with the variables.