3.14. Unit 3 Free Response Question (FRQ) Game Practice¶
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.
3.14.1. 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.
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.
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.
3.14.2. 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.
The first step is to determine what they are asking you to write.
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.
- getPoints()
- The ``getPoints()`` method is given to you to get the points for a level.
- goalReached()
- The ``goalReached()`` method is given to you to check if the goal is reached for a level.
- play()
- The ``play()`` method is given to you to simulate the play of the game.
- getScore()
- Correct, the ``getScore()`` method is the one you will write for part a.
3-12-1: Which method are you asked to write for part a?
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.
- getPoints()
- The getPoints() method is given to you to get the points for a level.
- goalReached()
- The goalReached() method is given to you to check if the goal is reached for a level.
- isBonus()
- The isBonus() method is given to you to check if the game is a bonus game.
- getScore()
- The getScore() method is the one you will write for part a.
3-12-3: What are some methods given to you that you will need to use for part a?
There are two classes given to you in this FRQ. Which method belongs to each class?
-
3-12-4: Drag the method from the left and drop it on the correct class that it belongs to on the right. Click the "Check Me" button to see if you are correct.
Review the FRQ description above.
- getPoints()
- Level
- isBonus()
- Game
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.
- goalReached()
- The goalReached() method is a non-static method of the Level class. You need to call it with an object of the class.
- levelOne.goalReached()
- Correct, this calls the levelOne object's goalReached() method.
- Level.goalReached()
- The goalReached() method is a non-static method of the ``Level`` class. You need to call it with an object of the class, not the class name.
- goalReached(levelOne)
- The goalReached() method does not take an argument.
3-12-5: How would you call the goalReached()
method of the levelOne
object?
Let’s simplify the problem by first writing the code to add the points for levelOne
to a variable called score
.
- score += levelOnePoints
- There is no levelOnePoints variable.
- score += levelOne.getPoints()
- Correct, this adds levelOne's getPoints() to score.
- score = score + Level.getPoints()
- The getPoints()`` method is a non-static method of the Level class. You need to call it with an object of the class, not the class name.
- score = Level.points
- There is no points instance variable.
3-12-6: Which expression would add the points for levelOne
into a variable called score
?
3.14.3. 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.
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.
3.14.4. 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.