FRQ4 - 2D Arrays - Part 2

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

Java1 class period2 exercises10 graded tests

Files

README.mdDownload

FRQ4 - 2D Arrays - Part 2

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.

From the 2025 Course and Exam Description:

Free-Response Question 4: 2D Array. Students will be provided with a scenario and its associated class(es). Students will write one method of a given class based on provided specifications and examples. The method requires students to use, analyze, and manipulate data in a 2D array structure.

FRQ 4 requires the following skills:

  • Iterate through a 2D array
  • Access elements of a 2D array
  • Call methods on elements of a 2D array

2022 FRQ 4 - Data - Part (a)

This question involves a two-dimensional array of integers that represents a collection of randomly generated data. A partial declaration of the Data class is shown. You will write two methods of the Data class.

public class Data
{
    public static final int MAX = /* value not shown */;
    private int[][] grid;

    /** Fills all elements of grid with randomly generated values, as described in part (a)
    * Precondition: grid is not null.
    * grid has at least one element.
    */
    public void repopulate()
    { /* to be implemented in part (a) */ }

    /** Returns the number of columns in grid that are in increasing order, as described
    * in part (b)
    * Precondition: grid is not null.
    * grid has at least one element.
    */
    public int countIncreasingCols()
    { /* to be implemented in part (b) */ }

    // There may be instance variables, constructors, and methods that are not shown.
}

Part (a) Write the repopulate method, which assigns a newly generated random value to each element of grid. Each value is computed to meet all of the following criteria, and all valid values must have an equal chance of being generated.

  • The value is between 1 and MAX, inclusive.
  • The value is divisible by 10.
  • The value is not divisible by 100.
Exercise 1

Complete the repopulate method.

Open the 01-frq4-2022-data-a folder and edit Data.java. Press Run to try it; Run Tests checks it (5 tests).

2022 FRQ 4 - Data - Part (b)

Part (b) Write the countIncreasingCols method, which returns the number of columns in grid that are in increasing order. A column is considered to be in increasing order if the element in each row after the first row is greater than or equal to the element in the previous row. A column with only one row is considered to be in increasing order.

The following examples show the countIncreasingCols return values for possible contents of grid.

The return value for the following contents of grid is 1, since the first column is in increasing order but the second and third columns are not.

Frq4 data grid1

The return value for the following contents of grid is 2, since the first and third columns are in increasing order but the second and fourth columns are not.

Frq4 data grid2

Exercise 2

Complete the countIncreasingCols method.

Open the 02-frq4-2022-data-b folder and edit Data.java. Press Run to try it; Run Tests checks it (5 tests).

For teachers

FRQ4 - 2D Arrays - Part 2

Students read CSAwesome2's “FRQ4 - 2D Arrays - Part 2” in CS Room and work through its 2 Java coding exercises.

  • Time: 1 class period
  • Tests: 10 in 2 graded exercises, set up for you when you import the lesson

Goals

  • Practice for the AP exam's free-response questions.

Before class

In class

  1. Students read README.md.
  2. Students do exercises 01–02, each in its own 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-frq4-2022-data-aData.javaAutograded5
02-frq4-2022-data-bData.javaAutograded5

Teachers can download a reference solution after signing in.