QUESTION 1: ARRAYS/ARRAYLISTS

PART A: Write a static method arraySum that calculates and returns the sum of the entries in a specified one-dimensional array. The following example shows an array arr1 and the value returned by a call to arraySum.

public class Tester {
    public static void main(String[] args) {
        // Fantasy points for drafted players
        int[] arr = {25, 15, 17, 19};

        int sum = arraySum(arr);

        System.out.println("Your fantasy team will get: " + sum + " points");
    }
}

public static int arraySum(int[] arr) {
    int sum = 0;
    for (int i = 0; i < arr.length; i++) {
        sum += arr[i];
    }
    return sum;

}

Tester.main(null);
Your fantasy team will get: 76 points

PART B: Write a static method rowSums that calculates the sums of each of the rows in a given two-dimensional array and returns these sums in a one-dimensional array. The method has one parameter, a two-dimensional array arr2D of int values. The array is in row-major order: arr2D [r] [c] is the entry at row r and column c. The method returns a one-dimensional array with one entry for each row of arr2D such that each entry is the sum of the corresponding row in arr2D. As a reminder, each row of a two-dimensional array is a one-dimensional array.

public class Main {
    public static void main(String[] args) {
        int[][] arr2D = {
            {5, 7, 5, 5},
            {15, 22, 28, 17},
            {7, 8, 4, 13}
        };

        int[] sums = rowSums(arr2D);

        System.out.println("League Stats:");
        for (int i = 0; i < sums.length; i++) {
            System.out.println("Team " + i + ": " + sums[i]);
        }
    }
}

public static int[] rowSums(int[][] arr2D) {
    int numRows = arr2D.length;
    int[] sums = new int[numRows];

    for (int i = 0; i < numRows; i++) {
        sums[i] = arraySum(arr2D[i]);
    }

    return sums;
}

Main.main(null);
League Stats:
Team 0: 22
Team 1: 82
Team 2: 32

PART C: A two-dimensional array is diverse if no two of its rows have entries that sum to the same value. In the following examples, the array mat1 is diverse because each row sum is different, but the array mat2 is not diverse because the first and last rows have the same sum. Write a static method isDiverse that determines whether or not a given two-dimensional array is diverse. The method has one parameter: a two-dimensional array arr2D of int values. The method should return true if all the row sums in the given array are unique; otherwise, it should return false. In the arrays shown above, the call isDiverse (mat1) returns true and the call isDiverse(mat2) returns false.

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        // 2d arrays
        int[][] mat1 = {
            {1, 3, 2},
            {4, 3, 1},
            {2, 1, 4}
        };
        
        int[][] mat2 = {
            {1, 3, 2},
            {4, 3, 1},
            {1, 3, 2}
        };
        
        // Testing isDiverse method
        System.out.println("mat1 is diverse: " + isDiverse(mat1)); // true
        System.out.println("mat2 is diverse: " + isDiverse(mat2)); // false
    }

    // given method from frq
    public static boolean isDiverse(int[][] arr2D) {
        int[] rowSums = rowSums(arr2D); // calculating sums
        
        // sorting row sums
        Arrays.sort(rowSums);
        
        // are any row sums equal??
        for (int i = 0; i < rowSums.length - 1; i++) {
            if (rowSums[i] == rowSums[i + 1]) {
                return false; // NOT diverse
            }
        }
        
        return true; // IS diverse.
    }

    // finding the actual sums of each row (FROM PART B)
    public static int[] rowSums(int[][] arr2D) {
        int numRows = arr2D.length; 
        int[] sums = new int[numRows];

      
        for (int i = 0; i < numRows; i++) {
            
            sums[i] = arraySum(arr2D[i]);
        }

        return sums; 
    }

    // METHOD FROM PART A
    public static int arraySum(int[] arr) {
        int sum = 0;
        for (int i = 0; i < arr.length; i++) {
            sum += arr[i];
        }
        return sum;
    }
}


Main.main(null);
mat1 is diverse: true
mat2 is diverse: false

Throughout this FRQ, we had to utilize static methods to perform operations on 1D and 2D Arrays/Array lists. This involved sorting the elements inside of the arrays and computing sums in order to find differences between different rows of multiple arrays. Using iterative loops throughout the FRQs was necessary in order to iterate through the different elements in the arrays, and it made the job quite easy. Once part (a) and part (b) were completed, they needed to be merged into the code for part (c) so that it could be run as a whole program (DiverseArray).

To run the code in each of the parts, I treated it as our group’s final project - where we had a fantasy basketball simulator. Each of the arrays contained the amount of fantasy points that each player generated on a team, and then many operations could be performed - when checking for diverse arrays, that could be compared to seeing if two teams are going to tie with each other, or when checking for row sums, that could simply be calculating the total number of projected fantasy points a team is going to receive.