Question 1

This FRQ is about 2D Arrays

public class DiverseArray 
{
    public static int arraySum(int[] array)  // research what static means 
    {
        int sum = 0;
        for (int i : array)  // loop through an array
        {
            sum += i;  // add each item to a predefined sum variable
        }
        return sum;
    }
    public static int[] rowSums(int[][] arr2D)
    {
        int[] arraysum = new int[arr2D.length];
        int tempsum = 0;  // temporary sum for each loop
        int counter = 0;  // counter for each loop
        for (int[] i : arr2D)
        {
            tempsum = 0;  // resets the temporary sum before each loop
            for (int b : i)
            {
                tempsum += b;  //add each item of an array to the temporary sum
            }
            arraysum[counter] = tempsum;  //record that temporary sum in an array
            counter += 1;
        }
        return arraysum;
    }
    public static boolean isDiverse(int[][] arr2D) 
    {
        HashSet<Integer> diversecheck = new HashSet<Integer>();  // hashsets can't have dupllicate values
        int tempsum = 0;
        for (int[] i : arr2D)
        {
            tempsum = 0;
            for (int b : i)
            {
                tempsum += b;  //add each value the same way as rowSums
            }
            diversecheck.add(tempsum); // add the sum to a hashset instead
        }
        if (diversecheck.size() == arr2D.length)
        {
            return true;  // if the hashset has the same amount of sums as the 2d array has rows, then it returns true
        } else {
            return false;  // if the hashset has less sums than the 2d array has rows, that means that one or more of the sums was a duplicate
        }
    }
    
    public static void main(String[] args)
    {
        int[] arr1 = {1, 3, 2, 7, 3};
        System.out.println(arraySum(arr1));
        for (int i : rowSums(mat1))
        {
            System.out.print(i);
            System.out.print(", ");
        }
        System.out.println();
        System.out.println(isDiverse(mat1));
        System.out.println(isDiverse(mat2));
    }
}
DiverseArray.main(null)
16
16, 32, 28, 20, 
true
false

Question 2

This FRQ is about classes

public class HiddenWord
{
    public static void getHint(String uinp, String secretword)
    {
        uinp.toUpperCase();
        char[] secret = secretword.toCharArray(); // making secret word into a char array of each letter 
        char[] user = uinp.toCharArray(); // making the user input into a char array of each letter
        String[] hint = new String[secretword.length()];

        for (int i = 0; i < secretword.length(); i++)
        {
            hint[i] = "*"; // filling each hint character with the star
        }

        for (int i = 0; i < secretword.length(); i++)
        {
            if (secret[i] == user[i]) // checks if the same characters are in the same spot
            {
                hint[i] = String.valueOf(secret[i]); // makes the charcter show in the hint if it's in the right place
            } else {
                for (char b : secret)  // parse through every letter of the secret wrod
                {
                    if (b == user[i]) // compares each letter of the secret word with a single character of the users input
                    {
                        hint[i] = "+"; // puts a plus if the character is in the word but in the wronf place 
                    }
                }
            }
        }
        for (String p : hint)
        {
            System.out.print(p);
        }
        System.out.println();
    }

    public static void main(String[] args)
    {
        String[] words = {
            "APPLE", "MANGO", "SMILE", "BRAVE", "HAPPY", "GRAPES", "DANCE", "SUNNY", "CLOUD", "QUIET",
            "OCEAN", "FAITH", "GREEN", "ROUND", "SHARP", "MAPLE", "JELLY", "QUEEN", "BROWN", "BEACH",
            "FLAME", "SWIFT", "APRIL", "WATER", "SMART", "FRESH", "SWEET", "TIGER", "BREEZE", "CHIRP",
            "AMBER", "FLOCK", "BLOOM", "BLAZE", "FROST", "PEARL", "SPARK", "FLUTE", "QUEST", "FALSE",
            "IVORY", "SONIC", "QUOTA", "YACHT", "BLISS", "CORAL", "COMET", "TRUE", "OCTANE", "TRAPS"
        };
        Random random = new Random();
        Scanner scanner = new Scanner(System.in);

        String secretword = words[random.nextInt(50)];
        System.out.println("Please type in all caps");
        for (int i = 0; i < secretword.length(); i++)
        {
            System.out.print("-");
        }
        System.out.println();

        for ( ; ; )  // infinite loop
        //while (1 == 1)
        {
            String uinp = scanner.nextLine();
            System.out.println(uinp);
            if (uinp.length() == secretword.length())
            {
                getHint(uinp, secretword);
                if (uinp.equalsIgnoreCase(secretword)) //maybe add another hint
                {
                    break;
                }
            } else {
                System.out.println("That's the wrong amount of letters");
            }
            if (uinp.equalsIgnoreCase("apex"))
            {
                break;
            }
            System.out.println();
        }
    }
}
HiddenWord.main(null)
|           while (;)

illegal start of expression



|           while (;)

illegal start of expression

Question 3

This FRQ is about Arrays/ArrayLists

