FRQ #1: Diverse Array

Question 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.

(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.

(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;

// Type of FRQ =  2D Array 
public class DiverseArray {
    public static int arraySum(int[] arr) { //Part A
        int sum = 0;
        for (int i : arr) { //Complete Search
            sum += i;
        }
        return sum;
    }

    public static int[] rowSums(int[][] arr2D) { //Part B
        int[] finalArr = new int[arr2D.length];
        for (int i = 0; i < arr2D.length; i++) { //Complete Search
            int sum = arraySum(arr2D[i]);
            finalArr[i] = sum;
        }
        return finalArr;
    }
    public static boolean isDiverse(int [][] arr2D){ //Part C
        int[] finalArr = rowSums(arr2D);
        int len = finalArr.length;
        for (int i  = 0; i<len-1;i++){ //Complete Search with Nested For Loops
            for (int j = i+1; j <len;j++){
                if (finalArr[i] == finalArr[j]){ 
                    return false;
                }
            }
        }
        return true;
    }
    //What makes the Code Run
    public static void Main(String[] args) {
        int[] values1 = {75,83,87,91};
        System.out.println(arraySum(values1));
        int[][] values2 = {
            {90,85,92,78},
            {88,91,89,95},
            {91,95,89,98},
            {72,75,83,85},
            {37,61,73,54}
        };
        int[] results = rowSums(values2);
        for (int i = 0; i<results.length;i++){
            System.out.print(Integer.toString(results[i])+" ");
        }
        System.out.println();
        System.out.println("Is it diverse: " + isDiverse(values2));
    }
}
DiverseArray.Main(null);
336
345 363 373 315 225 
Is it diverse: true

Extra

public static int arraySum(int[] arr) { //Part A
    int sum = 0;
    for (int i : arr) { //Complete Search
        sum += i;
    }
    return sum;
}

public static int[] rowSums(int[][] arr2D) { //Part B
    int[] finalArr = new int[arr2D.length];
    for (int i = 0; i < arr2D.length; i++) { //Complete Search
        int sum = arraySum(arr2D[i]);
        finalArr[i] = sum;
    }
    return finalArr;
}
public static boolean isDiverse(int [][] arr2D){ //Part C
    int[] finalArr = rowSums(arr2D);
    int len = finalArr.length;
    for (int i  = 0; i<len-1;i++){ //Complete Search with Nested For Loops
        for (int j = i+1; j <len;j++){
            if (finalArr[i] == finalArr[j]){ 
                return false;
            }
        }
    }
    return true;
}
public void Extra(){
    // Class Averages
    int[] classScores = {75,83,87,91};
    System.out.print(arraySum(classScores));
    System.out.println("/400 is the class average");

   //Student Scores
    int[][] student = {
    {90,85,92,78},
    {88,91,89,95},
    {91,95,89,98},
    {72,75,83,85},
    {37,61,73,54}
   };
   String[] studentNames = {"Jerry","Bobby","John","Ted","Amanda"};
   int[] studentScores = rowSums(student);
   int max = 0;
   int highestScorer = 0;
   System.out.println("The scores each person got are: ");
   for (int i = 0; i<studentScores.length;i++){
    System.out.println(studentNames[i] + ": "+ Integer.toString(studentScores[i]) + " ");
    if (studentScores[i]/(student[0].length) > max){
        max = studentScores[i]/(student[0].length);
        highestScorer = i;
    }
   }
   boolean diverse = isDiverse(student);
   if (diverse){
    System.out.println("All of the scores are different from each other");
   } else{
    System.out.println("At least two scores are the same");
   }
   System.out.println("The highest scorer is " + studentNames[highestScorer] + " with an average of " + Integer.toString(max));
}
Extra();
336/400 is the class average
The scores each person got are: 
Jerry: 345 
Bobby: 363 
John: 373 
Ted: 315 
Amanda: 225 
All of the scores are different from each other
The highest scorer is John with an average of 93