/** * Grid.java * CS231 - Lab 2: 2D Array Practice * * Practice accessing, traversing, modifying, and transforming 2D arrays. * * Name: ______________________________ */ public class Grid { /* * The methods in this lab do different things to a backing grid: * * READ - examine the grid without changing it * MODIFY - change values in the existing grid * TRANSFORM IN PLACE - rearrange values in the existing grid * MODEL -> VIEW - display the backing data without changing it */ /** * Task 1: Print every value in the grid. * TYPE: READ - do not modify the grid. * * Print one row per line and put one space between values. * * Example input: * { * {0, 2, 4}, * {1, 3, 5} * } * * Output: * 0 2 4 * 1 3 5 * * Hint: Use one loop for the rows and another loop for the columns. */ public static void printGrid(int[][] grid) { // TODO Task 1 } /** * Task 2: Count occurrences of a value in the entire grid. * TYPE: READ - do not modify the grid. * * Example input: * { * {1, 0, 1}, * {0, 1, 2} * } * * countValue(grid, 1) returns 3 * countValue(grid, 2) returns 1 * countValue(grid, 7) returns 0 * * Hint: Traverse every location. When grid[row][col] equals value, * increase a counter. */ public static int countValue(int[][] grid, int value) { // TODO Task 2 return 0; } /** * Task 3A: Change every cell in one row to value. * TYPE: MODIFY - change the original grid. * * Example input: * { * {1, 2, 3, 4}, * {5, 6, 7, 8}, * {9, 0, 1, 2} * } * * After setRow(grid, 1, 7): * { * {1, 2, 3, 4}, * {7, 7, 7, 7}, * {9, 0, 1, 2} * } * * Hint: The row stays fixed. Which index needs to change? */ public static void setRow(int[][] grid, int row, int value) { // TODO Task 3A } /** * Task 3B: Change every cell in one column to value. * TYPE: MODIFY - change the original grid. * * Example input: * { * {1, 2, 3, 4}, * {5, 6, 7, 8}, * {9, 0, 1, 2} * } * * After setColumn(grid, 2, 9): * { * {1, 2, 9, 4}, * {5, 6, 9, 8}, * {9, 0, 9, 2} * } * * Hint: The column stays fixed. Which index needs to change? */ public static void setColumn(int[][] grid, int col, int value) { // TODO Task 3B } /** * Task 4: Set every cell on the outside border of the grid to 0. * TYPE: MODIFY - change the original grid. * Do not change cells in the interior. * * Example input: * 5 5 5 5 5 * 5 1 2 3 5 * 5 4 5 6 5 * 5 5 5 5 5 * * After clearBorder(grid): * 0 0 0 0 0 * 0 1 2 3 0 * 0 4 5 6 0 * 0 0 0 0 0 * * A location is on the border when it is in: * - the first row, OR * - the last row, OR * - the first column, OR * - the last column. * * Hint: Traverse the grid and ask whether each [row][col] is on * one of those four edges. */ public static void clearBorder(int[][] grid) { // TODO Task 4 } /** * Task 5: Display the backing array as a user-facing view. * TYPE: READ - do not modify the grid. * * The int[][] is the MODEL: it stores the actual data. * This method creates a VIEW of that data for the user. * * Backing value 0 is displayed as . * Backing value 1 is displayed as # * * Example backing array: * 0 1 0 * 1 1 0 * * User-facing output: * . # . * # # . * * Notice that this method does NOT replace the 0s and 1s in grid. * It only chooses what character to print for each value. */ public static void displayGrid(int[][] grid) { // TODO Task 5 // Traverse every location in the backing array. // If the value is 0, print ".". // If the value is 1, print "#". // Print one row of the model per line. } /** * Task 6: Transpose a SQUARE grid IN PLACE. * * TYPE: TRANSFORM IN PLACE * This method MODIFIES the original grid. Do not create a new grid. * You may assume that the grid is square. * * Example before transpose(grid): * 1 2 3 * 4 5 6 * 7 8 9 * * The SAME grid after transpose(grid): * 1 4 7 * 2 5 8 * 3 6 9 * * Think about pairs of locations across the main diagonal: * * grid[0][1] <--> grid[1][0] 2 <--> 4 * grid[0][2] <--> grid[2][0] 3 <--> 7 * grid[1][2] <--> grid[2][1] 6 <--> 8 * * Values on the main diagonal do not move: * grid[0][0], grid[1][1], grid[2][2], ... * * IMPORTANT: Each pair should be swapped only ONCE. If you later * swap the same pair in the opposite direction, you undo your work. * This means you do NOT want to visit every [row][col] pair. * * Hint: Visit only the locations on one side of the main diagonal. * You will need a temporary int when swapping two values. */ public static void transpose(int[][] grid) { // TODO Task 6 } /** * Use main() as your testing space while you work. * Create grids, call your methods with different inputs, and inspect * the results before running GridTester. * * You can also pass values from the command line. For example: * * java Grid 2 7 * * args[0] will be "2" and args[1] will be "7". Convert them with * Integer.parseInt() before passing them to a method expecting int. */ public static void main(String[] args) { int[][] grid = { {1, 0, 1}, {0, 1, 0}, {1, 1, 0} }; // This is your testing space. Add and change method calls as you work. // Example: // printGrid(grid); // Example command-line experiment: // if (args.length == 2) { // int row = Integer.parseInt(args[0]); // int value = Integer.parseInt(args[1]); // setRow(grid, row, value); // printGrid(grid); // } } }