2020 Practice Exam #1 FRQ

#1

import java.util.ArrayList;

/**
 * Main class for Hailstone.java
 *
 */
public class Hailstone
{
    /**
     * generateHailstone 
     * 
     * @param n
     * @return
     */
    private static ArrayList<Integer> generateHailstone(int n)
    {
        ArrayList<Integer> hailstone = new ArrayList<Integer>();
        
        while (n > 1)
        {
            hailstone.add(n);

            if (n % 2 == 0)
            {
                n /= 2;
            }
            else
            {
                n = 3 * n + 1;
            }
        }
        if (n == 1)
        {
            hailstone.add(n);
        }
        return hailstone;
    }

    /**
     * Returns the length of a hailstone sequence that starts with n, as described
     * in part (a).
     * 
     * Precondition: n > 0
     */
    public static int hailstoneLength(int n)
    {
        ArrayList<Integer> hailstone = Hailstone.generateHailstone(n);

        return hailstone.size();
    }

    /**
     * Returns true if the hailstone sequence that starts with n is considered long
     * and false otherwise, as described in part (b).
     * 
     * Precondition: n > 0
     */
    public static boolean isLongSeq(int n)
    {
        ArrayList<Integer> hailstone = Hailstone.generateHailstone(n);

        if (n < hailstone.size())
        {
            return true;
        }
        return false;
    }

    /**
     * Returns the proportion of the first n hailstone sequences that are considered
     * long, as described in part (c).
     * 
     * Precondition: n > 0
     */
    public static double propLong(int n)
    {
        double proportion = 0.0;
        int numLong = 0;
        
        for (int i = 1; i <= n; i++)
        {
            Boolean result = isLongSeq(i);
            System.out.printf("Result of %d is %s\n", i, result.toString());

            if (result)
            {
                numLong++;
                System.out.println(numLong);
            }
        }
        
        proportion = (double) numLong / n;
        /* to be implemented in part (c) */
        return  proportion;
    }

    public static void printHailstone(int n)
    {
        ArrayList<Integer> hailstone = Hailstone.generateHailstone(n);

        for (int i = 0; i < hailstone.size(); i++)
        {
            System.out.println(hailstone.get(i));
        }
    }

    // There may be instance variables, constructors, and methods not shown.

    public static void main(String[] args)
    {
        Hailstone.printHailstone(5);

        int num = 5;
        int test = hailstoneLength(num);
        boolean test2 = isLongSeq(num);
        
        double p = propLong(3);

        System.out.printf("The length of hailstone: %d\n", test);
        System.out.printf("Is long? %b\n", test2);
        System.out.printf("Proportion of long sequences: %.3f\n", p);
    }
}
Hailstone.main(null);
5
16
8
4
2
1
Result of 1 is false
Result of 2 is false
Result of 3 is true
1
The length of hailstone: 6
Is long? true
Proportion of long sequences: 0.333

#2

/**
 * GameSpinner.java
 *
 * Description
 *
 * @author Natalie Beckwith
 * @version 1
 */

/**
 * Main class for GameSpinner.java
 *
 */
public class GameSpinner
{
    public int _sectors = 0;
    public int _currentConsecutiveSpin = 0;
    public int _previousSpin = 0;
    public Random _random = new Random();
    
    /**
     * Constructor for class
     *
     */
    public GameSpinner(int sectors)
    {
        _sectors = sectors;
        
    }
    
    /**
     * Reporting the length of the current run, the number of consecutive
     * spins that are the same as the most recent spin 
     * 
     * @return
     */
    public int currentRun()
    {
        
        return _currentConsecutiveSpin;
    }
    
    /**
     * Spinning a spinner and reporting the result 
     * 
     * @return
     */
    public int spin()
    {
        int spin = _random.nextInt(1, _sectors + 1);
                    
        if(spin == _previousSpin)
        {
            _currentConsecutiveSpin++;
        }
        else
        {
            _currentConsecutiveSpin = 1;
        }
        
        _previousSpin = spin;
        
        return spin;
    }
    
    /**
     * main 
     * 
     * @param args
     */
    public static void main(String[] args)
    {
        GameSpinner g = new GameSpinner(4);
        
        for( int i = 0; i < 15; i++)
        {
            System.out.println("Current run: " + g.currentRun());
            System.out.println("Spin: " + g.spin());
            System.out.println("Current run: " + g.currentRun());
            System.out.println();
        }
    }
}
GameSpinner.main(null);
Current run: 0
Spin: 2
Current run: 1

