Grid Logic WorksheetGrid Logic Worksheet AP Computer Science A Topic 8: 2D Arrays Student Name Date Mission Parameters Complete the following five Java methods. Ensure your solutions use standard nested for or for-each loops appropriately. Assume all 2D arrays are rectangular (all rows have the same length) and contain at least one element unless otherwise specified. 01 Row Averages Write a method rowAverages that takes a 2D array of integers and returns a 1D array of doubles containing the average value of each row. public static double[] rowAverages(int[][] grid) { } 02 Max Column Total Write a method maxColumnIndex that returns the index of the column with the highest sum of its elements. If there is a tie, return the first index. public static int maxColumnIndex(int[][] grid) { } 03 Lowest Row Sum Write a method lowestRowIndex that takes a 2D array of doubles and returns the index of the row with the lowest sum. public static int lowestRowIndex(double[][] grid) { } 04 Target Tracker Write a method countTarget that counts how many times a specific String target appears in a 2D array of Strings. Case sensitive. public static int countTarget(String[][] grid, String target) { } 05 Absolute Peak Write a method findMax that traverses a 2D array of doubles and returns the single largest value found in the entire grid. public static double findMax(double[][] grid) { }
Array Grid SlidesGRID LOGIC Mastering 2D Arrays in Java The Mental Model Indexing Access elements using grid[row][col]. Dimensions Rows: grid.length Columns: grid[0].length // 3x4 Grid int[][] nums = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 0, 1, 2} }; Traversal Strategy Row-Major Process row by row. This is the "default" for 2D arrays. for (int r = 0; r < g.length; r++) { for (int c = 0; c < g[r].length; c++) { // Access g[r][c] } } Column-Major Process column by column. Flip your loop nesting! for (int c = 0; c < g[0].length; c++) { for (int r = 0; r < g.length; r++) { // Access g[r][c] } } The Challenge Set 01 Row Averages int[][] → double[] 02 Max Column Index int[][] → int 03 Lowest Row Index double[][] → int 04 Target Counter String[][], String → int 05 Find Absolute Max double[][] → double Developer Pro Tips Precision Matters When calculating averages, ensure you divide by a double (e.g., sum / (double)row.length) to avoid integer division. Extreme Values When looking for a "min" or "max", initialize your variable with the first element of the grid or Integer.MIN_VALUE / Integer.MAX_VALUE.
Grid Logic KeyGrid Logic Answer Key Teacher Resource Topic 8: 2D Arrays 01 Row Averages public static double[] rowAverages(int[][] grid) { double[] averages = new double[grid.length]; for (int r = 0; r < grid.length; r++) { int sum = 0; for (int c = 0; c < grid[r].length; c++) { sum += grid[r][c]; } averages[r] = (double)sum / grid[r].length; } return averages; } 02 Max Column Total public static int maxColumnIndex(int[][] grid) { int maxCol = 0; int maxSum = Integer.MIN_VALUE; for (int c = 0; c < grid[0].length; c++) { int currentSum = 0; for (int r = 0; r < grid.length; r++) { currentSum += grid[r][c]; } if (currentSum > maxSum) { maxSum = currentSum; maxCol = c; } } return maxCol; } 03 Lowest Row Sum public static int lowestRowIndex(double[][] grid) { int minRow = 0; double minSum = Double.MAX_VALUE; for (int r = 0; r < grid.length; r++) { double currentSum = 0; for (double val : grid[r]) { currentSum += val; } if (currentSum < minSum) { minSum = currentSum; minRow = r; } } return minRow; } 04 Target Tracker (Strings) public static int countTarget(String[][] grid, String target) { int count = 0; for (String[] row : grid) { for (String word : row) { if (word.equals(target)) { count++; } } } return count; } 05 Absolute Peak (Double) public static double findMax(double[][] grid) { double max = grid[0][0]; for (double[] row : grid) { for (double val : row) { if (val > max) { max = val; } } } return max; }
Object Matrix WorksheetObject Matrix Worksheet AP Computer Science A Topic 8: 2D Arrays (Objects) Student Name Date Class References Student double getGPA() String getName() Car String getModel() int getYear() Person int getAge() String getName() 06 Valedictorian Search Write a method getTopStudent that finds and returns the Student object with the highest GPA in a 2D array. Assume the grid is not empty. public static Student getTopStudent(Student[][] classroom) { } 07 Fleet Counter Write a method countModel that returns the number of Car objects in the grid that match the given String modelName. public static int countModel(Car[][] lot, String modelName) { } 08 Senior Row Find the index of the row with the highest average age in a 2D array of Person objects. public static int oldestRowIndex(Person[][] people) { } 09 Empty Seat Detector In 2D object arrays, some elements might be null. Return true if there is at least one null element in the grid. public static boolean hasEmptySeats(Person[][] theater) { } 10 Modern Column Write a method colYearAvg that returns the average year of all Car objects in a specified column. public static double colYearAvg(Car[][] lot, int col) { }
Object Matrix KeyObject Matrix Answer Key Teacher Resource Topic 8: 2D Arrays (Objects) 06 Valedictorian Search public static Student getTopStudent(Student[][] classroom) { Student top = classroom[0][0]; for (Student[] row : classroom) { for (Student s : row) { if (s.getGPA() > top.getGPA()) { top = s; } } } return top; } 07 Fleet Counter public static int countModel(Car[][] lot, String modelName) { int count = 0; for (Car[] row : lot) { for (Car c : row) { if (c.getModel().equals(modelName)) { count++; } } } return count; } 08 Senior Row public static int oldestRowIndex(Person[][] people) { int maxIdx = 0; double maxAvg = 0; for (int r = 0; r < people.length; r++) { int sum = 0; for (Person p : people[r]) sum += p.getAge(); double avg = (double)sum / people[r].length; if (avg > maxAvg) { maxAvg = avg; maxIdx = r; } } return maxIdx; } 09 Empty Seat Detector public static boolean hasEmptySeats(Person[][] theater) { for (Person[] row : theater) { for (Person p : row) { if (p == null) return true; } } return false; } 10 Modern Column public static double colYearAvg(Car[][] lot, int col) { int sum = 0; for (int r = 0; r < lot.length; r++) { sum += lot[r][col].getYear(); } return (double)sum / lot.length; }
Object Matrix SlidesObject Matrix Grids of Objects & Null Logic Moving Beyond Numbers Method Calls Access element data using getters: grid[r][c].getSomeValue() Comparison Use .equals() for String properties, not ==. // Getting the age of person at [0][1] Person p = people[0][1]; int age = p.getAge(); The Null Reference Danger NullPointerException If an element in your grid is null, calling a method on it will crash your program. Safe Access if (grid[r][c] != null) Unsafe Access grid[r][c].getGPA() Object Mission Briefing 06 Valedictorian Search Student[][] → Student 07 Fleet Counter Car[][] → int 08 Senior Row Index Person[][] → int 09 Empty Seat Detector Object[][] → boolean 10 Modern Column Avg Car[][] → double Under the Hood What is actually stored? A 2D object array doesn't store the actual object data directly. It stores references (memory addresses) to where the objects live on the heap. matrix[0][0] → Address @4f2c (Student: Bob)
Object Blueprint SheetObject Blueprint Sheet AP Computer Science A Topic 8: Class Definitions These are the standard Java class definitions used in the Object Matrix Worksheet. Refer to these blueprints when writing your methods to ensure you are calling the correct getters and using the correct data types. Student Class public class Student { private String name; private double gpa; public Student(String n, double g) { name = n; gpa = g; } public String getName() { return name; } public double getGPA() { return gpa; } } Car Class public class Car { private String model; private int year; public Car(String m, int y) { model = m; year = y; } public String getModel() { return model; } public int getYear() { return year; } } Person Class public class Person { private String name; private int age; public Person(String n, int a) { name = n; age = a; } public String getName() { return name; } public int getAge() { return age; } }
Mastery Test CasesArray Mastery Test Cases Teacher Resource Validation Suite Lesson 1: Grid Logic (Primitives) Input Grid: Integer Matrix { {10, 20, 30}, {5, 5, 5}, {100, 0, 50} } 01. rowAverages {20.0, 5.0, 50.0} 02. maxColumnIndex Index: 0 (Sum: 115) Input Grid: Double Matrix { {1.5, 2.5}, {0.5, 0.2}, {10.5, 11.5} } 03. lowestRowIndex Index: 1 (Sum: 0.7) 05. findMax Value: 11.5 Input Grid: String Matrix Target: "Java" { {"Java", "Python", "Java"}, {"C++", "java", "Swift"} } 04. countTarget Result: 2 (Case-sensitive check) Lesson 2: Object Matrix (Grids of Objects) 06. getTopStudent Input { {new Student("Alice", 3.8), new Student("Bob", 3.2)}, {new Student("Charlie", 4.0), new Student("Diana", 3.9)} } Expected Result: Student object "Charlie" Car Grid Input (Lot) { {new Car("Civic", 2020), new Car("F150", 2018)}, {new Car("Civic", 2022), new Car("Civic", 2015)} } 07. countModel("Civic") Result: 3 10. colYearAvg(1) Result: 2016.5 Person Grid Input (Theater) { {new Person("Jan", 20), null}, {new Person("Lee", 60), new Person("Kim", 40)} } 08. oldestRowIndex Index: 1 (Avg: 50) 09. hasEmptySeats Result: true