Java

OOP GUIDE / WORK

 

DATA CLASS

 

Topics

  • 2D Arrays (searching columns)
  • Divisibility
  • Random numbers

 

Source

  • This question was part of the 2022 AP exam free response questions.  You can find all four of the original questions on the AP Free Response site.

 

 

TASK – PART 1 – QUESTION

 

Click here for a PDF containing the question relating to the Data class.  Please look at part 2 before solving the question.

 

 

TASK – PART 2 – REQUIRED CLASS

Copy the following class to your IDE.  You will be coding in the repopulate method and the countIncreasingColumns method.

public class Data

{

     public static final int MAX = 1000; //value needed for implementation

     private int[][] grid;

    

     //This constructor was added so that we can implement the Data class.

     public Data(int[][] tmpGrid)

     {

           grid = tmpGrid;

     }

    

     /**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

     }

    

     /**Return the number of colours 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

     }

    

     //Added to display grid's content

     public String toString()

     {

           String s = "";

          for (int r=0; r<grid.length; r++)

           {

                for (int c=0; c<grid[r].length; c++)

                {

                      s = s + grid[r][c] + " ";

                }

                s = s + "\n";

           }

           return s;

     }

    

    

}

 

 

 

TASK – PART 3 – IMPLEMENT THE DATA CLASS METHODS

 

Go implement both parts A and B.

 

TASK – PART 4 – TESTING

Copy and paste the following code that will test your Data class.  Note that the program below matches the example method calls in the PDF document.  You should compare your output to the output in the PDF document to see if they match.

 

 

public class DataTester

{

     public static void main(String[] args)

     {

           //PART A

           int[][] grid0 = {{0, 0, 0, 0},

                                  {0, 0, 0, 0},

                                  {0, 0, 0, 0},

                                  {0, 0, 0, 0}};

           Data d0 = new Data(grid0);

           System.out.println(d0);

           d0.repopulate();

           System.out.println(d0);

          

          

          

           //PART B

           int[][] grid1 = {{10, 50, 40},

                               {20, 40, 20},

                               {30, 50, 30}};

           int[][] grid2 = {{10, 540, 440, 440},

                               {220, 450, 440, 190}};

           Data d1 = new Data(grid1);

           System.out.println(d1);

           System.out.println("Increasing cols:" + d1.countIncreasingCols());

          

           Data d2 = new Data(grid2);

           System.out.println(d2);

           System.out.println("Increasing cols:" + d2.countIncreasingCols());

     }

}