// 2020 Practice FRQ #1
public class Hailstone {
    public static int hailstoneLength(int n) {
    int length = 1;  // start with n
    while (n != 1) {
        if (n % 2 == 0) {
            n = n / 2;
        } else {
            n = 3 * n + 1;
        }
        length++;
    }
    return length;
}

public static boolean isLongSeq(int n) {
    int length = hailstoneLength(n);
    return length > n;
}

public static double propLong(int n) {
    int count = 0;
    for (int i = 1; i <= n; i++) {
        if (isLongSeq(i)) {
            count++;
        }
    }
    return (double) count / n;
}

public static void main(String[] args) {
    // Test hailstoneLength method
    System.out.println("hailstoneLength(5): " + Hailstone.hailstoneLength(5)); // expected output: 6
    System.out.println("hailstoneLength(8): " + Hailstone.hailstoneLength(8)); // expected output: 4
    
    // Test isLongSeq method
    System.out.println("isLongSeq(5): " + Hailstone.isLongSeq(5)); // expected output: true
    System.out.println("isLongSeq(8): " + Hailstone.isLongSeq(8)); // expected output: false
    
    // Test propLong method
    System.out.println("propLong(10): " + Hailstone.propLong(10)); // expected output: 0.4
    System.out.println("propLong(20): " + Hailstone.propLong(20)); // expected output: 0.45
}
}


Hailstone.main(null);
hailstoneLength(5): 6
hailstoneLength(8): 4
isLongSeq(5): true
isLongSeq(8): false
propLong(10): 0.5
propLong(20): 0.5
// FRQ 2 Classes

public class Bus {
    private int currentStop;
    private int lastStop;
    private int direction;

    public Bus(int numStops) {
        currentStop = 1;
        lastStop = numStops;
        direction = 1;
    }

    public int getCurrentStop() {
        return currentStop;
    }

    public void move() {
        if (currentStop == 1) {
            direction = 1;
        } else if (currentStop == lastStop) {
            direction = -1;
        }
        currentStop += direction;
    }

    // Tester method
    public static void main(String[] args) {
        Bus bus1 = new Bus(3);
        System.out.println("Current Stop " + bus1.getCurrentStop()); // 1
        bus1.move();
        System.out.println("Current Stop " + bus1.getCurrentStop()); // 2
        bus1.move();
        System.out.println("Current Stop " + bus1.getCurrentStop()); // 3
        bus1.move();
        System.out.println("Current Stop " + bus1.getCurrentStop()); // 2
        bus1.move();
        System.out.println("Current Stop " + bus1.getCurrentStop()); // 1
        bus1.move();
        System.out.println("Current Stop " + bus1.getCurrentStop()); // 2
        System.out.println("Current Stop " + bus1.getCurrentStop()); // 2 (still at stop 2)
        
        Bus bus2 = new Bus(5);
        System.out.println("Current Stop " + bus2.getCurrentStop()); // 1
    }
}
Bus.main(null);
Current Stop 1
Current Stop 2
Current Stop 3
Current Stop 2
Current Stop 1
Current Stop 2
Current Stop 2
Current Stop 1
// 2020 FRQ #2
import java.util.Random;

public class GameSpinner {
    private int sectors; // number of sectors in the spinner
    private int prevSpin; // value of previous spin
    private int currentRunLength; // length of current run
    private Random rand; // random number generator

    /**
     * Constructs a new GameSpinner object with the specified number of sectors.
     * @param sectors the number of sectors in the spinner
     */
    public GameSpinner(int sectors) {
        this.sectors = sectors;
        this.prevSpin = 0;
        this.currentRunLength = 0;
        this.rand = new Random();
    }

    /**
     * Spins the spinner and returns the result.
     * @return a random integer between 1 and the number of sectors, inclusive
     */
    public int spin() {
        int result = rand.nextInt(sectors) + 1;
        if (result == prevSpin) {
            currentRunLength++;
        } else {
            currentRunLength = 1;
            prevSpin = result;
        }
        return result;
    }

    /**
     * Returns the length of the current run, the number of consecutive spins that are the same as the most recent spin.
     * @return the length of the current run
     */
    public int currentRun() {
        return currentRunLength;
    }

    // Tester main method
    public static void main(String[] args) {
        GameSpinner g = new GameSpinner(4);
        System.out.println(g.currentRun()); // 0
        System.out.println(g.spin()); // 3
        System.out.println(g.currentRun()); // 1
        System.out.println(g.spin()); // 3
        System.out.println(g.currentRun()); // 2
        System.out.println(g.spin()); // 4
        System.out.println(g.currentRun()); // 1
        System.out.println(g.spin()); // 3
        System.out.println(g.currentRun()); // 1
        System.out.println(g.spin()); // 1
        System.out.println(g.spin()); // 1
        System.out.println(g.spin()); // 1
        System.out.println(g.currentRun()); // 3
    }
}
GameSpinner.main(null);
0
2
1
1
1
2
1
3
1
1
4
2
1
// FRQ 5 Arrays, ArrayLists
import java.util.ArrayList;

public class Vocab {
    /** The controlled vocabulary for a Vocab object. */
    private String[] theVocab = { /* contents not shown */ };
    
    /** 
     * Searches for a string in theVocab. 
     * Returns true if its String parameter str is an exact match to an element in theVocab 
     * and returns false otherwise.
     */
    public boolean findWord(String str) {
        for (String s : theVocab) {
            if (s.equals(str)) {
                return true;
            }
        }
        return false;
    }
    
    /** 
     * Counts how many strings in wordArray are not found in theVocab.
     */
    public int countNotInVocab(String[] wordArray) {
        int count = 0;
        for (String word : wordArray) {
            if (!findWord(word)) {
                count++;
            }
        }
        return count;
    }
    
    /** 
     * Returns an array containing strings from wordArray not found in theVocab.
     */
    public String[] notInVocab(String[] wordArray) {
        ArrayList<String> notFound = new ArrayList<>();
        for (String word : wordArray) {
            if (!findWord(word)) {
                notFound.add(word);
            }
        }
        String[] result = new String[notFound.size()];
        for (int i = 0; i < notFound.size(); i++) {
            result[i] = notFound.get(i);
        }
        return result;
    }
    
    /** 
     * Tester method for the Vocab class.
     */
    public static void main(String[] args) {
        Vocab vocab = new Vocab();
        
        // Test countNotInVocab
        String[] testArray1 = {"dogs", "toys", "sun", "plants", "time"};
        System.out.println(vocab.countNotInVocab(testArray1)); // expected output: 2
        
        // Test notInVocab
        String[] testArray2 = {"dogs", "toys", "sun", "plants", "time"};
        String[] result = vocab.notInVocab(testArray2);
        for (String s : result) {
            System.out.println(s);
        }
        // expected output: "toys" "sun"
    }
}
Vocab.main(null);
5
dogs
toys
sun
plants
time