← All free-response questionsQuestion 1 · Methods and Control Structures · 7 points
Quiz Game
A quiz game awards points each round. The QuizGame class tracks one player's game.
public class QuizGame
{
/** Returns the points earned in the given round
* (0 means the player answered incorrectly).
* Precondition: round >= 1
*/
private int pointsFor(int round)
{ /* implementation not shown */ }
/** Returns the player's score after playing up to maxRounds
* rounds, as described in part (a).
* Precondition: maxRounds >= 1
*/
public int playGame(int maxRounds)
{ /* to be implemented in part (a) */ }
/** Returns code with every vowel after the first character
* removed, as described in part (b).
* Precondition: code.length() >= 1; code is lowercase
*/
public static String shorten(String code)
{ /* to be implemented in part (b) */ }
}Part (a)
4 pointsWrite playGame. Rounds are played in order starting with round 1, up to maxRounds. Each round adds pointsFor(round) to the score, except that a round worth more than 50 points counts double. The game ends early, right after the first round worth 0 points. Return the final score.
| Round | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|
| pointsFor | 20 | 60 | 10 | 0 | 40 |
playGame(5) returns 20 + 120 + 10 + 0 = 150 (the game ends after round 4). playGame(2) returns 140.
Tab inserts spaces. Drafts save as you type.
Part (b)
3 pointsWrite shorten. It returns a new String that keeps the first character of code and every later character that is not a vowel (a, e, i, o, u).
| Call | Returns |
|---|---|
shorten("example") | "exmpl" |
shorten("aerial") | "arl" |
shorten("q") | "q" |
Tab inserts spaces. Drafts save as you type.
Self-score
0 / 7