Topics Covered

  • Method Declaration and Invocation:
    • Define methods, return types, and calling.
  • Control Structures:
    • Conditional statements (if, else if, else).
    • Loops (for, while).
    • Switch statements.
  • Variable Scope:
    • How variables are scoped within methods.
    • Accessing variables.
  • Parameter Passing:
    • Passing parameters to methods.
    • Pass-by-value.
  • Recursion:
    • Recursion-related questions.
    • Base cases and recursive calls.

Common Questions

  • Method Definition:
    • Defining methods with name, return type, parameters, and purpose.
  • Method Invocation:
    • Calling methods with specific arguments.
  • Conditional Statements:
    • Writing code using if, else if, and else.
  • Loops:
    • Implementing loops for problem-solving.
  • Variable Usage:
    • Using variables within methods.
    • Differentiating local and instance variables.
  • Recursion Examples:
    • Writing recursive solutions for problems.

Past AP Exam FRQs: 2022 #1

public class Level
{

  private boolean goalReached;
  private int points;

  public Level(boolean goalReached, int points) {
    this.goalReached = goalReached;
    this.points = points;
  }
  /** Returns true if the player reached the goal on this level and returns false otherwise */
  public boolean goalReached()
  { return this.goalReached; }
  /** Returns the number of points (a positive integer) recorded for this level */
  public int getPoints()
  { return this.points; }
  // There may be instance variables, constructors, and methods that are not shown.
} 

#1a) Write the getScore method, which returns the score for the most recently played game. Each game consists of three levels. The score for the game is computed using the following helper methods.

  • The isBonus method of the Game class returns true if this is a bonus game and returns false otherwise.
  • The goalReached method of the Level class returns true if the goal has been reached on a particular level and returns false otherwise.
  • The getPoints method of the Level class returns the number of points recorded on a particular level. Whether or not recorded points are earned (included in the game score) depends on the rules of the game, which follow

#2a) Write the playManyTimes method, which simulates the play of num games and returns the highest game score earned. For example, if the four plays of the game that are simulated as a result of the method call playManyTimes(4) earn scores of 75, 50, 90, and 20, then the method should return 90.

  • Play of the game is simulated by calling the helper method play. Note that 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.
  • Complete the playManyTimes method. Assume that getScore works as intended, regardless of what you wrote in part (a). You must call play and getScore appropriately in order to receive full credit. /** 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)
import java.util.ArrayList;

public class Game {
  private Level levelOne;
  private Level levelTwo;
  private Level levelThree;
  private boolean isBonus = false;
  private int totalPoints = 0;

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

  public int getScore() {

    int multiplier = isBonus() ? 3 : 1;

    if (levelOne.goalReached() && levelTwo.goalReached()) {
      if (levelThree.goalReached()) 
        return (levelOne.getPoints()+levelTwo.getPoints()+levelThree.getPoints())*multiplier;
      else 
        return (levelOne.getPoints()+levelTwo.getPoints())*multiplier;
    } else {
      return levelOne.getPoints()*multiplier; 
    }
  }

  public int playManyTimes(int num, boolean[] isBonuses) {
    ArrayList<Integer> scores = new ArrayList<Integer>();
    
    for (int i = 0; i < num; i++) {
      this.play(isBonuses[i]);
      scores.add(this.getScore());
      System.out.println(scores.get(i));
    }

    int largestScore = scores.get(0);

    for (int i = 0; i < scores.size(); i++) {
      if (scores.get(i) > largestScore) largestScore = scores.get(i);
    }

    return largestScore;
  }

  public boolean isBonus() {
    return this.isBonus;
  }

  public void play() {
    this.totalPoints += this.getScore();
  }

  public void play(boolean isBonus) {
    this.isBonus = isBonus;
    this.totalPoints += getScore();
  }

  public static void main(String[] args) {
    Level levelOne = new Level(true, 12);
    Level levelTwo = new Level(true, 20);
    Level levelThree = new Level(false, 5);

    Game newGame = new Game(levelOne, levelTwo, levelThree);

    boolean[] isBonuses = new boolean[]{false, false, false, true};
    
    System.out.println(newGame.playManyTimes(4, isBonuses));
  }
}

Game.main(null);
32
32
32
96
96

image image