Computer Science A
2021 FRQ 4
FRQ Question 4
This question involves manipulating a two-dimensional array of integers. You will write two static methods of the ArrayResizer class, which is shown below.
public class ArrayResizer
{
/** Returns true if and only if every value in row r of array2D is non-zero.
* Precondition: r is a valid row index in array2D.
* Postcondition: array2D is unchanged.
*/
public static boolean isNonZeroRow(int[][] array2D, int r)
{ /* to be implemented in part (a) */ }
/** Returns the number of rows in array2D that contain all non-zero values.
* Postcondition: array2D is unchanged.
*/
public static int numNonZeroRows(int[][] array2D)
{ /* implementation not shown */ }
/** Returns a new, possibly smaller, two-dimensional array that contains only rows
* from array2D with no zeros, as described in part (b).
* Precondition: array2D contains at least one column and at least one row with no zeros.
* Postcondition: array2D is unchanged.
*/
public static int[][] resize(int[][] array2D)
{ /* to be implemented in part (b) */ }
}
FRQ Question Part A
(a) Write the method isNonZeroRow, which returns true if and only if all elements in row r of a two-dimensional array array2D are not equal to zero.
For example, consider the following statement, which initializes a two-dimensional array.
int[][] arr = {
{2, 1, 0},
{1, 3, 2},
{0, 0, 0},
{4, 5, 6}
};
Sample calls to isNonZeroRow are shown below.
Call to isNonZeroRow |
Value Returned | Explanation |
|---|---|---|
ArrayResizer.isNonZeroRow(arr, 0) |
false |
At least one value in row 0 is zero. |
ArrayResizer.isNonZeroRow(arr, 1) |
true |
All values in row 1 are non-zero. |
ArrayResizer.isNonZeroRow(arr, 2) |
false |
At least one value in row 2 is zero. |
ArrayResizer.isNonZeroRow(arr, 3) |
true |
All values in row 3 are non-zero. |
Complete the isNonZeroRow method.
/** Returns true if and only if every value in row r of array2D is non-zero.
* Precondition: r is a valid row index in array2D.
* Postcondition: array2D is unchanged.
*/
public static boolean isNonZeroRow(int[][] array2D, int r)
// CODE_RUNNER: FRQ 2021 #4 - ArrayResizer
// Complete solution with test cases
class ArrayResizer
{
/** Returns true if and only if every value in row r of array2D is non-zero.
* Precondition: r is a valid row index in array2D.
* Postcondition: array2D is unchanged.
*/
public static boolean isNonZeroRow(int[][] array2D, int r)
{
for (int col = 0; col < array2D[r].length; col++)
{
if (array2D[r][col] == 0)
{
return false;
}
}
return true;
}
/** Returns the number of rows in array2D that contain all non-zero values.
* Postcondition: array2D is unchanged.
*/
public static int numNonZeroRows(int[][] array2D)
{
int count = 0;
for (int row = 0; row < array2D.length; row++)
{
if (isNonZeroRow(array2D, row))
{
count++;
}
}
return count;
}
/** Returns a new, possibly smaller, two-dimensional array that contains only rows
* from array2D with no zeros, as described in part (b).
* Precondition: array2D contains at least one column and at least one row with no zeros.
* Postcondition: array2D is unchanged.
*/
public static int[][] resize(int[][] array2D)
{
int numRows = numNonZeroRows(array2D);
int numCols = array2D[0].length;
int[][] result = new int[numRows][numCols];
int resultRow = 0;
for (int row = 0; row < array2D.length; row++)
{
if (isNonZeroRow(array2D, row))
{
for (int col = 0; col < numCols; col++)
{
result[resultRow][col] = array2D[row][col];
}
resultRow++;
}
}
return result;
}
// Helper method to print a 2D array
public static void print2DArray(int[][] arr)
{
System.out.print("{");
for (int i = 0; i < arr.length; i++)
{
System.out.print("{");
for (int j = 0; j < arr[i].length; j++)
{
System.out.print(arr[i][j]);
if (j < arr[i].length - 1)
{
System.out.print(", ");
}
}
System.out.print("}");
if (i < arr.length - 1)
{
System.out.print(", ");
}
}
System.out.println("}");
}
public static void main(String[] args)
{
// Test array from the FRQ
int[][] arr = {
{2, 1, 0},
{1, 3, 2},
{0, 0, 0},
{4, 5, 6}
};
System.out.println("Original array:");
print2DArray(arr);
System.out.println();
// Test Part (a) - isNonZeroRow
System.out.println("Testing isNonZeroRow:");
System.out.println("ArrayResizer.isNonZeroRow(arr, 0): " + isNonZeroRow(arr, 0) + " (Expected: false)");
System.out.println("ArrayResizer.isNonZeroRow(arr, 1): " + isNonZeroRow(arr, 1) + " (Expected: true)");
System.out.println("ArrayResizer.isNonZeroRow(arr, 2): " + isNonZeroRow(arr, 2) + " (Expected: false)");
System.out.println("ArrayResizer.isNonZeroRow(arr, 3): " + isNonZeroRow(arr, 3) + " (Expected: true)");
System.out.println();
// Test Part (b) - resize
System.out.println("Testing resize:");
int[][] smaller = resize(arr);
System.out.print("Result: ");
print2DArray(smaller);
System.out.println("Expected: { {1, 3, 2}, {4, 5, 6} }");
System.out.println();
// Verify original array is unchanged
System.out.println("Original array after resize (should be unchanged):");
print2DArray(arr);
System.out.println();
// Additional test case
System.out.println("Additional test case:");
int[][] arr2 = {
{5, 3, 0, 1},
{2, 7, 4, 6},
{1, 0, 8, 3},
{9, 8, 7, 6}
};
System.out.println("Array 2:");
print2DArray(arr2);
System.out.println("Non-zero rows: " + numNonZeroRows(arr2));
int[][] smaller2 = resize(arr2);
System.out.print("After resize: ");
print2DArray(smaller2);
}
}
FRQ Question Part B
(b) Write the method resize, which returns a new two-dimensional array containing only rows from array2D with all non-zero values. The elements in the new array should appear in the same order as the order in which they appeared in the original array.
The following code segment initializes a two-dimensional array and calls the resize method.
int[][] arr = {
{2, 1, 0},
{1, 3, 2},
{0, 0, 0},
{4, 5, 6}
};
int[][] smaller = ArrayResizer.resize(arr);
When the code segment completes, the following will be the contents of smaller.
{ {1, 3, 2}, {4, 5, 6} }
A helper method, numNonZeroRows, has been provided for you. The method returns the number of rows in its two-dimensional array parameter that contain no zero values.
Complete the resize method. Assume that isNonZeroRow works as specified, regardless of what you wrote in part (a). You must use numNonZeroRows and isNonZeroRow appropriately to receive full credit.
/** Returns a new, possibly smaller, two-dimensional array that contains only rows from array2D
* with no zeros, as described in part (b).
* Precondition: array2D contains at least one column and at least one row with no zeros.
* Postcondition: array2D is unchanged.
*/
public static int[][] resize(int[][] array2D)
// CODE_RUNNER: Arrays
// CODE_RUNNER: FRQ 2021 #4 - ArrayResizer
// Complete solution with test cases
class ArrayResizer
{
/** Returns true if and only if every value in row r of array2D is non-zero.
* Precondition: r is a valid row index in array2D.
* Postcondition: array2D is unchanged.
*/
public static boolean isNonZeroRow(int[][] array2D, int r)
{
for (int col = 0; col < array2D[r].length; col++)
{
if (array2D[r][col] == 0)
{
return false;
}
}
return true;
}
/** Returns the number of rows in array2D that contain all non-zero values.
* Postcondition: array2D is unchanged.
*/
public static int numNonZeroRows(int[][] array2D)
{
int count = 0;
for (int row = 0; row < array2D.length; row++)
{
if (isNonZeroRow(array2D, row))
{
count++;
}
}
return count;
}
/** Returns a new, possibly smaller, two-dimensional array that contains only rows
* from array2D with no zeros, as described in part (b).
* Precondition: array2D contains at least one column and at least one row with no zeros.
* Postcondition: array2D is unchanged.
*/
public static int[][] resize(int[][] array2D)
{
int numRows = numNonZeroRows(array2D);
int numCols = array2D[0].length;
int[][] result = new int[numRows][numCols];
int resultRow = 0;
for (int row = 0; row < array2D.length; row++)
{
if (isNonZeroRow(array2D, row))
{
for (int col = 0; col < numCols; col++)
{
result[resultRow][col] = array2D[row][col];
}
resultRow++;
}
}
return result;
}
// Helper method to print a 2D array
public static void print2DArray(int[][] arr)
{
System.out.print("{");
for (int i = 0; i < arr.length; i++)
{
System.out.print("{");
for (int j = 0; j < arr[i].length; j++)
{
System.out.print(arr[i][j]);
if (j < arr[i].length - 1)
{
System.out.print(", ");
}
}
System.out.print("}");
if (i < arr.length - 1)
{
System.out.print(", ");
}
}
System.out.println("}");
}
public static void main(String[] args)
{
// Test array from the FRQ
int[][] arr = {
{2, 1, 0},
{1, 3, 2},
{0, 0, 0},
{4, 5, 6}
};
System.out.println("Original array:");
print2DArray(arr);
System.out.println();
// Test Part (a) - isNonZeroRow
System.out.println("Testing isNonZeroRow:");
System.out.println("ArrayResizer.isNonZeroRow(arr, 0): " + isNonZeroRow(arr, 0) + " (Expected: false)");
System.out.println("ArrayResizer.isNonZeroRow(arr, 1): " + isNonZeroRow(arr, 1) + " (Expected: true)");
System.out.println("ArrayResizer.isNonZeroRow(arr, 2): " + isNonZeroRow(arr, 2) + " (Expected: false)");
System.out.println("ArrayResizer.isNonZeroRow(arr, 3): " + isNonZeroRow(arr, 3) + " (Expected: true)");
System.out.println();
// Test Part (b) - resize
System.out.println("Testing resize:");
int[][] smaller = resize(arr);
System.out.print("Result: ");
print2DArray(smaller);
System.out.println("Expected: { {1, 3, 2}, {4, 5, 6} }");
System.out.println();
// Verify original array is unchanged
System.out.println("Original array after resize (should be unchanged):");
print2DArray(arr);
System.out.println();
// Additional test case
System.out.println("Additional test case:");
int[][] arr2 = {
{5, 3, 0, 1},
{2, 7, 4, 6},
{1, 0, 8, 3},
{9, 8, 7, 6}
};
System.out.println("Array 2:");
print2DArray(arr2);
System.out.println("Non-zero rows: " + numNonZeroRows(arr2));
int[][] smaller2 = resize(arr2);
System.out.print("After resize: ");
print2DArray(smaller2);
}
}
Scoring Guidelines
Canonical Solution
// (a) 3 points
public static boolean isNonZeroRow(int[][] array2D, int r)
{
for (int col = 0; col < array2D[0].length; col++)
{
if (array2D[r][col] == 0)
{
return false;
}
}
return true;
}
// (b) 6 points
public static int[][] resize(int[][] array2D)
{
int numRows = array2D.length;
int numCols = array2D[0].length;
int[][] result = new int[numNonZeroRows(array2D)][numCols];
int newRowIndex = 0;
for (int r = 0; r < numRows; r++)
{
if (isNonZeroRow(array2D, r))
{
for (int c = 0; c < numCols; c++)
{
result[newRowIndex][c] = array2D[r][c];
}
newRowIndex++;
}
}
return result;
}
Part (a) isNonZeroRow Rubric
| # | Scoring Criteria | Decision Rules | Points |
|---|---|---|---|
| 1 | Compares an item from array2D with 0 |
Responses will not earn the point if they fail to attempt the comparison, even if they access an item from array2D |
1 point |
| 2 | Accesses every item from row r of 2D array (no bounds errors) |
Responses can still earn the point even if they return early from an otherwise correctly-bounded loop | 1 point |
| 3 | Returns true if and only if row contains no zeros |
Responses can still earn the point even if they process a column of the 2D array rather than a row Responses will not earn the point if they fail to return a value in some cases |
1 point |
| Total for part (a): 3 points |
Part (b) resize Rubric
| # | Scoring Criteria | Decision Rules | Points |
|---|---|---|---|
| 4 | Calls numNonZeroRows and isNonZeroRow |
Responses can still earn the point even if they fail to use or store the return value Responses will not earn the point if they • include incorrect number or type of parameters • call methods on an object or class other than ArrayResizer |
1 point |
| 5 | Identifies rows with no zeros (in the context of an if) |
Responses can still earn the point even if they call isNonZeroRow incorrectly, if the row being tested is clearly identified (index or reference) |
1 point |
| 6 | Declares and creates a new 2D array of the correct size | Response will not earn the point if they transpose the dimensions of the created array | 1 point |
| 7 | Maintains an index in the new array | Responses will not earn the point if they • fail to declare, initialize, and update a different index • maintain the index in a way that overwrites, skips, or duplicates rows |
1 point |
| 8 | Traverses all necessary elements of array2D (no bounds errors) |
Responses can still earn the point even if they • cause a bounds error by declaring and creating a new 2D array of an incorrect size • fail to maintain an index in the new array correctly, resulting in a bounds error • fail to access individual elements in a nested loop, if they access each row as an entire row Responses will not earn the point if they transpose coordinates, leading to a bounds error and/or copying columns |
1 point |
| 9 | Copies all and only rows identified as having no zero elements into the new array | Responses can still earn the point even if they • copy a reference • identify rows incorrectly, if the logical sense can be determined and is correct • copy columns instead of rows, consistent with the dimensions of the created 2D array Responses will not earn the point if they • remove or overwrite data from array2D (instead of or in addition to copying it to the new array)• reverse the logical sense of which rows to copy |
1 point |
| Total for part (b): 6 points |
Question-specific Penalties
-1 (u) Use array2D[].length to refer to the number of columns in a row of the 2D array
Total for question 4: 9 points