AP CSA 2021 Q4 - ArrayResizer Class


<-- Back to Solution of Q3 (ClubMembers) - FRQ - 2021 Home to AP CSA Exam Solutions-->





Solution of Q4 (ArrayResizer) - Free Response Question - 2021


The original question can be found at: https://apcentral.collegeboard.org/media/pdf/ap21-frq-computer-science-a.pdf


Part (a)- public static boolean isNonZeroRow(int[][] array2D, int r)


    public static boolean isNonZeroRow(int[][] array2D, int r)
    {
        boolean isNonZero = true;
        for (int i=r; (i<= r) && (isNonZero) ; i++)
        {
            for (int j=0; (j < array2D[i].length) && (isNonZero); j++)
            {
                if (array2D[i][j] == 0) isNonZero = false;
            }
        }
        return isNonZero;
    }



Part (b)- public static int[][] resize(int[][] array2D)


    public static int[][] resize(int[][] array2D)
    {
        int numRows = numNonZeroRows(array2D);
        int[][] newArray =new int[numRows][array2D[0].length];
        int k=0;
        
        for (int i=0; (i< array2D.length); i++)
        {
            if (isNonZeroRow(array2D,i))
            {
                for (int j=0; j< array2D[0].length; j++)
                {
                    newArray[k][j] = array2D[i][j];
                }
                k++;
            }
        }
        return newArray;
    }

Java project files (with Runner code):


<-- Back to Solution of Q3 (ClubMembers) - FRQ - 2021 Home to AP CSA Exam Solutions-->