2022 AP® Computer Science A FRQ 1 — Game (Methods & Conditional Logic)

 

 

 

Topic: Method implementation with conditional scoring and game simulation

Skills Tested: Method calls, boolean logic, conditional statements, loop simulation, maximum value tracking

Curriculum Alignment: Unit 2 (Methods), Unit 3 (Conditionals)

Question

This question involves simulation of the play and scoring of a single-player video game. In the game, a player attempts to complete three levels. A level in the game is represented by the Level class.

Provided Classes

public class Level {
    /** Returns true if the player reached the goal on this level 
     *  and returns false otherwise */
    public boolean goalReached() 
    { /* implementation not shown */ }
    
    /** Returns the number of points (a positive integer) 
     *  recorded for this level */
    public int getPoints() 
    { /* implementation not shown */ }
    
    // There may be instance variables, constructors, and methods 
    // that are not shown.
}
public class Game {
    private Level levelOne;
    private Level levelTwo;
    private Level levelThree;
    
    /** Postcondition: All instance variables have been initialized. */
    public Game() 
    { /* implementation not shown */ }
    
    /** Returns true if this game is a bonus game 
     *  and returns false otherwise */
    public boolean isBonus() 
    { /* implementation not shown */ }
    
    /** Simulates the play of this Game (consisting of three levels) 
     *  and updates all relevant game data */
    public void play() 
    { /* implementation not shown */ }
    
    /** Returns the score earned in the most recently played game, 
     *  as described in part (a) */
    public int getScore()
    { /* to be implemented in part (a) */ }
    
    /** Simulates the play of num games and returns the highest 
     *  score earned, as described in part (b)
     *  Precondition: num > 0 */
    public int playManyTimes(int num)
    { /* to be implemented in part (b) */ }
}

Part (a): Write the getScore Method

Write the getScore method, which returns the score for the most recently played game. The score is computed according to the following rules:

  • Level one points are earned only if the level one goal is reached.
  • Level two points are earned only if both the level one and level two goals are reached.
  • Level three points are earned only if the goals of all three levels are reached.
  • The score for the game is the sum of the points earned for levels one, two, and three.
  • If the game is a bonus game, the score for the game is tripled.

Example Score Calculations:

Level 1 Goal Reached? Level 2 Goal Reached? Level 3 Goal Reached? Level 1 Points Level 2 Points Level 3 Points Is Bonus? Score
true true true 200 100 500 false 800
true true false 200 100 500 false 300
true false true 200 100 500 true 600
false true true 200 100 500 true 0

Part (b): Write the playManyTimes Method

Write the playManyTimes method, which simulates the play of num games and returns the highest game score earned.

Example: If the four plays of the game simulated as a result of the method call playManyTimes(4) earn scores of 75, 50, 90, and 20, then the method should return 90.

Important notes:

  • Play of the game is simulated by calling the helper method play.
  • If play is called only one time followed by multiple consecutive calls to getScore, each call to getScore will return the score earned in the single simulated play of the game.

Solution & Explanation

Part (a) Solution: getScore Method

public int getScore() {
    int score = 0;
    
    // Check if level 1 goal is reached
    if (levelOne.goalReached()) {
        score += levelOne.getPoints();
        
        // Check if level 2 goal is also reached
        if (levelTwo.goalReached()) {
            score += levelTwo.getPoints();
            
            // Check if level 3 goal is also reached
            if (levelThree.goalReached()) {
                score += levelThree.getPoints();
            }
        }
    }
    
    // Triple the score if this is a bonus game
    if (isBonus()) {
        score = score * 3;
    }
    
    return score;
}

Explanation of Part (a):

  1. Initialize score variable: We start with score = 0 to accumulate points.
  2. Nested conditional logic: We use nested if statements to enforce the "all previous levels must be reached" rule:
    • First check if level 1 goal is reached → add level 1 points
    • Inside that, check if level 2 goal is reached → add level 2 points
    • Inside that, check if level 3 goal is reached → add level 3 points
  3. Bonus multiplier: After calculating the base score, check if isBonus() returns true. If so, multiply the score by 3.
  4. Return final score: Return the calculated score value.