Current run: 1
Spin: 3
Current run: 1

Current run: 1
Spin: 1
Current run: 1

Current run: 1
Spin: 4
Current run: 1

Current run: 1
Spin: 2
Current run: 1

Current run: 1
Spin: 1
Current run: 1

Current run: 1
Spin: 1
Current run: 2

Current run: 2
Spin: 4
Current run: 1

Current run: 1
Spin: 3
Current run: 1

Current run: 1
Spin: 1
Current run: 1

Current run: 1
Spin: 3
Current run: 1

Current run: 1
Spin: 3
Current run: 2

Current run: 2
Spin: 2
Current run: 1

Current run: 1
Spin: 3
Current run: 1

Current run: 1
Spin: 2
Current run: 1

#3

/**
 * ProductReview.java
 *
 * Description
 *
 * @author Natalie Beckwith
 * @version 1
 */

/**
 * Main class for ProductReview.java
 *
 */
public class ProductReview
{
    private String name;
    private String review;
    
   /**
    * Constructs a ProductReview object and initializes the instance variables.
    */
    public ProductReview(String pName, String pReview)
    {
        name = pName;
        review = pReview;
    }
    
   /**
    * Returns the name of the product.
    */
    public String getName()
    {
        return name;
    }
    
   /**
    * Returns the review of the product.
    */
    public String getReview()
    {
        return review;
    }

}
/**
 * ReviewCollector.java
 *
 * Description
 *
 * @author Natalie Beckwith
 * @version 1
 */

/**
 * Main class for ReviewCollector.java
 *
 */
public class ReviewCollector
{
    private ArrayList<ProductReview> _reviewList;
    private ArrayList<String> _productList;

    /**
     * Constructs a ReviewCollector object and initializes the instance variables.
     */
    public ReviewCollector()
    {
        _reviewList = new ArrayList<ProductReview>();
        _productList = new ArrayList<String>();
    }

    /**
     * Adds a new review to the collection of reviews, as described in part (a).
     */
    public void addReview(ProductReview prodReview)
    {
        /* to be implemented in part (a) */

        _reviewList.add(prodReview);

        if (!_productList.contains(prodReview.getName()))
        {
            _productList.add(prodReview.getName());
        }
    }

    /**
     * Returns the number of good reviews for a given product name, as described in
     * part (b).
     */
    public int getNumGoodReviews(String prodName)
    {
        int numGood = 0;
        for(int i = 0; i < _reviewList.size(); i++)
        {
            ProductReview review = _reviewList.get(i);
            if (review.getName().equals(prodName))
            {
                String tempReview = review.getReview();
                
                if(tempReview.contains("good"))
                {
                    numGood++;
                }
            }
        }
        /* to be implemented in part (b) */
        return numGood;
    }

    /**
     * main 
     * 
     * @param args
     */
    public static void main(String[] args)
    {
        ProductReview product1Review1 = new ProductReview("product1", "this is a good review");
        
        ProductReview product1Review2 = new ProductReview("product1", "this is a Good review 2");
        
        ProductReview product1Review3 = new ProductReview("product1", "this is a GOOD review 3");
        
        ProductReview product2Review1 = new ProductReview("product2", "this is a good review 3");
        
        ProductReview product3Review1 = new ProductReview("product3", "this is a good review 3");
        ProductReview product3Review2 = new ProductReview("product3", "this is another good review 3");
        
        ReviewCollector review = new ReviewCollector();
        
        review.addReview(product1Review1);
        review.addReview(product1Review2);
        review.addReview(product1Review3);
        review.addReview(product2Review1);
        review.addReview(product3Review1);
        review.addReview(product3Review2);
        
        System.out.println("product list: " + review._productList);
        
        
        System.out.println("# of reviews: " + review.getNumGoodReviews(product1Review1.getName()));
        
        System.out.println("# of reviews: " + review.getNumGoodReviews(product2Review1.getName()));
        
        System.out.println("# of reviews: " + review.getNumGoodReviews(product3Review1.getName()));
                
        
    }

}
ReviewCollector.main(null);
product list: [product1, product2, product3]
# of reviews: 1
# of reviews: 1
# of reviews: 2

#4

/**
 * Seat.java
 *
 * Description
 *
 * @author Natalie Beckwith
 * @version 1
 */

/**
 * Main class for Seat.java
 *
 */
public class Seat
{
    private boolean available;

    private int tier;

