Hack: Create method that sets all elements in array to n

// Function to set all elements in array to specified integer
void setArray(int[] arr, int n) {
    for (int i = 0; i < arr.length; i++) {
        arr[i] = n;
    }
}

int[] array = new int[10];

// Set all elements to 10
setArray(array, 10);

for (int i = 0; i < array.length; i++) {
    System.out.println(array[i]);
}

// Should print all 10s when working properly
10
10
10
10
10
10
10
10
10
10

Hack: Write an array to find the average of an array

//Example finding the average an array. 

//Finds the average of an array
public static double average(int[] array) {
    // Initialize sum to be 0
    int sum = 0;
    // Add all elements to sum
    for (int item : array) {
        sum += item;
    }
    // Divide sum by total number of elements to get the average. Note the double cast
    double average = (double) sum/array.length;
    return average;
}

//tester array
int[] test = {3, 5, 7, 2, 10};

//returns 5.4
System.out.println(average(test));
5.4

Hack: Find the average number of a diagonal in a 2d array

For example, here find the average of the bolded #s
1 2 3 4
5 6 7 8
9 1 2 3

public static double averageDiagonal (int[][] array2D) {
    // Initialize important variables to control the function
    int sum = 0;
    int index = 0;
    int count = 0;
    // Loop through the 2d array by checking each row
    for (int[] array : array2D) {
        // Using the index variable, check the elements in a diagonal (going bottom right), 
        // adding them to the sum until there is no more elements in the current diagonal position
        try {
            sum += array[index];
            index++;
            count++;
        } catch (Exception e) {
            break;
        }
    }
    // Divide sum by total number of elements and double cast in order to find the average value
    double average = (double) sum/count;
    return average;
}

int[][] arr = {
    {1,2,3,4,5,6},
    {7,8,9,10,11,12},
    {0,1,2,3,4,5},
    {10,11,12,13,14,15},
    {15,16,17,18,19,20}
};

System.out.println(averageDiagonal(arr));
8.6

FRQ 2018 Q4

public class ArrayTester {
    public static int[] getColumn(int[][] arr2D, int c) {
        // Create 1D array that will contain the column values
        int[] column = new int[arr2D.length];
        // Add all values in the specified column to the column array. Iterate through each row
        for (int i = 0; i < arr2D.length; i++) {
            column[i] = arr2D[i][c];
        }
        return column;
    }   

    public static boolean hasAllValues(int[] arr1, int[] arr2) {
        // Implementation not shown
    }

    public static boolean containsDuplicates(int[] arr) {
        // Implementation not shown
    }

    public static boolean isLatin(int[][] square) {
        // Get the first row array of the 2D array
        int[] firstRow = square[0];
        // Check if the first row contains any duplicate values
        if (containsDuplicates(firstRow) != true) {
            return false;
        }
        // For each row under the first, check if it contains all of the values of the first row
        for (int i = 1; i < square.length; i++) {
            if (hasAllValues(firstRow, square[i]) != true) {
                return false;
            }
        }
        // For each column in the square, check if it contains all of the values of the first row
        for (int j = 0; j < square[0].length; j++) {
            int[] column = getColumn(square, j);
            if (hasAllValues(firstRow, column) != true) {
                return false;
            }
        }
        return true;
    }
}

FRQ 2017 Q4

// Position class that will be processed by the Successors class
public class Position {
    public Position(int r, int c) {
        // Implementation not shown
    }
}

public class Successors {
    // Static function to return the position (row and column) of the searched value
    public static Position findPosition(int num, int[][] intArr) {
        boolean found = false;
        // Iterate through rows and columns to search through every element of the 2D array
        for (int i = 0; i < intArr.length; i++) {
            for (int j = 0; j < intArr[i].length; j++) {
                // Stop the search once the element is found
                if (intArr[i][j] == num) {
                    found = true;
                    Position positionObject = new Position(i, j);
                    break;
                }
            }
        }
        // Return null if the element is not found
        if (found == false) {
            return null;
        } else {
            return positionObject;
        }
        
    }

    // Static function
    public static Position[][] getSuccessorArray(int[][] intArr) {
        // Instantiate new array with the same dimensions as intArr
        Position[][] successorArray = new Position[intArr.length][intArr[0].length];
        // Set a Position object (position of respective intArr element + 1) for each element of successorArray
        for (int i = 0; i < successorArray.length; i++) {
            for (int j = 0; j < successorArray[i].length; j++) {
                successorArray[i][j] = findPosition(intArr[i][j] + 1, intArr);
            }
        }
        
        return successorArray;
    }
}

FRQ 2016 Q4

public class StringFormatter {
    public static int totalLetters(List<String> wordList) {
        int total = 0;
        // Add the length of every word in wordList to get the total letters
        for (int i = 0; i < wordList.size(); i++) {
            total += wordList.get(i).length();
        }
        return total;
    }

    public static int basicGapWidth(List<String> wordList, int formattedLen) {
        // Get the number of gaps for the wordList
        int numGaps = wordList.size() - 1;
        // Get the number of spaces by subtracting the total letters from the formatted sentence length
        int numSpaces = formattedLen - totalLetters(wordList);
        // Divide using int type to get rid of remainders, effectively getting the basic gap width
        int gapWidth = numSpaces/numGaps;
        return gapWidth;
    }

    public static int leftoverSpaces(List<String> wordList, int formattedLen) {
        // Implementation not shown
    }

    public static String format(List<String> wordList, int formattedLen) {
        // Instantiate empty string that will ultimately contain the formatted string
        String formattedString = "";
        // Get leftover spaces
        int leftover = leftoverSpaces(wordList, formattedLen);
        // Get basic gap width
        int gapWidth = basicGapWidth(wordList, formattedLen);
        for (int i = 0; i < wordList.size(); i++) {
            // Add each word to formattedString
            formattedString += wordList.get(i);
            // If not the last word, add the basic gap width, as well as a leftover space if applicable
            if (i < wordList.size() - 1) {
                for (int j = 0; j < gapWidth; j++) {
                    formattedString += " ";
                }
                if (leftover > 0) {
                    formattedString += " ";
                    leftover--;
                }
            }
        }

        return formattedString;
    }
}