FRQs - Methods and Control Structures

Question 3

part a)

/** Precondition: n is between 1 and 12, inclusive.
* Returns the factorial of n, as described in part (a).
*/

public static int factorial(int n) { // incorrect way of doing it
    n *= (n - 1);
    n--;
    if ( n > 1) {
        factorial(n);
    } else {
        return n;
    }
}

part b)

/** Precondition: n and r are between 1 and 12, inclusive.
* Determines the number of ways r items can be selected
* from n choices and prints the result, as described in part (b).
*/

public static void numCombinations(int n, int r) { // correct
    int w = 0;
    if ( r > n) {
        return ("There are " + w + " ways of choosing " + n + " items from " + r + " choices.");
    } else {
        w = (factorial(n)) / ((factorial(r)) * (n - r));
        return ("There are " + w + " ways of choosing " + n + " items from " + r + " choices.");
    }
}

running result

public class Tester {
    
    public static int factorial(int n) { // my factorial code was wrong and would not run
        if (n >= 1 && n <=12) {
            if (n==1) {
                return 1;
            } else {
                return n * factorial(n-1);
            }
        } else {
        return -1;
        }
    }

    public static String numCombinations(int n, int r) { // this method runs correctly
        int w = 0;
        if ( r > n) {
            return ("There are " + w + " ways of choosing " + n + " items from " + r + " choices.");
        } else {
            w = (factorial(n)) / ((factorial(r)) * (n - r));
            return ("There are " + w + " ways of choosing " + n + " items from " + r + " choices.");
        }
    }
    
    public static void main(String args[]) {
        System.out.println("Factorial of 7 : " + factorial(7));
        System.out.println("Combination of 8 and 4 : " + numCombinations(8, 4));
        
    }
}
Tester.main(null);
Factorial of 7 : 5040
Combination of 8 and 4 : There are 420 ways of choosing 8 items from 4 choices.

FRQs - Class

Question 3

public class MultPractice implements StudyPractice { //+1
    
    private int n1;
    private int n2; //+1

    public MultPractice (int n1, int n2) { //+2
        this.n1 = n1;
        this.n2 = n2;
    }

    public String getProblem() { //+3
        return (n1 + " TIMES " + n2);
    }

    public void nextProblem() { //+2
        n2++;
    }

    public static void main(String args[]) {
        StudyPractice p2 = new MultPractice(3, 4);
        System.out.println(p2.getProblem());
    }
    
}
// 9/9!

running result

public class MultPractice {
    
    private int n1;
    private int n2;

    public MultPractice (int n1, int n2) {
        this.n1 = n1;
        this.n2 = n2;
    }

    public String getProblem() {
        return (n1 + " TIMES " + n2);
    }

    public void nextProblem() {
        n2++;
    }

    public static void main(String args[]) {
        MultPractice p2 = new MultPractice(3, 4);
        System.out.println(p2.getProblem());
        p2.nextProblem();
        System.out.println(p2.getProblem());
    }
    
}
MultPractice.main(null);
3 TIMES 4
3 TIMES 5

FRQs - Arrays, ArrayLists

Question 3

part a)

public static int[] getCubeTosses(NumberCube cube, int numTosses) {
    int[] values = new int[numTosses]; //+1
    for (int i = 0; i < values.length; i++) {
        values[i] = cube.toss(); //+2.5
    }
    return values; //+0.5
}
// 4/4!

part b)

public static int getLongestRun(int[] values) {
    int counter = 0;
    int finalCounter = 0;
    for (int i = 0; i < values.length; i++) { //+0.5, accesses but out of bound potential
        if (values[i] == values[i + 1]) { //+1
            counter++;
        } else {
            if (counter > finalCounter) {
                finalCounter = counter; //+1
            }
            counter = 0;
        }
        
    } 
    if (finalCounter == 0) {
        return -1;
    } else {
        return finalCounter; //+2
    }
}
// 4.5/5!

running result

import java.util.*;

public class NumberCube {
    
    private int numTosses;
    
    public NumberCube()  { }

    public int toss() {
        return ((int) (Math.random() * 6) + 1);
    }

    public int getNumTosses() {
        return numTosses;
    }