    /**
     * Constructor for class
     *
     * @param isAvail
     * @param tierNum
     */
    public Seat(boolean isAvail, int tierNum)
    {
        available = isAvail;

        tier = tierNum;
    }

    /**
     * isAvailable 
     * 
     * @return
     */
    public boolean isAvailable()
    {
        return available;
    }

    /**
     * getTier 
     * 
     * @return
     */
    public int getTier()
    {
        return tier;
    }

    /**
     * setAvailability 
     * 
     * @param isAvail
     */
    public void setAvailability(boolean isAvail)
    {
        available = isAvail;
    }

}
/**
 * Theater.java
 *
 * Description
 *
 * @author Natalie Beckwith
 * @version 1
 */

/**
 * Main class for Theater.java
 *
 */
public class Theater
{
    private Seat[][] _theaterSeats;
    private int _seatsPerRow;
    private int _tier1Rows;
    private int _tier2Rows;

    /**
     * Constructs a Theater object, as described in part (a).
     * 
     * Precondition: seatsPerRow > 0; tier1Rows > 0; tier2Rows >= 0
     * 
     */
    public Theater(int seatsPerRow, int tier1Rows, int tier2Rows)
    {
        _seatsPerRow = seatsPerRow;
        _tier1Rows = tier1Rows;
        _tier2Rows = tier2Rows;
        
        _theaterSeats = new Seat[_tier1Rows + _tier2Rows][_seatsPerRow];
        
        
            // t1
            for(int t1 = 0; t1 < _tier1Rows; t1++)
            {
                for(int col = 0; col < _seatsPerRow; col++)
                {
                    Seat tempSeat = new Seat(true, 1);
                    _theaterSeats[t1][col] = tempSeat;
                }
            }
            
            //t2
            for(int t2 = 0; t2 < _tier2Rows; t2++)
            {
                for(int col = 0; col < _seatsPerRow; col++)
                {
                    Seat tempSeat = new Seat(true, 2);
                    _theaterSeats[t2+_tier1Rows][col] = tempSeat;
                }
            }
            
        /* to be implemented in part (a) */
    }

    /**
     * Returns true if a seat holder was reassigned from the seat at fromRow,
     * fromCol to the seat at toRow, toCol; otherwise it returns false, as
     * described in part (b).
     * 
     * Precondition: fromRow, fromCol, toRow, and toCol represent valid row and
     * column positions in the theater.
     * 
     * The seat at fromRow, fromCol is not available.
     */
    public boolean reassignSeat(int fromRow, int fromCol, int toRow, int toCol)
    {
        Seat oldSeat = _theaterSeats[fromRow][fromCol];
        
        Seat newSeat = _theaterSeats[toRow][toCol];
        
        if((newSeat.isAvailable()) && (oldSeat.getTier() <= newSeat.getTier()))
        {
            newSeat.setAvailability(false);
            return true;
        }
        return false;
        
    }
    
    /**
     * main 
     * 
     * @param args
     */
    public static void main(String[] args)
    {
        Theater theatre = new Theater(10, 4, 6);
        
        for(int row = 0; row < (theatre._tier1Rows + theatre._tier2Rows); row++)
        {
            for( int col = 0; col < theatre._seatsPerRow; col++)
            {
                System.out.print(theatre._theaterSeats[row][col].getTier() + "-" + theatre._theaterSeats[row][col].isAvailable()+ "\t");
            }
            System.out.println();
        }
        
        boolean seatChange1 = theatre.reassignSeat(5, 5, 5, 3); // should succeed
        boolean seatChange2 = theatre.reassignSeat(9, 5, 1, 3); // should fail
        boolean seatChange3 = theatre.reassignSeat(5, 4, 5, 3); // should fail
        
        System.out.println("\n" + seatChange1);
        System.out.println(seatChange2);
        System.out.println(seatChange3);
    }
}
Theater.main(null);
1-true	1-true	1-true	1-true	1-true	1-true	1-true	1-true	1-true	1-true	
1-true	1-true	1-true	1-true	1-true	1-true	1-true	1-true	1-true	1-true	
1-true	1-true	1-true	1-true	1-true	1-true	1-true	1-true	1-true	1-true	
1-true	1-true	1-true	1-true	1-true	1-true	1-true	1-true	1-true	1-true	
2-true	2-true	2-true	2-true	2-true	2-true	2-true	2-true	2-true	2-true	
2-true	2-true	2-true	2-true	2-true	2-true	2-true	2-true	2-true	2-true	
2-true	2-true	2-true	2-true	2-true	2-true	2-true	2-true	2-true	2-true	
2-true	2-true	2-true	2-true	2-true	2-true	2-true	2-true	2-true	2-true	
2-true	2-true	2-true	2-true	2-true	2-true	2-true	2-true	2-true	2-true	
2-true	2-true	2-true	2-true	2-true	2-true	2-true	2-true	2-true	2-true	

