2017 FRQ

import java.util.*;
import java.lang.*;

public class Digits {

    private ArrayList<Integer> digitList;

    public Digits(int num){

        digitList = new ArrayList<Integer>();

        int remainderNum = 0;

        if(num != 0){
            while(num != 0){

                remainderNum = num % 10;
                System.out.println("Remainder: "+ remainderNum);
                digitList.add(0,remainderNum);
                num = num / 10;
                System.out.println("Number: " + num);
            
            }
        }else{
            digitList.add(num);
        }

    }

    public String toString(){
        
        String digitListPrint = "";

        for(int i = 0; i < digitList.size(); i++){
            digitListPrint+=digitList.get(i);
        } 
        return digitListPrint;
    }

    public static void main(String[] args){
        Digits d1 = new Digits(15704);
        System.out.println(d1);

        Digits d2 = new Digits(0);
        System.out.println(d2);

    }

}
Digits.main(null);
Remainder: 4
Number: 1570
Remainder: 0
Number: 157
Remainder: 7
Number: 15
Remainder: 5
Number: 1
Remainder: 1
Number: 0
15704
0

a)

+1 Constructs digitList

+1 Identifies a digit in num

+1 Adds at least one identified digit to a list

+1 Adds all identified digits to a list (must be in context of a loop)

+1 On exit: digitList contains all and only digits of num in the correct order

b)

+1 Compares at least one identified consecutive pair of digitList elements

+1 Determines if a consecutive pair of digitList is out of order (must be in context of a digitList traversal)

+1 Compares all necessary consecutive pairs of elements (no bounds errors)

+1 Returns true iff all consecutive pairs of elements are in order; returns false otherwise

a) 5/5 all reqiuirements are met

b) 4/4 all reqiuirements are met

public interface StudyPractice{ // defintion of the method is given, not the implementation
    String getProblem();

    void nextProblem();
}
public class MultPractice implements StudyPractice {

    private int firstInt = 0;
    private int secondInt = 0;

    public MultPractice(int firstInt, int secondInt){
        this.firstInt = firstInt;
        this.secondInt = secondInt;
    }
    
    public String getProblem(){
        return firstInt + " TIMES " + secondInt;
    }

    public void nextProblem(){
        secondInt+=1;
    }

    public static void main(String[] args){
        StudyPractice p1 = new MultPractice(7, 3);
        System.out.println(p1.getProblem());

        p1.nextProblem();
        System.out.println(p1.getProblem());

        p1.nextProblem();
        System.out.println(p1.getProblem()); 

        p1.nextProblem();
        System.out.println(p1.getProblem());


        StudyPractice p2 = new MultPractice(4, 12);
        p2.nextProblem();
        
        System.out.println(p2.getProblem());
        System.out.println(p2.getProblem());
        p2.nextProblem();
        p2.nextProblem();
        System.out.println(p2.getProblem());
        p2.nextProblem();
        System.out.println(p2.getProblem());
    }
        

}
MultPractice.main(null);
7 TIMES 3
7 TIMES 4
7 TIMES 5
7 TIMES 6
4 TIMES 13
4 TIMES 13
4 TIMES 15
4 TIMES 16

a) +1 Declares header: public class MultPractice implements StudyPractice

+1 Declares all necessary private instance variables

+1 Initializes all instance variables using parameters

+1 Declares header: public String getProblem()

+1 Builds string with current values of instance variables

+1 Returns constructed string

+1 Declares header: public void nextProblem()

+1 Updates instance variable(s) to reflect incremented second number

8/9 requirements are met

public class Phrase{

    private String currentPhrase = "";

    public Phrase(String p){
        currentPhrase = p;
    }

    public int findNthOccurrence(String str, int n){
        return 3;
    }

    public void replaceNthOccurrence(String str, int n, String repl){
        int indexOccurence = findNthOccurrence(str, n);

        if(indexOccurence != -1){
            String str1 = currentPhrase.substring(0, indexOccurence);
            String str2 = currentPhrase.substring(indexOccurence + str.length());
            currentPhrase = str1 + repl + str2;
        }
    }

    public int findLastOccurrence(String str){
        
        String newPhrase = new String(currentPhrase);
        int n = currentPhrase.indexOf(str);
       
        int lastIndex = 0;

        while(n != -1){

            newPhrase = newPhrase.substring(n+1);
            System.out.println(newPhrase);
            n = newPhrase.indexOf(str);
            System.out.println(n);
        }

        if(newPhrase.length() != 0){
            lastIndex = currentPhrase.length() - newPhrase.length() -1;
            System.out.println("Last occurrence: " + lastIndex);
            return lastIndex;
        } else {
            System.out.println("Last occurrence: -1");
            return -1;
        }
    }


    public String toString(){
        return "Phrase: " + currentPhrase;
    }