    public static int[] getCubeTossesArr(NumberCube cube, int numTosses) {
        int[] values = new int[numTosses]; //+1
        for (int i = 0; i < values.length; i++) {
            values[i] = cube.toss(); //+2.5
        }
        return values; //+0.5
    }

    public static String getCubeTosses(NumberCube cube, int numTosses) {
        int[] values = new int[numTosses]; //+1
        for (int i = 0; i < values.length; i++) {
            values[i] = cube.toss(); //+2.5
        }

        System.out.print("The Generated Tosses Array: ");

        for (int i = 0; i < values.length; i++) {
            System.out.print(values[i] + " ");
        }

        System.out.println("\nThe Longest Run Number: " + getLongestRun(values));

        return (""); //+0.5
    }

    public static int getLongestRun(int[] values) {
        int counter = 1;
        int finalCounter = 0;
        for (int i = 0; i < values.length-1; i++) { 
            if (values[i] == values[i + 1]) { //+1
                counter++;
            } else {
                if (counter > finalCounter) {
                    finalCounter = counter; //+1
                }
                counter = 1;
            }
            
        } 
        if (finalCounter == 0) {
            return -1;
        } else {
            return finalCounter; //+2
        }
    }

    public static void main(String args[]) {
        NumberCube cube1 = new NumberCube();
        System.out.println(getCubeTosses(cube1, 17));
    }

}
NumberCube.main(null);
The Generated Tosses Array: 3 2 6 5 5 2 1 6 3 6 6 6 5 3 2 5 3 
The Longest Run Number: 3

FRQs 2D Array

Question 3

part a)

public static int diagonalOp(int[][] matA, int[][] matB) { // done correctly! 
    int sum = 0;
    for (int i = 0; i < matA.length; i++) {
        sum += (matA[i][i] * matB[i][i]);
    }
    return sum;
}

part b)

public static int[][] expandMatrix(int[][] matA) { // almost but incorrect
    int[][] matB = new int[matA.length * 2][matA[0].length * 2];
    for (int i = 0; i < matB.length; i += 2) { // out of bounds error
        for (int j = 0; j < matB[0].length; j += 2) {
            matB[i][j] = matA[i][j]; //incorrect implementation, needs to be * 2 for all
            matB[i + 1][j] = matA[i][j];
            matB[i][j + 1] = matA[i][j];
            matB[i + 1][j + 1] = matA[i][j];
        }
    }
    return matB;
}

running result

import java.util.*;

public class Tester {
    
    public static int diagonalOp(int[][] matA, int[][] matB) {
        int sum = 0;
        for (int i = 0; i < matA.length; i++) {
            sum += (matA[i][i] * matB[i][i]);
        }
        return sum;
    }

    public static String expandMatrix(int[][] matA) {
        int[][] matB = new int[matA.length * 2][matA[0].length * 2];
        for (int i = 0; i < matA.length; i++) {
            for (int j = 0; j < matA[0].length; j++) {
                matB[2*i][2*j] = matA[i][j];
                matB[2*i + 1][2*j] = matA[i][j];
                matB[2*i][2*j + 1] = matA[i][j];
                matB[2*i + 1][2*j + 1] = matA[i][j];
            }
        }

        for (int i = 0; i < matB.length; i++) {
            for (int j = 0; j < matB[0].length; j++) {
                System.out.print(matB[i][j] + " ");
            }
            System.out.println(" ");
        }

        return (" ");
    }
    
    public static void main(String args[]) {
        int[][] matA = { { 4, 5, 6 }, {1, 2, 3 }, { 4, 5, 6 } };
        int[][] matB = { { 4, 5, 6 }, { 1, 3, 2 }, { 4, 5, 6 } };
        System.out.println("Diagonal Sum: " + diagonalOp(matA, matB) + "\n");
        System.out.println("Expanded Form: ");
        System.out.println(expandMatrix(matA));
    }
}
Tester.main(null);
Diagonal Sum: 58

Expanded Form: 
4 4 5 5 6 6  
4 4 5 5 6 6  
1 1 2 2 3 3  
1 1 2 2 3 3  
4 4 5 5 6 6  
4 4 5 5 6 6