• **Question 2**
  • **Question 3**
  • **Question 4**
  • 2020 Practice Exam FRQ

    **Question 1**

    I’m working with a Java class called Hailstone that deals with hailstone sequences. A hailstone sequence follows these rules:

    • If I have 1, I stop.
    • If I have an even number, I divide by 2.
    • If I have an odd number, I multiply by 3 and add 1.
    • I keep doing this until I reach 1.

    Part (a)

    • This method helps me calculate how many numbers appear in the hailstone sequence when I start with a given number n.
    public static int hailstoneLength(int n) {
        int length = 1; // Starts with the first term (n itself)
        while (n != 1) {
            if (n % 2 == 0) {
                n = n / 2;
            } else {
                n = 3 * n + 1;
            }
            length++;
        }
        return length;
    }
    

    Part (b)

    • I use this method to determine if a sequence is “long.” I consider a sequence “long” if it contains more numbers than the starting value.
    public static boolean isLongSeq(int n) {
        return hailstoneLength(n) > n;
    }
    

    Part (c)

    • With this method, I can find what fraction of hailstone sequences from 1 to n are “long.” This gives me a value between 0.0 and 1.0.
    public static double propLong(int n) {
        int count = 0;
        for (int i = 1; i <= n; i++) {
            if (isLongSeq(i)) {
                count++;
            }
        }
        return (double) count / n;
    }
    



    **Question 2**

    I’m working with a GameSpinner class that I can use to simulate a spinner divided into equal sectors (like on a game board). My spinner lets me:

    • Create a spinner with any number of sectors I choose
    • Spin it to get a random number between 1 and the total number of sectors
    • Keep track of how many times in a row I’ve landed on the same number

    import java.util.Random;
    
    public class GameSpinner {
        private int sectors;
        private int currentValue;
        private int runLength;
        private Random rand;
    
        public GameSpinner(int numSectors) {
            sectors = numSectors;
            currentValue = 0; // no spin yet
            runLength = 0;
            rand = new Random();
        }
    
        public int spin() {
            int result = rand.nextInt(sectors) + 1;
    
            if (result == currentValue) {
                runLength++;
            } else {
                currentValue = result;
                runLength = 1;
            }
    
            return currentValue;
        }
    
        public int currentRun() {
            return runLength;
        }
    }
    



    **Question 3**

    • I’m examining code that manages product reviews. The system stores and analyzes customer feedback about products in two main classes.
    • The first class creates individual review objects with product names and review text. The second class maintains collections of these reviews and provides functionality to:
      • Store new reviews in a list
      • Keep track of unique product names
      • Count how many reviews for a specific product contain the word “best” (in lowercase only)

    import java.util.ArrayList;
    
    public class ProductReview {
        private String name;
        private String review;
    
        // I use this constructor to create a new review with the product name and text
        public ProductReview(String pName, String pReview) {
            name = pName;
            review = pReview;
        }
    
        // I can retrieve the product name with this method
        public String getName() {
            return name;
        }
    
        // I can retrieve the review text with this method
        public String getReview() {
            return review;
        }
    }
    
    public class ReviewCollector {
        private ArrayList<ProductReview> reviewList;
        private ArrayList<String> productList;
    
        // When I create a new ReviewCollector, I initialize both lists
        public ReviewCollector() {
            reviewList = new ArrayList<ProductReview>();
            productList = new ArrayList<String>();
        }
    
        /**
         * I use this method to add a new review to my collection.
         * - I add the review to my reviewList.
         * - I also add the product name to my productList if I haven't seen it before.
         */
        public void addReview(ProductReview prodReview) {
            reviewList.add(prodReview);
    
            if (!productList.contains(prodReview.getName())) {
                productList.add(prodReview.getName());
            }
        }
    
        /**
         * I use this method to count good reviews for a specific product.
         * I consider a review "good" if it contains the word "best" in lowercase.
         */
        public int getNumGoodReviews(String prodName) {
            int count = 0;
    
            for (ProductReview pr : reviewList) {
                if (pr.getName().equals(prodName) && pr.getReview().contains("best")) {
                    count++;
                }
            }
    
            return count;
        }
    }
    



    **Question 4**


    Part (a)

    • I need to write the constructor that creates the theater with rows of seats. Tier 1 seats are better because they’re closer to the stage.
    public Theater(int seatsPerRow, int tier1Rows, int tier2Rows) {
        theaterSeats = new Seat[tier1Rows + tier2Rows][seatsPerRow];
        
        // Initialize tier 1 seats (rows closest to stage)
        for (int row = 0; row < tier1Rows; row++) {
            for (int col = 0; col < seatsPerRow; col++) {
                theaterSeats[row][col] = new Seat(true, 1);
            }
        }
        
        // Initialize tier 2 seats
        for (int row = tier1Rows; row < tier1Rows + tier2Rows; row++) {
            for (int col = 0; col < seatsPerRow; col++) {
                theaterSeats[row][col] = new Seat(true, 2);
            }
        }
    }
    

    Part (b)

    • In this part, I’m writing code to move someone from one seat to another. They can only move to an empty seat that’s the same quality or worse than their current seat.
    public boolean reassignSeat(int fromRow, int fromCol, int toRow, int toCol) {
        // Get source and destination seats
        Seat fromSeat = theaterSeats[fromRow][fromCol];
        Seat toSeat = theaterSeats[toRow][toCol];
        
        // Check if destination seat is available
        if (toSeat.isAvailable()) {
            // Check if destination seat tier is same or greater than source seat tier
            // (Remember: lower tier number means better seats - tier 1 is better than tier 2)
            if (toSeat.getTier() >= fromSeat.getTier()) {
                // Make the reassignment
                fromSeat.setAvailability(true);    // Source seat becomes available
                toSeat.setAvailability(false);     // Destination seat becomes unavailable
                return true;
            }
        }
        
        // If reassignment isn't possible, return false
        return false;
    }