2015 FRQ #1

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 ArrayAdd {

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

    public static void main() {
        int[] array = {3,2,1,8,10,2};
        int sum = Add(array);
        System.out.println("For the following array");
        System.out.println(" ");
        for (int i = 0; i < array.length; i++) {
            System.out.print(" " + array[i] + ",");
        }
        System.out.println(" "); System.out.println(" ");
        System.out.println("The sum is: " + sum);
    }
    
}
ArrayAdd.main();
For the following array
 
 3, 2, 1, 8, 10, 2, 
 
The sum is: 26

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.

class RowSummation {
    public static int[] rowSums(int[][] arr2D) {
        int[] output = new int[arr2D.length];
        // For loop for each int array in the array
        for (int i = 0; i < arr2D.length; i++) {
            int isum = 0;
            // For loop for each int in the int[]
            isum = ArrayAdd.Add(arr2D[i]);
            //Append isum to array
            output[i] = isum;
        }

        return output;
    }

    public static void main() {
        int[][] arr2D = new int[][] {
            {1,2,3},
            {1,2,3,4},
            {5,4,3,2,1},
            {6,7,2,1},
            {9,9,11}
        };
        System.out.println("The array sums are: ");
        int[] result = rowSums(arr2D);
        for (int i = 0; i < result.length; i++) {
            System.out.print(" " + result[i] + ",");
        }
        
    }
}

RowSummation.main();
The array sums are: 
 6, 10, 15, 16, 29,

c.) Write a static method isDiverse that determines whether or not a given two-dimensional array is diverse.

public class Diversity {
    public static boolean isDiverse(int[][] arr2D) {

        // Defining isDiverse boolean and getting the result int[]
        boolean isDiverse = true;
        int[] result = RowSummation.rowSums(arr2D);

        // Loops through result[]
        for (int i = 0; i < result.length; i++) {
            int orig = result[i];
            // Loops through all values greater than result[i]
            for (int j = i + 1; j < result.length; j++) {
                // If statement to compare sums
                if (orig == result[j]) {
                    // If same sum
                    isDiverse = false;
                }
            }
        }
        return isDiverse;
    }
    
    public static void main() {
        // Test should be false
        int[][] arr2D = {{1,2,3},{3,2,1},{6,5,4}};
        boolean diverse = isDiverse(arr2D);
        System.out.println(diverse);
        // Test should be true
        int[][] arr2D2 = {{1,2,3},{9,10,11},{6,5,4}};
        boolean diverse2 = isDiverse(arr2D2);
        System.out.println(diverse2);

    }
}

Diversity.main();
false
true