Computer Science A
2022 FRQ 4
- FRQ Question Part A
- Code Runner Challenge
- Code Runner Challenge
- Scoring Guidelines Part A
- Scoring Guidelines Part B
FRQ Question 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. }
(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.
Complete the repopulate method.
/** 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()
// CODE_RUNNER: <challenge text>
Code Runner Challenge
FRQ #4 (a) - repopulate()
View IPYNB Source
// CODE_RUNNER: FRQ #4 (a) - repopulate()
import java.util.Arrays;
import java.util.Random;
public class Main {
public static class Data {
public static final int MAX = 500;
private int[][] grid;
public Data(int rows, int cols) {
grid = new int[rows][cols];
}
/**
* Fills all elements of grid with randomly generated values:
* - between 1 and MAX inclusive
* - divisible by 10
* - NOT divisible by 100
* All valid values have equal probability.
*/
public void repopulate() {
Random rand = new Random();
int maxMultiple = MAX / 10; // largest k such that 10*k <= MAX
for (int r = 0; r < grid.length; r++) {
for (int c = 0; c < grid[0].length; c++) {
int value;
do {
int k = rand.nextInt(maxMultiple) + 1; // 1..maxMultiple
value = 10 * k;
} while (value % 100 == 0);
grid[r][c] = value;
}
}
}
}
public void driver() {
Data d = new Data(3, 5);
d.repopulate();
System.out.println("Generated Grid:");
for (int[] row : d.grid) {
System.out.println(Arrays.toString(row));
}
}
public static void main(String[] args) {
Main m = new Main();
m.driver();
}
}
Main.main(null);
(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.
10 50 40 20 40 20 30 50 30
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.
10 540 440 440 220 450 440 190
Complete the countIncreasingCols method.
/** 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()
Begin your response at the top of a new page in the Free Response booklet and fill in the appropriate circle indicating the question number. If there are multiple parts to this question, write the part letter with your response.
public class Data { public static final int MAX = /* value not shown */; private int[][] grid;
public void repopulate()
public int countIncreasingCols() }
Code Runner Challenge
FRQ #4 (b) - countIncreasingCols()
View IPYNB Source
// CODE_RUNNER: FRQ #4 (b) - countIncreasingCols()
import java.util.Arrays;
public class Main {
public static class Data {
private int[][] grid;
public Data(int[][] g) {
grid = g;
}
/**
* Returns the number of columns in grid that are in increasing order.
* A column is increasing if for every row r > 0:
* grid[r][c] >= grid[r-1][c]
* A column with only one row is considered increasing.
*/
public int countIncreasingCols() {
int rows = grid.length;
int cols = grid[0].length;
int count = 0;
for (int c = 0; c < cols; c++) {
boolean increasing = true;
for (int r = 1; r < rows; r++) {
if (grid[r][c] < grid[r - 1][c]) {
increasing = false;
break;
}
}
if (increasing) {
count++;
}
}
return count;
}
}
private static void printGrid(int[][] g) {
for (int[] row : g) System.out.println(Arrays.toString(row));
}
public void driver() {
int[][] example1 = {
{10, 50, 40},
{20, 40, 20},
{30, 50, 30}
};
int[][] example2 = {
{10, 540, 440, 440},
{220, 450, 440, 190}
};
System.out.println("Example 1 Grid:");
printGrid(example1);
Data d1 = new Data(example1);
System.out.println("countIncreasingCols() = " + d1.countIncreasingCols());
System.out.println();
System.out.println("Example 2 Grid:");
printGrid(example2);
Data d2 = new Data(example2);
System.out.println("countIncreasingCols() = " + d2.countIncreasingCols());
System.out.println();
}
public static void main(String[] args) {
Main m = new Main();
m.driver();
}
}
Main.main(null);
Scoring Guidelines Part A
(a) repopulate
| # | Scoring Criteria | Decision Rules | Points |
|---|---|---|---|
| 1 | Traverses grid (no bounds errors) |
Responses will not earn the point if they: - fail to access an element of grid- access the elements of grid incorrectly- use enhanced for loops without using a grid element inside the loop |
1 point |
| 2 | Generates a random integer in a range based on MAX |
Responses can still earn the point even if they: - assume or verify that MAX >= 10Responses will not earn the point if they: - fail to cast to an int |
1 point |
| 3 | Ensure that all produced values are divisible by 10 but not by 100 | Responses can still earn the point even if they: - fail to use a loop |
1 point |
| 4 | Assigns appropriate values to all elements of grid (algorithm) |
Responses can still earn the point even if they: - assume or verify that MAX >= 10- produce some values that are not divisible by 10 or divisible by 100, if the range and distribution are otherwise correct Responses will not earn the point if they: - use enhanced for loops and fail to maintain indices - produce values that are not equally distributed - produce values outside the specified range - exclude values that should be considered valid (other than errors in 10/100 handling) |
1 point |
Scoring Guidelines Part B
(b) countIncreasingCols
| # | Scoring Criteria | Decision Rules | Points |
|---|---|---|---|
| 5 | Traverses grid in column major order (no loop header bounds errors) |
Responses can still earn the point even if they: - access an out-of-bounds row or column index adjacent to the edge of the grid, if the loop bounds include only valid indices Responses will not earn the point if they: - fail to access an element of grid- access the elements of grid incorrectly |
1 point |
| 6 | Compares two elements in the same column of grid |
Responses can still earn the point even if they: - access elements of grid incorrectly- access elements in nonadjacent rows - compare elements with ==- compare two elements in the same row instead of the same column |
1 point |
| 7 | Determines whether a single column is in increasing order (algorithm) | Responses can still earn the point even if they: - fail to reset variables in the outer loop before proceeding to the next column Responses will not earn the point if they: - fail to access all pairs of adjacent elements in a single column - cause a bounds error by attempting to compare the first element of a column with a previous element or the last element of a column with a subsequent element - incorrectly identify a column with at least one pair of adjacent elements in decreasing order as increasing |
1 point |
| 8 | Counts all columns that are identified as increasing (algorithm) | Responses can still earn the point even if they: - detect increasing order for each row instead of each column - incorrectly identify increasing columns in the inner loop Responses will not earn the point if they: - fail to initialize the counter - fail to reset variables in the outer loop causing subsequent runs of the inner loop to misidentify columns |
1 point |
| 9 | Returns calculated count of increasing columns | Responses can still earn the point even if they: - calculate the count incorrectly |
1 point |