public class SparseArrayEntry
{
    private int row;
    private int col;
    private int value;

    public SparseArrayEntry(int r, int c, int v)
    {
        row = r;
        col = c;
        value = v;
    }
    public int getRow()  // a bunch of setters and getters because rox, col and value are private
    { return row; }
    public int getCol()  // private variables suck
    { return col; }
    public int getValue()
    { return value; }
    public void setValue(int valu)
    { this.value = valu; }
    public void setCol(int co)
    { this.col = co; }

    public static void main(String[] args)
    {
        SparseArrayEntry num1 = new SparseArrayEntry(1, 4, 4);  // defining some random 
        SparseArrayEntry num2 = new SparseArrayEntry(2, 0, 1);
        SparseArrayEntry num3 = new SparseArrayEntry(3, 1, -9);
        SparseArrayEntry num4 = new SparseArrayEntry(1, 1, 5); 
    }
    
}
SparseArrayEntry.main(null)
public class SparseArray
{
    private int numRows;
    private int numCols;
    private List<SparseArrayEntry> entries;

    public SparseArray()
    { entries = new ArrayList<SparseArrayEntry>(); }

    public int getNumRows()
    { return numRows; }
    public int getNumCols()
    { return numCols; }

    // Precondition: 0 <= row < getNumRows()
    //               0 <= col < getNumCols()
    public int getValueAt(int row, int col) 
    {
        for (SparseArrayEntry i : this.entries)
        {
            if (i.getRow() == row && i.getCol() == col)  // make sure both the column and row index match
            {
                return i.getValue();  // return the value if a match is found
            }
        }
        return 0; // return 0 if there's no match
    }

    // removes the column *col* from the sparse array
    // Precondition: 0 <= col < getNumCols()
    public void removeColumn(int col)
    {
        this.numCols = this.getNumCols() - 1;   // removes 1 from the total columns count
        for (SparseArrayEntry i : this.entries) 
        {
            if (i.getCol() == col)  // search for all entries with the column that's being removed
            {
                i.setValue(0);  // sets their value to 0
            } else if (i.getCol() > col) {  // checks if the entry's column is above the removed column
                i.setCol(i.getCol() - 1);  // lowers that entry's column by one
            }
        }
    }

    public static void main(String[] args)
    {
        SparseArrayEntry num1 = new SparseArrayEntry(1, 4, 4);
        SparseArrayEntry num2 = new SparseArrayEntry(2, 0, 1);
        SparseArrayEntry num3 = new SparseArrayEntry(3, 1, -9);
        SparseArrayEntry num4 = new SparseArrayEntry(1, 1, 5);

        SparseArray test = new SparseArray();

        test.numCols = 5;
        test.numRows = 6;
        
        test.entries.add(num1);
        test.entries.add(num2);
        test.entries.add(num3);
        test.entries.add(num4);

        System.out.println("Getting values:");
        System.out.println(test.getValueAt(1, 2));
        System.out.println(test.getValueAt(1, 4));
        System.out.println(test.getValueAt(3, 1));
        System.out.println();

        test.removeColumn(1);
        System.out.println("Getting values after removing column 1:");
        System.out.println(test.getValueAt(2, 0));
        System.out.println(test.getValueAt(1, 3));
        System.out.println(test.getValueAt(3, 1));
    }
}
SparseArray.main(null)
Getting values:
0
4
-9

Getting values after removing column 1:
1
4
0

Question 4

This FRQ is about methods and control structures

public interface NumberGroup 
{
    boolean contains(int number);
}
public class Range implements NumberGroup 
{
    private int min;
    private int max;

    public Range(int min, int max) 
    {
        this.min = min;
        this.max = max;
    }

    public boolean contains(int number) 
    {
        return number >= min && number <= max;
    }
}
public class MultipleGroups implements NumberGroup 
{
    private List<NumberGroup> groupList;

    public MultipleGroups(List<NumberGroup> groupList) 
    {
        this.groupList = groupList;
    }

    public boolean contains(int number) 
    {
        for (NumberGroup group : groupList) 
        {
            if (group.contains(number)) 
            {
                return true;
            }
        }
        return false;
    }
}
public class Main 
{
    public static void main(String[] args) 
    {
        NumberGroup range1 = new Range(-3, 2); // use range through numbergroup because its an interface
        NumberGroup range2 = new Range(5, 8);
        NumberGroup range3 = new Range(10, 12);
        NumberGroup range4 = new Range(1, 6);

        List<NumberGroup> groupList = Arrays.asList(range1, range2, range3, range4); // creating the ranges into a list
        MultipleGroups multiple1 = new MultipleGroups(groupList); // putting that list into a multiplegroups object

        System.out.println("test for single group contain:");
        System.out.println(range1.contains(4));
        System.out.println(range1.contains(-1));
        System.out.println();
        System.out.println("test for multiple group conatain:");
        System.out.println(multiple1.contains(9));
        System.out.println(multiple1.contains(11));
    }
}
Main.main(null)
test for single group contain:
false
true

test for multiple group conatain:
false
true