true
false
false

2015 Practice Exam FRQ

#1

/**
 * Arrays.java
 *
 * Description
 *
 * @author Natalie Beckwith
 * @version 1
 */

/**
 * Main class for Arrays.java
 *
 */
public class Arrays
{
    /**
     * arraySum 
     * 
     * @param arr
     * @return
     */
    public static int arraySum(int[] arr)
    {
        int sum = 0;
        
        for(int i = 0; i < arr.length; i++)
        {
            sum += arr[i];
        }
        return sum;
    }

    /**
     * rowSums 
     * 
     * @param arr2D
     * @return
     */
    public static int[] rowSums(int[][] arr2D)
    {
        int[] sums = new int[arr2D.length];
        
        for(int row = 0; row < arr2D.length; row++)
        {
            int sum = 0;
            for(int col = 0; col < arr2D[0].length; col++)
            {
                sum += arr2D[row][col];
            }
            sums[row] = sum;
        }
        return sums;
    }

    /**
     * isDiverse 
     * 
     * @param arr2D
     * @return
     */
    public static boolean isDiverse(int[][] arr2D)
    {
        int[] sums = rowSums(arr2D);
        
        for(int i = 0; i < sums.length; i++)
        {
            for(int j = i + 1; j < sums.length; j++)
            {
                if(i != j && (sums[i] == sums[j]))
                {
                    return false;
                }
            }
        }
        return true;
    }

    /**
     * main 
     * 
     * @param args
     */
    public static void main(String[] args)
    {
        int[] arr = {1, 3, 2, 7, 3};
        int[][] arr2D = {{1, 3, 2, 7, 3},
                         {10, 10, 4, 6, 2},
                         {5, 3, 5, 9, 6},
                         {7, 6, 4, 2, 1}};
        
        int[][] mat2 = {{1, 1, 5, 3, 4},
                        {12, 7, 6, 1, 9},
                        {8, 11, 10, 2, 5},
                        {3, 2, 3, 0, 6}};
        
        System.out.println("Sum of array: " + arraySum(arr));
        
        int sums[] = rowSums(arr2D);
        for(int i = 0; i < arr2D.length; i++)
        {
            System.out.println("Sum of 2D array: " + sums[i]);
        }
        
        System.out.println();
        
        int sums2[] = rowSums(mat2);
        for(int i = 0; i < mat2.length; i++)
        {
            System.out.println("Sum of 2D array: " + sums2[i]);
        }
        
        System.out.println("Diverse array?: " + isDiverse(arr2D));
        System.out.println("Diverse array?: " + isDiverse(mat2));
    }
}
Arrays.main(null);
Sum of array: 16
Sum of 2D array: 16
Sum of 2D array: 32
Sum of 2D array: 28
Sum of 2D array: 20

Sum of 2D array: 14
Sum of 2D array: 35
Sum of 2D array: 36
Sum of 2D array: 14
Diverse array?: true
Diverse array?: false

#2

/**
 * HiddenWord.java
 *
 * Description
 *
 * @author Natalie Beckwith
 * @version 1
 */

/**
 * Main class for HiddenWord.java
 *
 */
public class HiddenWord
{
    private String _word;
    
    /**
     * Constructor for class
     *
     * @param hWord
     */
    public HiddenWord (String hWord)
    {
        _word = hWord;
    }
    
    /**
     * getHint 
     * 
     * @param guess
     * @return
     */
    public String getHint (String guess)
    {
        String hint = "";
        
        for(int i = 0; i < guess.length(); i++)
        {
            if (guess.substring(i, i + 1).equals(_word.substring(i, i + 1)))
            {
                hint += guess.substring(i, i + 1);
            }
            else if (_word.indexOf(guess.substring(i, i + 1)) != -1)
            {
                hint += "X";
            }
            else
            {
                hint += "_";
            }
        }
        return hint;
    }
    
