Part A

(a) Write the repopulate method, which assigns a newly generated random value to each element of grid. Each value is computed to meet all of the following criteria, and all valid values must have an equal chance of being generated. • The value is between 1 and MAX, inclusive. • The value is divisible by 10. • The value is not divisible by 100. Complete the repopulate method.

public void repopulate()
{
    Random rand = new Random();
    for (int row = 0; row < grid.length; row++)
    {
        for (int col = 0; col < grid[0].length; col++)
        {
            while (true)
            {
                int randNum = rand.nextInt(MAX) + 1; 
                if (randNum % 10 == 0 && randNum % 100 != 0)
                {
                    grid[row][col] = randNum;
                    break;
                }
            }
        }
    }
}

The repopulate method scans throughout the array and generates a random integer at each index. Then it checks if the integer is divisible by 10 and not divisible by 100, and if it satisfies this condition, the loop moves on to the next index.

Part B

(b) Write the countIncreasingcols method, which returns the number of columns in grid that are in increasing order. A column is considered to be in increasing order if the element in each row after the first row is greater than or equal to the element in the previous row. A column with only one row is considered to be in increasing order. The following examples show the count Increasingcols return values for possible contents of grid. The return value for the following contents of grid is 1, since the first column is in increasing order but the second and third columns are not.

10 50 40
20 40 20
30 50 30

The return value for the following contents of grid is 2, since the first and third columns are in increasing order but the second and fourth columns are not.

10 540 440 440
220 450 440 190

Complete the countincreasingCols method.

public int countIncreasingCols() { //Part B
    int ans = 0;
    for (int col = 0; col < grid[0].length; col++)
    {
        boolean increasing = true;
        for (int row = 1; row < grid.length; row++) 
        {
            if (grid[row][col] < grid[row-1][col]) 
            {
                increasing = false;
                break;
            }
        }
        if (increasing)
        {
            ans++;
        }
    }
    return ans;
}

In the countIncreasingCols, row scan through the columns first (grid[0].length yields the total number of columns) and then scan through each row in each column (grid.length yields the total number of rows). row set the increasing boolean as a flag to ensure that, if the column is not in increasing order, the answer is not increased. Then row checked if every element in the column is greater than or equal to the previous element to see if it is increasing order, changing the increasing boolean to false if it is not. Then row output the number of increasing columns.

Fully Working Answer

import java.util.*;

public class Data {
    private int[][] grid;
    private int MAX;

    public Data(int rows, int cols, int max) {
        this.grid = new int[rows][cols];
        this.MAX = max;
    }
    public void repopulate() { // Part A
        Random rand = new Random();
        for (int row = 0; row < grid.length; row++) {
            for (int col = 0; col < grid[0].length; col++) // 1 point for traversing the grid
            {
                while (true) {
                    int randNum = rand.nextInt(MAX) + 1; // 1 point for getting rand
                    if (randNum % 10 == 0 && randNum % 100 != 0) // 1 point for ensuring /10 and /- 100
                    {
                        grid[row][col] = randNum; // 1 point for assigning the value
                        break;
                    }
                }
            }
        }
    }

    public void displayGrid() {
        for (int row = 0; row < grid.length; row++) {
            for (int col = 0; col < grid[0].length; col++) {
                System.out.print(grid[row][col] + " ");
            }
            System.out.println();
        }

        System.out.println("Print the array backwards");

        for (int row = grid.length - 1; row >= 0; row--) {
            for (int col = grid[row].length - 1; col >= 0; col--) {
                System.out.print(grid[row][col] + " ");
            }
            System.out.println();
        }
    }

    public int countIncreasingCols() { // Part B
        int ans = 0;
        for (int col = 0; col < grid[0].length; col++) {
            boolean increasing = true;
            for (int row = 1; row < grid.length; row++) // 1 point for going thru columns
            {
                if (grid[row][col] < grid[row - 1][col]) // 1 point for comparing elements in the grid, 1 point for detecting if the column is increasing
                {
                    increasing = false;
                    break;
                }
            }
            if (increasing) {
                ans++; // 1 point for counting all columns that are increasing
            }
        }
        return ans; // 1 point for returning the answer
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        System.out.print("Enter the number of rows: ");
        int rows = scanner.nextInt();
        
        System.out.print("Enter the number of columns: ");
        int cols = scanner.nextInt();
        
        System.out.print("Enter the maximum value for random integers: ");
        int max = scanner.nextInt();
        
        Data data = new Data(rows, cols, max);
        data.repopulate();
        
        System.out.println("Generated Grid:");
        data.displayGrid();
        
        int increasingCols = data.countIncreasingCols();
        System.out.println("Number of Increasing Columns: " + increasingCols);
    }
}

Data.main(null);
Enter the number of rows: Enter the number of columns: Enter the maximum value for random integers: Generated Grid:
70 3060 3120 4840 
760 4970 3750 3720 
4390 1840 3350 4310 
Print the array backwards
4310 3350 1840 4390 
3720 3750 4970 760 
4840 3120 3060 70 
Number of Increasing Columns: 1

Improvements

For part A, row should have changed the row and col in the loops to be named row and col because it makes it easier to read and understand in the array. The answers also used the Math.random object instead of the Random class. row think my choice was better in this case because the Random class let me directly create a random integer instead of the Math.random making a double. row would have gotten a 4/4 on this part.

For part B, instead of breaking or continuing a loop and creating a label, row could have added a boolean that determines whether the elements in the column are already ordered. row also should have labelled the loop variables “column” and “row” instead of row and col so that it is easier to understand and work with. If row had done this row wouldn’t have made the mistake row did, where row added the increment instead of subtracting it, which would have gotten the previous value to compare with the current value. row would have gotten a 4/5 on this part.

Crossover Notes

A-REEL

  • 2022 Classes:
    • Subclass textbook (extends Book)
    • Call super function: get method from parent class eg: super.getBookInfo()
  • 2023 Arrays/ArrayLists:
    • Records highest temp from each day
    • Modify arraylist to suit parameters
    • Iterate backwords thru the arraylist bc removing from the arraylist causes size change
  • Aliya
    • Check for candy at each element
    • Good diagrams
    • Detailed explanation of removeNextByFlavor (based on specific flavor)
  • Raunak
    • Range of consecutive ints
    • Contains for if given number in group
    • Running code/expected output

      VACTQ

  • Vinay
    • Running code
    • WordMatch with ScoreGuess based on frequency
  • Haoxuan
    • CombinedTable class
    • Running code
    • Methods to accomplish seating and desirability
  • Colin
    • Creates getters and setters
    • No running code
    • Good comments and documentation
    • Images did not work
  • Aaron
    • Resize 2D arr based on number of 0s
    • No running code