2018 CSA FRQ Question 4

public class ArrayTester {
    
    // returns an array that represents the column at index c in a 2D array
    public static int[] getColumn(int[][] arr2D, int c) {
        int[] newArray = new int[arr2D.length];
        for (int r = 0; r < arr2D.length; r++) {
            newArray[r] = arr2D[r][c];
        }
        return newArray;
    }

    
    public static boolean hasAllValues(int[] arr1, int[] arr2) {
        // getter for hasAllValues
        return true;
    }

    
    public static boolean containsDuplicates(int[] arr) {
        // getter for determining duplicates
        return false;
    }

    // returns true if the 2D array square is a Latin square
    public static boolean isLatin(int[][] square) {

        // check rows of square for duplicates
        for (int i = 0; i < square.length; i++) {
            if (containsDuplicates(square[i])) {
                return false;
            }
            // compare rows against row 0, return false if not the same
            if (!hasAllValues(square[0], square[i])) {
                return false;
            }
        }

        // check columns for duplicates
        for (int i = 0; i < square[0].length; i++) {
            // get column at index
            int[] col = getColumn(square, i);

            // check for duplicates in the columns of the two arrays
            if (containsDuplicates(col)) {
                return false;
            }
            // compare columns against column 0, return false if not the same
            if (!hasAllValues(getColumn(square, 0), col)) {
                return false;
            }
        }

        // if passes all checks return true
        return true;

    }

    public static void main(String[] args) {
        // create first 2D array
        int[][] arr2D = {
            {0,1,2},
            {3,4,5},
            {6,7,8},
            {9,5,3}
        };

        // print out the first array
        int[] newArray = ArrayTester.getColumn(arr2D, 1);
        for (int i = 0; i < newArray.length; i++) {
            System.out.print(newArray[i] + " ");
        }

        // create the second array
        int[][] latinSq = {
            {1,2,3},
            {2,3,1},
            {3,1,2}
        };

        System.out.println(""); 
        System.out.println("Is latin: " + isLatin(latinSq)); // tests whether the arrays are the same if they are flipped

    }
   
}

ArrayTester.main(null);
1 4 7 5 
Is latin: true

2017 CSA FRQ Question 4

public class Position {
    int r;
    int c;

    // Position constructor
    public Position(int row, int col) {
        this.r = row;
        this.c = col;
    }

    // method to return string representation of Position object
    public String toString() {
        String s = "(" + this.r + "," + this.c + ")";
        return s;
    }

    // method to find the Position of a given value in a 2D array
    public static Position findPosition(int val, int[][] arr) {
        // nested for loop to iterate through rows and columns of the array
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                // if the value is found in the array, create and return a new Position object
                if (val == arr[i][j]) {
                    Position p = new Position(i,j);
                    return p;
                }
            }
        }

        // if the value is not found in the array, return null
        return null;
    }

    // method to create a 2D array of Position objects that represent the positions of each element's successor in the given array
    public static Position[][] getSuccessorArray(int[][] intArr) {
        Position[][] newArr = new Position[intArr.length][intArr[0].length];
        // nested for loop to iterate through rows and columns of the array
        for (int row=0; row < intArr.length; row++) {
            for (int col=0; col < intArr[0].length; col++) {
                // get the successor of the current element in the array, and find its Position
                newArr[row][col] = findPosition(intArr[row][col]+1, intArr);
            }
        }
        return newArr;
    } 

    // main method for testing
    public static void main(String[] args) {
        int[][] arr = {
            {15,5,9,10},
            {12,16,11,6},
            {14,8,13,7}
        };

        // find the Position of the value 9 in the array, and print it
        System.out.println(findPosition(9, arr));

        // create a 2D array of Position objects that represent the positions of each element's successor in the given array, and print it
        Position[][] successor = getSuccessorArray(arr);
        for (int i = 0; i < successor.length; i++) {
            System.out.println("");
            for (int j = 0; j < successor[i].length; j++) { 
                System.out.print(successor[i][j] + " ");
            }
        }
    }
}

// call the main method to run the program
Position.main(null);
(0,2)

(1,1) (1,3) (0,3) (1,2) 
(2,2) null (1,0) (2,3) 
(0,0) (0,2) (2,0) (2,1) 

2016 CSA FRQ Question 3

public class Crossword {

    // A private inner Square class that represents a square in the puzzle
    private Square[][] puzzle;

