# Selection Free Response Question (FRQ) Game Practice (2.1-2.6)

Each coding exercise has its own folder. Open its `.java` file and press **Run** to try it. **Run Tests** checks every graded exercise in this lesson at once.

The AP CSA exam has 4 free response questions (FRQs) where you have to write Java code in 1.5 hours. The first FRQ is about Methods and Control Structures using expressions, methods, loops, and if statements. In the last unit, we tried a simplified FRQ 1 part a about the points earned in a video game with 3 levels using the class `Level`. You will now try the complete 2022 FRQ 1 part a from [https://apcentral.collegeboard.org/media/pdf/ap22-frq-computer-science-a.pdf](https://apcentral.collegeboard.org/media/pdf/ap22-frq-computer-science-a.pdf).

## FRQ 1 Part A Description (2022)

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.

```java
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.
}
```

Play of the game is represented by the `Game` class. You will write a method of the Game class.

```java
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) */ }

    // There may be instance variables, constructors, and methods that are not shown.
}
```

In part a, you will 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.

The `score` for the game 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.

## Warm up Exercises

FRQs often have a lot of dense text. It is a good idea to highlight important keywords and the methods and variables that you will need.

1. The first step is to determine what they are asking you to write.
2. The second step is to determine which methods given in the problem description you need to use in your solution.

Let’s practice this below.

> Check your understanding: Which method are you asked to write for part a? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/frq-game-score.html#frq_which_method

> Check your understanding: Select the phrases below which will probably correspond to an if clause of a conditional statement.
>
> ```
> The score for the game 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.
> ```
>
> Answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/frq-game-score.html#frqifs

> Check your understanding: What are some methods given to you that you will need to use for part a? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/frq-game-score.html#frq_which_methods_used

There are two classes given to you in this FRQ. Which method belongs to each class?

> Check your understanding: Drag the method from the left and drop it on the correct class that it belongs to on the right. — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/frq-game-score.html#frq_method_class_match

Notice that the `Game` class has 3 instance variables to represent each level, `levelOne`, `levelTwo`, and `levelThree` which are object of class type `Level`. You will need to use these variables to get their points and check if the goal is reached for each level.

> Check your understanding: How would you call the `goalReached()` method of the `levelOne` object? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/frq-game-score.html#frq_call_method

Let’s simplify the problem by first writing the code to add the points for `levelOne` to a variable called `score`.

> Check your understanding: Which expression would add the points for `levelOne` into a variable called `score`? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/frq-game-score.html#frq_expression

## Solve the Problem

Let’s write the code for the `getScore()` method. The method should use the `goalReached()` and `getPoints()` methods of the `Level` class to calculate the score for the game. It will need to check if the goal is reached for each level using the `levelOne`, `levelTwo`, and `levelThree` objects and add the points for each level to the score. If the game is a bonus game, which can be checked with the `isBonus()` method (no object needed since it is in the same class), the score will be tripled. At the end of the method, a `return` statement will return the score to the main method to be printed out.

### Exercise 1

Write the code for the `getScore()` method of the `Game` class. The method should use the `goalReached()` and `getPoints()` methods of the `levelOne`, `levelTwo` and `levelThree` objects to calculate the score for the game. If it is a `isBonus()` game, the score should be tripled.

> Open the `01-frq-getScore` folder and edit `Game.java`. Press **Run** to try it; **Run Tests** checks it (4 tests).

## AP Scoring Rubric

Here is the AP rubric for this problem. Did your code meet the requirements for all 4 points? Notice that even a partial solution would get some of the points. It is not all or nothing. In class, your teacher may have you grade each others’ code.

![AP Rubric](images/ap-frq-scoring.png)

*AP Rubric*

## More FRQ Practice with Ifs

Try the following additional FRQ 1 practice problem from the Oracle learn.java site:

- [FRQ If Statements Practice](https://learn.java/learning/lessons/algorithms/apfrq-ifstatements/)
