Computer Science A
2022 FRQ 1
- FRQ Question
- FRQ Question Part A
- Code Runner Challenge
- Scoring Guidelines Part A
- FRQ Question Part B
- Code Runner Challenge
- Scoring Guidelines Part B
FRQ 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.
// Code Example for FRQ
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 two methods 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. }
FRQ Question Part A
(a) 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.
| Level One Results | Level Two Results | Level Three Results | isBonus Return Value | Score Calculation |
|---|---|---|---|---|
| goalReached: true getPoints: 200 |
goalReached: true getPoints: 100 |
goalReached: true getPoints: 500 |
true | (200 + 100 + 500) × 3 = 2,400 The recorded points for levels one, two, and three are earned because the goals were reached in all three levels. The earned points are multiplied by 3 because isBonus returns true. |
| goalReached: true getPoints: 200 |
goalReached: true getPoints: 100 |
goalReached: false getPoints: 500 |
false | 200 + 100 = 300 The recorded points for level one and level two are earned because the goal was reached in levels one and two. The recorded points for level three are not earned because the goal was not reached in level three. |
| goalReached: true getPoints: 200 |
goalReached: false getPoints: 100 |
goalReached: true getPoints: 500 |
true | 200 × 3 = 600 The recorded points for only level one are earned because the goal was not reached in level two. The earned points are multiplied by 3 because isBonus returns true. |
| goalReached: false getPoints: 200 |
goalReached: true getPoints: 100 |
goalReached: true getPoints: 500 |
false | 0 Because the goal in level one was not reached, no points are earned for any level. |
Complete the getScore method.
/** Returns the score earned in the most recently played game, as described in part (a) */
public int getScore()
Code Runner Challenge
FRQ #1 (a) - getScore() - Calculate game score based on level goals and bonus
View IPYNB Source
// CODE_RUNNER: FRQ #1 (a) - getScore() - Calculate game score based on level goals and bonus
class Level {
private boolean goal;
private int points;
public Level(boolean g, int p) {
goal = g;
points = p;
}
public boolean goalReached() {
return goal;
}
public int getPoints() {
return points;
}
}
class Main {
private Level levelOne;
private Level levelTwo;
private Level levelThree;
private boolean bonus;
public Main(Level l1, Level l2, Level l3, boolean b) {
levelOne = l1;
levelTwo = l2;
levelThree = l3;
bonus = b;
}
public boolean isBonus() {
return bonus;
}
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;
}
// REQUIRED for code runner
public static void main(String[] args) {
// Test case 1: All goals reached, bonus game
Level l1 = new Level(true, 200);
Level l2 = new Level(true, 100);
Level l3 = new Level(true, 500);
//Main=Game but coderunner uses Main
Main g1 = new Main(l1, l2, l3, true);
System.out.println("Test 1 - All goals, bonus: " + g1.getScore());
// Test case 2: First two goals, no bonus
Level l4 = new Level(true, 200);
Level l5 = new Level(true, 100);
Level l6 = new Level(false, 500);
Main g2 = new Main(l4, l5, l6, false);
System.out.println("Test 2 - First two goals, no bonus: " + g2.getScore());
// Test case 3: Only first goal, bonus
Level l7 = new Level(true, 200);
Level l8 = new Level(false, 100);
Level l9 = new Level(true, 500);
Main g3 = new Main(l7, l8, l9, true);
System.out.println("Test 3 - Only first goal, bonus: " + g3.getScore());
// Test case 4: No first goal
Level l10 = new Level(false, 200);
Level l11 = new Level(true, 100);
Level l12 = new Level(true, 500);
Main g4 = new Main(l10, l11, l12, false);
System.out.println("Test 4 - No first goal: " + g4.getScore());
}
}
Game.main(null);
Scoring Guidelines Part A
| # | Scoring Criteria | Decision Rules | Points |
|---|---|---|---|
| 1 | Calls getPoints, goalReached, and isBonus |
Will NOT earn the point if: - fails to call getPoints or goalReached on a Level object- calls isBonus on an object other than this (use of this is optional)- includes parameters |
1 |
| 2 | Determines if points are earned based on goalReached return values |
Can still earn the point if: - calculates the score total incorrectly - calls goalReached incorrectly- fails to distinguish all cases correctly Will NOT earn the point if: - fails to use a nested if statement or equivalent |
1 |
| 3 | Guards update of score for bonus game based on isBonus return value |
Can still earn the point if: - triples the calculated score incorrectly - updates the score with something other than tripling - calls isBonus incorrectlyWill NOT earn the point if: - uses the isBonus return value incorrectly |
1 |
| 4 | Initializes and accumulates appropriate score (algorithm) | Can still earn the point if: - calls methods incorrectly, as long as method calls are attempted - fails to return the score (return is not assessed) Will NOT earn the point if: - calculates the score total incorrectly - triples the calculated score incorrectly |
1 |
Total for part (a): 4 points
FRQ Question Part B
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)
Code Runner Challenge
FRQ #1 (b) - playManyTimes() - Simulate multiple games and return highest score
View IPYNB Source
// CODE_RUNNER: FRQ #1 (b) - playManyTimes() - Simulate multiple games and return highest score
class Level {
private boolean goal;
private int points;
public Level(boolean g, int p) {
goal = g;
points = p;
}
public boolean goalReached() {
return goal;
}
public int getPoints() {
return points;
}
}
//Should be Game but coderunner
class Main {
private Level levelOne;
private Level levelTwo;
private Level levelThree;
private boolean bonus;
private int currentScore;
private int gameCount;
public Main(Level l1, Level l2, Level l3, boolean b) {
levelOne = l1;
levelTwo = l2;
levelThree = l3;
bonus = b;
gameCount = 0;
}
public boolean isBonus() {
return bonus;
}
public void play() {
// Simulate playing a game - each play generates a new random score
gameCount++;
// Simulate different game outcomes
int[] scores = {75, 50, 90, 20, 100, 60};
currentScore = scores[gameCount % scores.length];
}
public int getScore() {
return currentScore;
}
public int playManyTimes(int num) {
int maxScore = 0;
for (int i = 0; i < num; i++) {
play();
int currentScore = getScore();
if (currentScore > maxScore) {
maxScore = currentScore;
}
}
return maxScore;
}
// REQUIRED for code runner
public static void main(String[] args) {
Level l1 = new Level(true, 200);
Level l2 = new Level(true, 100);
Level l3 = new Level(true, 500);
Main g = new Main(l1, l2, l3, false);
System.out.println("Playing 4 games...");
int max = g.playManyTimes(4);
System.out.println("Highest score: " + max);
System.out.println("Playing 6 games...");
Main g2 = new Main(l1, l2, l3, false);
int max2 = g2.playManyTimes(6);
System.out.println("Highest score: " + max2);
}
}
Game.main(null);
Scoring Guidelines Part B
| # | Scoring Criteria | Decision Rules | Points |
|---|---|---|---|
| 5 | Loops num times |
Can still earn the point if: - returns early |
1 |
| 6 | Calls play and getScore |
Will NOT earn the point if: - calls either method on an object other than this (use of this is optional)- includes parameters |
1 |
| 7 | Compares a score to an identified max or to another score | Can still earn the point if: - makes the comparison outside the loop - calls getScore incorrectly- fails to call play between calls to getScore |
1 |
| 8 | Identifies the maximum score (algorithm) | Will NOT earn the point if: - fails to initialize the result variable - compares a score to an identified max or another score outside the loop - fails to call play exactly once each time through the loop |
1 |
| 9 | Returns identified maximum score | Can still earn the point if: - calculates the maximum score incorrectly Will NOT earn the point if: - assigns a value to the identified maximum score without any loop or logic to find the maximum |
1 |
Total for part (b): 5 points
Total for Question 1: 9 points