    /**
     * main 
     * 
     * @param args
     */
    public static void main (String[] args)
    {
        HiddenWord puzzle = new HiddenWord("HARPS");
        
        System.out.println(puzzle.getHint("AAAAA"));
        System.out.println(puzzle.getHint("HELLO"));
        System.out.println(puzzle.getHint("HEART"));
        System.out.println(puzzle.getHint("HARMS"));
        System.out.println(puzzle.getHint("HARPS"));
        
    }
}
HiddenWord.main(null);
XAXXX
H____
H_XX_
HAR_S
HARPS

#3

/**
 * SparseArrayEntry.java
 *
 * Description
 *
 * @author Natalie Beckwith
 * @version 1
 */

/**
 * Main class for SparseArrayEntry.java
 *
 */
public class SparseArrayEntry
{
    private int _row;
    private int _col;
    
    private int _value;
    
    /**
     * Constructor for class
     *
     * @param row
     * @param col
     * @param value
     */
    public SparseArrayEntry(int row, int col , int value)
    {
        _row = row;
        _col = col;
        _value = value;
    }
    
    /**
     * getRow 
     * 
     * @return
     */
    public int getRow()
    {
        return _row;
    }
    
    /**
     * getCol 
     * 
     * @return
     */
    public int getCol()
    {
        return _col;
    }
    
    /**
     * getValue 
     * 
     * @return
     */
    public int getValue()
    {
        return _value;
    }
}
import java.util.ArrayList;
import java.util.List;

/**
 * SparseArray.java
 *
 * Description
 *
 * @author Natalie Beckwith
 * @version 1
 */

/**
 * Main class for SparseArray.java
 *
 */
public class SparseArray
{
    private int numRows;
    private int numCols;
    
    private List <SparseArrayEntry> entries;
    
    /**
     * Constructor for class
     *
     */
    public SparseArray()
    {
        entries = new ArrayList <SparseArrayEntry>();
    }
    
    /**
     * getNumRows 
     * 
     * @return
     */
    public int getNumRows()
    {
        return numRows;
    }
    
    /**
     * getNumCols 
     * 
     * @return
     */
    public int getNumCols()
    {
        return numCols;
    }
    
    /**
     * getValueAt 
     * 
     * @param row
     * @param col
     * @return
     */
    public int getValueAt (int row, int col)
    {
        for(SparseArrayEntry entry : entries)
        {
            if (entry.getRow() == row && entry.getCol() == col)
            {
                return entry.getValue();
            }
        }
        return 0;
    }
    
    /**
     * removeColumn 
     * 
     * @param col
     */
    public void removeColumn (int col)
    {
        int i = 0;
        
        while (i < entries.size())
        {
            SparseArrayEntry entry = entries.get(i);
            if (entry.getCol() == col)
            {
                entries.remove(i);
            }
            else if (entry.getCol() > col)
            {
                entries.set(i, new SparseArrayEntry(entry.getRow(),
                                                    entry.getCol()-1,
                                                    entry.getValue()));
                
                i++;
            }
            else
            {
                i++;
            }
            numCols--;
        }
    }
    
    /**
     * main 
     * 
     * @param args
     */
    public static void main (String[] args)
    {
        SparseArray sparse = new SparseArray();
        
        System.out.println(sparse.getValueAt(1, 4));
    }
}
SparseArray.main(null);
0

#4

/**
 * NumberGroup.java
 *
 * Description
 *
 * @author Natalie Beckwith
 * @version 1
 */


/**
 * Main class for NumberGroup.java
 *
 */
public interface NumberGroup
{
    boolean contains (int num);
}
/**
 * Range.java
 *
 * Description
 *
 * @author Natalie Beckwith
 * @version 1
 */

/**
 * Main class for Range.java
 *
 */
public class Range implements NumberGroup
{
    private List<NumberGroup> groupList;

    private int _min;
    private int _max;

    /**
     * Constructor for class
     *
     * @param min
     * @param max
     */
    public Range(int min, int max)
    {
        _min = min;
        _max = max;
    }

    /**
     *
     */
    public boolean contains(int num)
    {
        return num >= _min && num <= _max;
    }

    /**
     * contains2 
     * 
     * @param num
     * @return
     */
    public boolean contains2(int num)
    {
        for (NumberGroup group : groupList)
        {
            if (group.contains(num))
            {
                return true;
            }
        }
        return false;
    }
    
    /**
     * main 
     * 
     * @param args
     */
    public static void main (String[] args)
    {
        NumberGroup range1 = new Range(-3, 2);
        
        System.out.println(range1.contains(0));
    }
}
Range.main(null);
true