public class ArrayResizer
{
public static boolean isNonZeroRow( int[][] array2D, int r )
{
// Write this method for part A
return false; // Statement added to prevent compile error
// Remove it when you write this method
}
public static int numNonZeroRows( int[][] array2D )
{
int numNonZeroRows = 0;
for ( int i = 0; i < array2D.length; i++ )
if( isNonZeroRow( array2D, i ) )
numNonZeroRows++;
return numNonZeroRows;
}
public static int[][] resize( int[][] array2D )
{
// Write this method for part B
return null; // Statement added to prevent compile error
// Remove it when you write this method
}
}
|
Use this Java program to test your answer to question A:
public class TestA
{
public static void main(String[] args)
{
int[][] arr = {{2, 1, 0},
{1, 3, 2},
{0, 0, 0},
{4, 5, 6}};
System.out.println(ArrayResizer.isNonZeroRow(arr, 0));
System.out.println(ArrayResizer.isNonZeroRow(arr, 1));
System.out.println(ArrayResizer.isNonZeroRow(arr, 2));
System.out.println(ArrayResizer.isNonZeroRow(arr, 3));
}
}
|
The correct answer is:
false true false true |
Use this Java program to test your answer to question B:
import java.util.Arrays;
public class TestB
{
public static void main(String[] args)
{
int[][] arr = {{2, 1, 0},{1, 3, 2},{0, 0, 0},{4, 5, 6}};
System.out.println( Arrays.deepToString( arr ) );
int[][] smaller = ArrayResizer.resize(arr);
System.out.println( Arrays.deepToString( smaller ) );
}
}
|
The correct answer is:
[[2, 1, 0], [1, 3, 2], [0, 0, 0], [4, 5, 6]] [[1, 3, 2], [4, 5, 6]] |