    // A public inner Square class that represents a square in the puzzle
    public class Square {
        private boolean isBlackCell;
        private int number;

        // Constructor for a Square object
        public Square(boolean isBlack, int num){
            isBlackCell = isBlack;
            number = num;
        }

        // Get the number associated with the square
        public int getNumber() {
            return number;
        }

        // Return a string representation of the square
        public String toString() {
            String result = "";
            if (isBlackCell) {
                result = "B";
            }
            else {
                result = String.valueOf(number);
            }
            return result;
        }
    }

    // Constructor for a Crossword object that initializes the puzzle
    public Crossword(boolean[][] blackSquares) {
        puzzle = new Square[blackSquares.length][blackSquares[0].length];
        int num = 1;
        // nested for loop that iterates through the 2D array
        for (int i = 0; i < blackSquares.length; i++) {
            for (int j = 0; j < blackSquares[0].length; j++) {
                if (blackSquares[i][j]) { // test if the array index is black
                    puzzle[i][j] = new Square(true, 0);
                }
                else {
                    if (toBeLabeled(i, j, blackSquares)) {
                        puzzle[i][j] = new Square(false, num);
                        num++;
                    }
                    else {
                        puzzle[i][j] = new Square(false, 0);
                    }
                }
            }
        }
    }

    // Determines whether a square should be labeled with a number or not
    public boolean toBeLabeled(int r, int c, boolean[][] blackSquares) {
        if (blackSquares[r][c]) { // tests if the square is black and makes it false
            return false;
        }

        if (r == 0 || c == 0) { // tests if its on the very left or very top of the crosswords and returns true
            return true;
        }

        if (blackSquares[r-1][c] && blackSquares[r][c-1]) { // has to be black on top and black on left for it to be true
            return true;
        }
        else {
            return false;
        }
    }

    // Returns a string representation of the crossword puzzle
    public String toString() {
        String result = "";
        for (int i = 0; i < puzzle.length; i++) {
            for (int j = 0; j < puzzle[0].length; j++) {
                result += puzzle[i][j] + " ";
            }
            result += "\n";
        }
        return result;
    }

    // Main method to test the Crossword class
    public static void main(String[] args)
    {
        // creates new crossword with black spots
        boolean[][] cells = {
        {true, false, false, true},
        {false, false, false, false},
        {false, false, false, false},};
        
        Crossword game = new Crossword(cells);
        
        System.out.println("Part (a):");
        boolean result = game.toBeLabeled(0, 3, cells);
        System.out.println("Positive number at row=0, col=8? Should be true: " + result);
        System.out.println();
        System.out.println("Part (b):");
        System.out.println("Puzzle initialized from the Crossword constructor:");
        System.out.println(game);
    }
}

// Run the main method to test the Crossword class
Crossword.main(null);
Part (a):
Positive number at row=0, col=8? Should be true: false

Part (b):
Puzzle initialized from the Crossword constructor:
B 1 2 B 
3 0 0 0 
4 0 0 0 

2015 CSA FRQ Question 2

public class HiddenWord {

        public static String word;

        // constructor for HiddenWord
        public HiddenWord(String word) {
            this.word = word;
        }

        public String getHint(String guess) {
            String hint = "";
            for (int i = 0; i < guess.length(); i++) {
                if (guess.substring(i, i + 1).equals(word.substring(i, i + 1))) { 
                    // tests if the element of the guess matches the element of the word
                    hint += guess.substring(i, i + 1);
                }
                else if (word.indexOf(guess.substring(i,i + 1)) != -1) {
                    // tests if the element is found anywhere in the array
                    hint += "+";
                }
                else {
                    hint += "*";
                }
            }  
            return hint;
            
        }
        
        public static void main(String[] args) {
            HiddenWord puzzle = new HiddenWord("HARPS");

            // guess with no correct letters
            String guess1 = "KFLMC";
            String hint1 = puzzle.getHint(guess1);
            System.out.println(hint1);

            // guess with one right letter in right spot and one right letter in wrong spot
            String guess2 = "HIFSK";
            String hint2 = puzzle.getHint(guess2);
            System.out.println(hint2);

            // guess with right word
            String guess3 = "HARPS";
            String hint3 = puzzle.getHint(guess3);
            System.out.println(hint3);
            
        }

        
    }


HiddenWord.main(null);
*****
H**+*
HARPS