From the free course AP Computer Science A on Lykke

AP Practice 5

A two-dimensional array stores data in a rectangular grid, but an AP Computer Science A free-response task rewards more than producing the right final number: it rewards correct traversal, valid indexing, appropriate updates, and code that matches the requested behavior.

AP Practice 5

A two-dimensional array stores data in a rectangular grid, but an AP Computer Science A free-response task rewards more than producing the right final number: it rewards correct traversal, valid indexing, appropriate updates, and code that matches the requested behavior.

This original practice task targets the two-dimensional array free-response task type. It emphasizes Computational Thinking Practice 2: Develop Code, because you must implement Java code; Computational Thinking Practice 3: Analyze Code, because you must reason about rows, columns, and boundary conditions; and Computational Thinking Practice 4: Document Code and Computing Systems, because clear explanations of behavior and assumptions matter.

Exam target: the grid-analysis method

Imagine a greenhouse divided into plots. Each row represents one greenhouse aisle, and each column represents one plot within that aisle. The value at readings[row][col] is the temperature recorded in that plot.

Your task is to complete a method that finds the number of plots whose temperature is at least a specified threshold.

public static int countWarmPlots(double[][] readings,
                                 double threshold)

The method must:

  • return the number of elements in readings whose value is greater than or equal to threshold;
  • examine every element exactly as needed;
  • work for a rectangular two-dimensional array;
  • return 0 if readings has no rows;
  • not modify readings.

Original practice prompt

Complete the method countWarmPlots.

public static int countWarmPlots(double[][] readings,
                                 double threshold)
{
    // Write your code here
}

For example, if readings contains the following values and threshold is $20.0$:

double[][] readings = {
    {18.5, 21.0, 20.0},
    {24.5, 19.0, 22.0}
};

the method should return $4$, because $21.0$, $20.0$, $24.5$, and $22.0$ meet the threshold.

Work it out under timed conditions

Allow approximately 15 minutes: $3$ minutes to identify the data structure and required result, $8$ minutes to write and mentally trace the method, and $4$ minutes to test edge cases. Do not begin by typing nested loops mechanically. First identify what one counter represents: here, count represents the number of qualifying plots encountered so far.

Worked solution

The outer loop visits each row. The inner loop visits each element in that row. Using readings[row].length is safer than assuming every row has the same length, because Java two-dimensional arrays are arrays whose elements are themselves arrays.

public static int countWarmPlots(double[][] readings,
                                 double threshold)
{
    int count = 0;

    for (int row = 0; row < readings.length; row++)
    {
        for (int col = 0; col < readings[row].length; col++)
        {
            if (readings[row][col] >= threshold)
            {
                count++;
            }
        }
    }

    return count;
}

For the sample data, the trace is:

Element examined Comparison count after comparison
$18.5$ $18.5 \ge 20.0$ is false $0$
$21.0$ true $1$
$20.0$ true $2$
$24.5$ true $3$
$19.0$ false $3$
$22.0$ true $4$

The returned value is therefore $4$.

What earns credit

A strong response provides evidence for the rubric’s core implementation requirements:

  1. Correct traversal: the code reaches every relevant row and every element within each row.
  2. Correct access: each value is accessed with the two-dimensional expression readings[row][col].
  3. Correct selection condition: values equal to the threshold count, so the comparison must use >=, not >.
  4. Correct accumulation: the counter increases exactly when the condition is satisfied.
  5. Correct return value: the completed count is returned after traversal.
  6. No unintended mutation: the method reads the array but never assigns a new value to an array element.

These are the reasoning behaviors an examiner can verify directly from the code. A solution that produces $4$ only for the sample but skips rows, uses the wrong comparison, or returns the threshold itself does not demonstrate the required algorithm.

Boundary checks and common misconceptions

Misconception: a two-dimensional array is always a perfect rectangle. In Java, double[][] may contain rows of different lengths. The expression readings[row].length describes the current row; readings.length describes the number of rows.

Misconception: “at least” means strictly greater. The value $20.0$ must be counted when the threshold is $20.0$, so >= is essential.

Misconception: the inner loop should use readings.length. That would confuse the number of rows with the number of columns and can cause an index-out-of-bounds error. The inner loop must use the current row’s length.

Test these cases mentally:

double[][] a = {};
double[][] b = {{20.0}};
double[][] c = {{19.0, 21.0}, {20.0}};

For threshold $20.0$, the expected results are $0$, $1$, and $2$. Notice that a has no rows, while b confirms that equality counts and c confirms that rows may have different lengths.

Error-review routine

After attempting the task, classify every error rather than merely correcting it:

  • Traversal error: a row or column was skipped.
  • Indexing error: the wrong dimension or index expression was used.
  • Condition error: equality or inequality was mishandled.
  • State error: the counter was initialized or updated incorrectly.
  • Contract error: the method returned the wrong quantity or changed the input array.

Then rewrite the method once without looking at the solution and explain, in one sentence, what count means before and after each loop iteration. That invariant—“count equals the number of qualifying elements already examined”—is the compact reasoning an examiner rewards because it connects traversal, condition, and result.

AP Practice 5 - AP Computer Science A - image 1
AP Practice 5 - AP Computer Science A - image 1
AP Practice 5 - AP Computer Science A - diagram 1
AP Practice 5 - AP Computer Science A - diagram 1

Source Materials

The whole course on Lykke · Browse all public course wikis