2022 AP CSA FRQ #1: Methods and Control Structures

Part A: getScore method

public int getScore(){

    int score = 0;

    if(levelOne.goalReached){
        score += levelOne.getPoints;
        if(levelTwo.goalReached){
            score += levelTwo.getPoints;
            if(levelThree.goalReached){
                score += levelThree.getPoints;

            }
        }

    }

    if (Game.isBonus){
        score *= 3;
    }

    return score;

}

Part B: playManyTimes method

public int playManyTimes(int num){

    int highScore = 0;

    for(int i = 0; i < num; i++){
        play();
        int score = getScore;

        if(score > highScore){
            highScore = score;
        }

    }

    return highScore;

}

2022 Methods and Control Structures FRQ

Generally speaking, the Methods and Control Structures FRQ involve having to write the code for a certain method that College Board provides you with for a specified purpose. For example, with the FRQ I just did, in part a, I had to create a method for getScore, which returned the score of the user. If the game that the user played was a bonus round, their total score would be multiplied by 3. For part b, I had to write the code for the playManyTimes method, which returned the highest score of the games played if the play() method was called more than once.

Runnable Versions of the FRQ

Part A Runnable Version

public class Level {
    private boolean isGoalReached;
    private int points;

    public Level(boolean goalReached, int points) {
        this.isGoalReached = goalReached;
        this.points = points;
    }
    
    public boolean goalReached() {
        return isGoalReached;
    }

    public void setGoalReached(boolean isGoalReached) {
        this.isGoalReached = isGoalReached;
    }
    
    public int getPoints() {
        return points;
    }
}

class Game {
    private Level levelOne;
    private Level levelTwo;
    private Level levelThree;
    private boolean isBonus;

    public Game(Level l1, Level l2, Level l3) {
        this.levelOne = l1;
        this.levelTwo = l2;
        this.levelThree = l3;
    }

    public void isBonus() {
        this.isBonus = Math.random() > 0.5; 
    }

    public void play() {
        levelOne.setGoalReached(Math.random() > 0.5);
        levelTwo.setGoalReached(Math.random() > 0.5);
        levelThree.setGoalReached(Math.random() > 0.5);
        isBonus = Math.random() > 0.5;
    }

    public int getScore() {
        int score = 0;
        
        if (levelOne.goalReached()) {
            score += levelOne.getPoints();
            if (levelTwo.goalReached()) {
                score += levelTwo.getPoints();
                if (levelThree.goalReached()) {
                    score += levelThree.getPoints();
                }
            }
        }
        
        if (isBonus) {
            score *= 3;
        }
        
        return score;
    }

    

    public void displayScore() {
        int score = getScore();

        if (isBonus) {
            System.out.println("Your Original Score: " + score/3);
            System.out.println("Your Final Score (After Tripling): " + score);
        } else {
            System.out.println("Your Final Score (No Tripling): " + score);
        }
    }
}

Part A Testing

public class GameScoreTest {
    public static void main(String[] args) {
        Level l1 = new Level(true, 300);
        Level l2 = new Level(false, 100);
        Level l3 = new Level(true, 500);

        Game game = new Game(l1, l2, l3);
        game.isBonus();
        game.play();
        game.displayScore();
    }
}

GameScoreTest.main(null);
Your Original Score: 300
Your Final Score (After Tripling): 900

Part B Runnable Version

public class GameSimulator {

    Game game;

    public GameSimulator(Level l1, Level l2, Level l3) {
        game = new Game(l1, l2, l3);
    }

    public int playManyTimes(int num) {
        int highScore = 0;

        for (int i = 0; i < num; i++) {
            game.play();
            int score = game.getScore(); 

            if (score > highScore) {
                highScore = score;
            }
        }

        return highScore;
    }

}

Part B Testing

public class GameSimulatorTest {
    public static void main(String[] args) {
        Level l1 = new Level(true, 100);
        Level l2 = new Level(true, 200);
        Level l3 = new Level(false, 300);  

        GameSimulator simulator = new GameSimulator(l1, l2, l3);

        int highestScore = simulator.playManyTimes(4);
        
        System.out.println("Highest Score Achieved: " + highestScore);
    }
}

GameSimulatorTest.main(null);
Highest Score Achieved: 300