Week 29 FRQ Responses

Trio - Akhil N, Tristan C, Adi K

By Akhil Nandhakumar4/17/2023
Article Thumbnail

FRQ Question 1

Part A:

     public static int hailstoneLength(int n) {
        int t = 1;
        while(n>1){
            if(n%2==0){
                n = n/2;
            }
            else{
                n = n*3 + 1;
            }
            t++;
        }
        return(t);
     }
  • Defines a static method hailstoneLength that takes an integer n as input and returns the length of the hailstone sequence starting at n.
  • The method uses a while loop to iterate through the sequence, incrementing a counter variable t for each step.
  • If n is even, it is divided by 2, otherwise it is multiplied by 3 and added to 1.
  • The loop continues until n reaches 1.

Part B:

public static boolean isLongSeq(int n){ 
     return hailstoneLength(n) > n; 
}
  • Defines a static method isLongSeq that takes an integer n as input and returns true if the length of the hailstone sequence starting at n is greater than n.
  • The method calls the hailstoneLength method with n as input and compares the result to n.

Part C:

public static double propLong(int n){

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

}
  • Defines a static method propLong that takes an integer n as input and returns the proportion of integers from 1 to n for which the hailstone sequence length is greater than the starting integer.
  • The method initializes a counter variable count to 1 and iterates through the integers from 1 to n.
  • For each integer, the method calls the isLongSeq method and increments count if it returns true.
  • Finally, the method returns count/n as a double.

FRQ 2:


import java.lang.Math;

public class Gamespinner{

    public int sectors;
    public int lastSpin;
    public int run;

    public Gamespinner(int n){

        sectors = n;
        lastSptn = 0;
        run = 0;

    }

    public int currentRun(){
        return run;
    }

    public int spin(){
        int newSpin = Math.random()*sectors + 1;
        if (newSpin == lastSpin){
            run++;
        }
        else{
            run = 1;
            lastSpin = newSpin;
        }
        return newSpin;
    }

}
  • Defines a class Gamespinner that simulates a game spinner with a specified number of sectors.
  • The class has instance variables sectors (number of sectors), lastSpin (the result of the previous spin), and run (the length of the current run of consecutive identical spins).
  • The constructor takes the number of sectors as input and initializes the instance variables.
  • The class has methods currentRun, which returns the current run length, and spin, which simulates a spin of the spinner by generating a random integer between 1 and the number of sectors.
  • If the result matches the previous spin, the run variable is incremented. Otherwise, the run variable is reset to 1 and the lastSpin variable is updated with the new spin result.

FRQ 3:

Part A:

public void addReview(ProductReview prodReview){

    reviewList.add(prodReview);
    String temp = prodReview.getName();

    for(int i = 0; i<productList.size(); i++){
        if !(temp.equals(productList.get(i))){
            productList.add(temp);
        }
    }
    
}
  • Defines a method addReview that takes a ProductReview object as input and adds it to a list of reviews called reviewList.
  • The method extracts the product name from the review object and checks if it is already in a list of products called productList.
  • If the product name is not in productList, it is added to the end of the list.

Part B:

public int getNumGoodReviews(String prodName){

    int count = 0;

    for (int i = 0; i < reviewList.size(); i++){
        if prodName.equals(reviewList.get(i).getReview().indexOf("best")){
            count++;
        }
    }

    return count;
    
}
  • Defines a method getNumGoodReviews that takes a product name as input and returns the number of reviews in the reviewList where the review text contains the word "best".
  • The method initializes a counter variable count to 0 and iterates through the reviewList.
  • For each review, the method extracts the review text and checks if it contains the word "best" using the indexOf method.
  • If the text contains "best", the count variable is incremented.
  • Finally, the method returns the count variable.

FRQ 4:

Part A:


public Theater (int seats PerRow, int tierlRows, int tier 2 Rows){
    theaterSeats = new Seat [tierlRows+tier2Rows] [seats PerRow]; 
    for (int r = 0; r<theaterSeats.length; i++){
        for(int c = 0; c<seats PerRow; c++){
            if (r<tier1 Rows){
                theater Seats [r] [c]=new Seat (true,1);
            }
            else{
                theaterSeats [r] [c]=new Seat (true, 2);
            }
        }
    }
}
  • Takes three parameters: seatsPerRow, tier1Rows, and tier2Rows, which represent the number of seats in each row and the number of rows in the first and second tiers, respectively.
  • Creates 2D array theaterSeats with dimensions (tier1Rows + tier2Rows) x seatsPerRow and uses nested for loops to initialize each element of the array as a Seat object.
  • For each element, if the row number is less than tier1Rows, the Seat is created with true for availability and 1 for tier. Otherwise, the Seat is created with the same availability and 2 for tier.

Part B:

public boolean reassignSeat(int fromRow, int fromCol, int toRow, int toCol){
    if(!theaterSeats[toRow][toCol].isAvailable()){ return false }
    if(theaterSeats[toRow][toCol].getTier() >= theaterSeats[fromRow][fromCol].getTier()){
        theaterSeats[toRow][toCol].setAvailability(false);
        theaterSeats[fromRow][fromCol].setAvailability(true);
    }
    else{
        return false;
    }
}
  • Takes four parameters: fromRow, fromCol, toRow, and toCol, which represent the row and column indices of the seat to be moved and the destination row and column indices, respectively.
  • Checks if the destination seat is available. If not, it returns false and the method ends. If the destination seat is available, it checks if the destination seat's tier is greater than or equal to the tier of the seat being moved.
  • If available, it sets the availability of the destination seat to false, sets the availability of the moved seat to true, and returns true to indicate that the reassignment was successful. If the destination seat's tier is lower than the seat being moved, it returns false and the method ends without making any changes.

unkown • 6/16/2025, 12:07:10 PM