'F' → Fullscreen
grid[row][col]. for loops (row-major order). grid.length, columns use grid[r].length. // Figure 1 — A small 2D array you can picture as a gridint[][] grid ={ {3, 5, 7}, {1, 4, 9}, {8, 6, 2}};
// grid has 3 rows and 3 columns// grid[0][0] is 3// grid[1][2] is 9// grid[2][1] is 6// Figure 2 — Access + update one elementint[][] seats ={ {0, 0, 1, 0}, {0, 1, 1, 0}, {0, 0, 0, 0}};
// 1 means occupied, 0 means emptySystem.out.println(seats[1][1]); // prints 1
// Mark row 2, col 0 as occupiedseats[2][0] = 1;// Figure 3 — Nested loop traversal template (Allman braces)for (int r = 0; r < grid.length; r++){ for (int c = 0; c < grid[r].length; c++) { // use grid[r][c] }}public class Program{ public static void main(String[] args) { // Task 1 — Grid Basics (Access + Update) // // You are given a 3x3 grid of integers. // 1) Print the center element (row 1, col 1) // 2) Print the top-right element (row 0, col 2) // 3) Change the bottom-left element (row 2, col 0) to 99 // 4) Print the updated bottom-left element to confirm
int[][] grid = { {3, 5, 7}, {1, 4, 9}, {8, 6, 2} };
System.out.println("Center (1,1): " + grid[1][1]); System.out.println("Top-right (0,2): " + grid[0][2]);
grid[2][0] = 99;
System.out.println("Updated bottom-left (2,0): " + grid[2][0]); }}public class Program{ public static void main(String[] args) { // Task 2 — Print the Grid Nicely // // Print the grid so it looks like rows and columns. // Example: // 3 5 7 // 1 4 9 // 8 6 2
int[][] grid = { {3, 5, 7}, {1, 4, 9}, {8, 6, 2} };
for (int r = 0; r < grid.length; r++) { for (int c = 0; c < grid[r].length; c++) { System.out.print(grid[r][c] + " "); } System.out.println(); } }}public class Program{ public static void main(String[] args) { // Task 3 — Grid Stats (Sum, Max, Count) // // Using traversal: // 1) Compute the sum of ALL values in the grid // 2) Find the maximum value // 3) Count how many values are even
int[][] grid = { {3, 5, 7}, {1, 4, 9}, {8, 6, 2} };
int sum = 0; int max = grid[0][0]; int evenCount = 0;
for (int r = 0; r < grid.length; r++) { for (int c = 0; c < grid[r].length; c++) { int value = grid[r][c]; sum += value;
if (value > max) { max = value; }
if (value % 2 == 0) { evenCount++; } } }
System.out.println("Sum: " + sum); System.out.println("Max: " + max); System.out.println("Even count: " + evenCount); }}public class Program{ public static void main(String[] args) { // Task 4 — Row Totals (Practical: Weekly Steps) // // Each row is a person. // Each column is a day of the week (Mon-Sun). // // 1) Print each person's total steps for the week. // 2) Print which person (row index) had the highest weekly total.
int[][] steps = { {5200, 6100, 4300, 7000, 8000, 12000, 9000}, // Person 0 {8000, 7800, 8200, 7900, 8100, 8300, 7600}, // Person 1 {3000, 4500, 5000, 4000, 6500, 7000, 6000} // Person 2 };
int bestPerson = 0; int bestTotal = 0;
for (int r = 0; r < steps.length; r++) { int rowSum = 0;
for (int c = 0; c < steps[r].length; c++) { rowSum += steps[r][c]; }
System.out.println("Person " + r + " weekly total: " + rowSum);
if (r == 0 || rowSum > bestTotal) { bestTotal = rowSum; bestPerson = r; } }
System.out.println("Highest weekly total: Person " + bestPerson); System.out.println("Total steps: " + bestTotal); }}import java.util.Scanner;
public class Program{ public static void main(String[] args) { // Task 5 — Gradebook Mini App (Practical) // // Rows = students // Cols = assignments // // 1) Print each student's average. // 2) Ask the user for a student number (0-2) and print that student's scores. // // NOTE: We'll keep it small so it's easy to trace.
int[][] grades = { {90, 82, 88, 100}, // Student 0 {70, 85, 79, 92}, // Student 1 {95, 91, 93, 89} // Student 2 };
// 1) Student averages for (int r = 0; r < grades.length; r++) { int sum = 0;
for (int c = 0; c < grades[r].length; c++) { sum += grades[r][c]; }
double avg = (double) sum / grades[r].length; System.out.println("Student " + r + " average: " + avg); }
// 2) Lookup a student row and print their scores Scanner scanner = new Scanner(System.in); System.out.print("Enter a student number (0-2): "); int student = scanner.nextInt();
if (student < 0 || student >= grades.length) { System.out.println("Invalid student number."); } else { System.out.print("Student " + student + " scores: "); for (int c = 0; c < grades[student].length; c++) { System.out.print(grades[student][c] + " "); } System.out.println(); }
scanner.close(); }}public class Program{ public static void main(String[] args) { // Task 6 — Seat Map (Practical + Visual) // // 0 means empty seat, 1 means occupied seat. // // 1) Count how many seats are occupied. // 2) Count how many seats are empty. // 3) Print the coordinates (row, col) of every occupied seat. // // This is a very common "grid scanning" pattern.
int[][] seats = { {0, 0, 1, 0}, {1, 1, 1, 0}, {0, 0, 0, 0} };
int occupied = 0; int empty = 0;
for (int r = 0; r < seats.length; r++) { for (int c = 0; c < seats[r].length; c++) { if (seats[r][c] == 1) { occupied++; System.out.println("Occupied at (" + r + ", " + c + ")"); } else { empty++; } } }
System.out.println("Occupied seats: " + occupied); System.out.println("Empty seats: " + empty); }}Your program output should something similar to the sample output below.
Center (1,1): 4Top-right (0,2): 7Updated bottom-left (2,0): 99
3 5 71 4 98 6 2
Sum: 45Max: 9Even count: 4
Person 0 weekly total: 51600Person 1 weekly total: 55900Person 2 weekly total: 36000Highest weekly total: Person 1Total steps: 55900
Student 0 average: 90.0Student 1 average: 81.5Student 2 average: 92.0Enter a student number (0-2): 1Student 1 scores: 70 85 79 92
Occupied at (0, 2)Occupied at (1, 0)Occupied at (1, 1)Occupied at (1, 2)Occupied seats: 4Empty seats: 8You may write your reflection answers as comments at the bottom of your code.
grid.length and grid[r].length?rowSum back to 0 for each new row?Submit your activity and reflection answers to the appropriate dropbox.