    public static void main(String[] args){
        Phrase phrase1 = new Phrase("A cat ate late.");
        phrase1.replaceNthOccurrence("at", 1, "rane");
        // System.out.println(new String("A cat ate late.").length());
        Phrase phrase2 = new Phrase("A cat ate late.");
        phrase2.findLastOccurrence("at");
    }

}
Phrase.main(null);
t ate late.
2
te late.
4
te.
-1
Last occurrence: 11

a) +1 Calls findNthOccurrence to find the index of the nth occurrence

+1 Preserves currentPhrase only if nth occurrence does not exist

+1 Identifies components of currentPhrase to retain (uses substring to extract before/after)

+1 Creates replacement string using identified components and repl

+1 Assigns replacement string to instance variable (currentPhrase)

b)

+1 Calls findNthOccurrence to find the index of the nth occurrence

+1 Increments (or decrements) the value used as n when finding nth occurrence

+1 Returns the index of the last occurrence, if it exists

+1 Returns -1 only when no occurrences exist

a) 4/4 all reqiuirements are met

b) Does not utilize findNthOccurrence therefore 3/4

public class Position{

    // int[][] intArr = { { 15, 5, 9 ,10 }, { 12, 16, 11, 6 }, { 14, 8, 13, 7}};

    private int row = 0;
    private int col = 0;


    public Position(int r, int c){
        row = r;
        col = c;
    }

    public static Position findPosition(int num, int[][] intArr){
        for(int i = 0; i < intArr.length; i++){
            for(int g = 0; g < intArr[i].length; g++){
                if(intArr[i][g] == num){
                    System.out.println("row: " + i + " column: " + g);
                    return new Position(i, g);
                }

            }
        }
        System.out.println("null");
        return null;
    }

    public static Position[][] getSuccessorArray(int[][] intArr){
        Position[][] posArray = new Position[intArr.length][intArr[0].length];
        for(int i = 0; i < intArr.length; i++){
            for(int g = 0; g < intArr[i].length; g++){
                posArray[i][g] = findPosition(intArr[i][g] + 1, intArr);
            }
        } 
        return posArray;
    }


    public static void main(String[] args){
        int[][] intArr = { { 15, 5, 9 ,10 }, { 12, 16, 11, 6 }, { 14, 8, 13, 7}};
        Position.findPosition(8, intArr);
        Position.findPosition(15, intArr);
        Position.findPosition(17, intArr);
    }

}
Position.main(null);
row: 2 column: 1
row: 0 column: 0
null

a)

+1 Accesses all necessary elements of intArr (no bounds errors)

+1 Identifies intArr element equal to num (in context of an intArr traversal)

+1 Constructs Position object with same row and column as identified intArr element

+1 Selects constructed object when intArr element identified; null when not

+1 Returns selected value

b)

+1 Creates 2D array of Position objects with same dimensions as intArr

+1 Assigns a value to a location in 2D successor array using a valid call to findPosition

+1 Determines the successor Position of an intArr element accessed by row and column (in context of intArr traversal)

+1 Assigns all necessary locations in successor array with corresponding position object or null (no bounds errors)

a) 5/5 all reqiuirements are met

b) 4/4 all reqiuirements are met

2015 FRQ

public class ArrayManipulation{

    public static int arraySum(int[] arr){
        int arraySum = 0;
        for(int i = 0; i < arr.length; i++){
            // System.out.println("array sum: " + arraySum);
            // System.out.println("arr i "+ arr[i]);

            arraySum += arr[i];

        }
        System.out.println(arraySum);
        return arraySum;
    }

    public static int[] rowSums(int[][] arr2D){
        int rowSum;
        int[] rowSumS = new int[arr2D.length];

        for(int i = 0; i < arr2D.length; i++){
            rowSum = 0;
            for(int h = 0; h < arr2D[i].length; h++){
                rowSum += arr2D[i][h];
            }
            rowSumS[i] = rowSum;
            System.out.println(rowSumS[i]);
        }
        return rowSumS;
    }

    public static void main(String[] args){
        int[] arr = {1, 3, 2, 7 ,3};
        ArrayManipulation.arraySum(arr);

        int[][] arr2D = {{1, 3, 2, 7 ,3}, {10, 10, 4, 6, 2}, {5, 3, 5, 9 ,6 }, {7,6,4,2,1}};
        ArrayManipulation.rowSums(arr2D);
    } 

}
ArrayManipulation.main(null);
16
16
32
28
20

a)

+1 Accesses all elements of arr, (no bounds errors on arr)

+1 Initializes, computes, and returns sum of elements

b)

+1 Constructs correctly-sized 1D array of ints

+1 Accesses all rows in arr2D (no bounds errors on arr2D)

+1 Computes sum of row in arr2D using arraySum and assigns to element in 1D array

+1 Returns 1D array where kth element is computed sum of corresponding row in 2D array for all rows

a) 2/2 all reqiuirements are met

b) 4/4 all reqiuirements are met

c) incomplete