⚠️ Scoring Notes for Part (a):
  • Must correctly call goalReached() on each level
  • Must correctly call getPoints() on each level
  • Must add points only when appropriate goals are reached
  • Must correctly use isBonus() to triple score
  • Nested conditionals are the clearest way to enforce "all previous" logic

Part (b) Solution: playManyTimes Method

public int playManyTimes(int num) {
    int highestScore = 0;
    
    for (int i = 0; i < num; i++) {
        play();  // Simulate one game
        int currentScore = getScore();  // Get the score for that game
        
        if (currentScore > highestScore) {
            highestScore = currentScore;
        }
    }
    
    return highestScore;
}

Explanation of Part (b):

  1. Initialize maximum tracker: Start with highestScore = 0 to track the best score seen so far.
  2. Loop exactly num times: Use a for loop to simulate exactly num games.
  3. Simulate and score each game:
    • Call play() to simulate one game
    • Call getScore() to retrieve the score for that game
  4. Update maximum: Compare currentScore to highestScore and update if current is higher.
  5. Return highest score: After all simulations, return the highest score found.
⚠️ Scoring Notes for Part (b):
  • Loop must iterate exactly num times
  • Must call play() in each iteration (not just once)
  • Must call getScore() after each play()
  • Must correctly compare and update maximum score
  • Common error: calling play() outside the loop

Detailed Explanation

Key Concepts Tested

1. Method Calls and Return Values

This FRQ tests your understanding of calling methods and using their return values in expressions. You must call goalReached(), getPoints(), isBonus(), play(), and getScore() correctly.

2. Conditional Logic

Part (a) requires nested if statements to implement the "cumulative achievement" logic where each level's points are only earned if all previous levels' goals were also reached.

3. Loop Simulation

Part (b) requires a loop to simulate multiple games. Each iteration must simulate a new game by calling play(), then retrieve its score.

4. Maximum Value Tracking

You must track the highest score across all simulations using a standard "find maximum" algorithm pattern.

Why Nested Conditionals?

The nested if structure in part (a) elegantly enforces the rule that "level N points are earned only if all previous levels' goals were reached." Each nested level automatically requires all outer conditions to be true.

Alternative approaches (using boolean variables to track whether all previous goals were met) are valid but more complex and error-prone.

Common Mistakes to Avoid

Part (a) Common Errors:

  • ❌ Adding points for level 2 without checking if level 1 goal was reached
  • ❌ Adding points for level 3 without checking if both level 1 and 2 goals were reached
  • ❌ Multiplying individual level scores by 3 instead of the total score
  • ❌ Forgetting to use the isBonus() method
  • ❌ Using && operators incorrectly (nested if is clearer)
  • ❌ Not initializing the score variable

Part (b) Common Errors:

  • ❌ Calling play() only once outside the loop
  • ❌ Not calling play() at all (just calling getScore())
  • ❌ Initializing highestScore to a negative value or using Integer.MIN_VALUE (0 is sufficient since scores are positive)
  • ❌ Comparing scores incorrectly (< instead of >)
  • ❌ Returning the last score instead of the highest score
  • ❌ Off-by-one errors in the loop (iterating num-1 or num+1 times)
💡 Practice Tip: Try writing this solution without looking at the answer first. Time yourself for 25 minutes total (15 minutes for part a, 10 minutes for part b) to simulate exam conditions. Focus on getting the logic correct before optimizing.

Scoring Rubric

Part (a): 4 Points

Point Criteria
1 Calls goalReached(), getPoints(), and isBonus() correctly
1 Adds level 1 points if level 1 goal is reached
1 Adds level 2 points only if both level 1 AND level 2 goals are reached
1 Adds level 3 points only if ALL THREE goals are reached, then triples score if bonus

Part (b): 3 Points

Point Criteria
1 Loop iterates exactly num times
1 Calls play() and getScore() correctly in loop
1 Correctly identifies and returns maximum score

Total: 7 points possible

📄 Official College Board Resources

Written by Tanner, Certified AP Computer Science Teacher with 5+ years teaching AP CSA.

Last updated:

Contact form