1. This question involves a trivia game simulation. The TriviaSimulation class tracks the results of a series of questions and calculates a score based on those results. You will write a constructor and a method in the TriviaSimulation class.
public class TriviaSimulation
{
/** The history of question results, formatted as a string of statuses separated by spaces */
private String resultHistory; // To be initialized in part (a)
/** The total score earned in the simulation */
private int score; // To be initialized in part (a)
/**
* Simulates a game with a specified number of questions,
* as described in part (a)
* Precondition: numQuestions > 0
*/
public TriviaSimulation(int numQuestions)
{ /* to be implemented in part (a) */ }
/**
* Returns the result of the next question.
* Returns "Pass" if answered correctly, "Fail" otherwise.
*/
public String getQuestionResult()
{ /* implementation not shown */ }
/**
* Returns the point value associated with a result status.
* Precondition: status is "Pass" or "Fail"
*/
public int getPoints(String status)
{ /* implementation not shown */ }
/**
* Returns the number of times "Pass" appears in resultHistory,
* as described in part (b)
*/
public int countPasses()
{ /* to be implemented in part (b) */ }
}Write the TriviaSimulation constructor, which simulates a game with numQuestions questions. The constructor should initialize resultHistory to an empty string and score to 0. It should then simulate the game by repeatedly calling the helper method getQuestionResult to obtain the result of each question.
For each question:
The simulation ends when numQuestions have been asked OR when the player fails two questions in a row. If getQuestionResult returns "Fail" for two consecutive calls, the simulation stops immediately, even if fewer than numQuestions have been asked.
Example 1
| Call Number | Return Value |
|---|---|
| 1 | "Pass" |
| 2 | "Fail" |
| 3 | "Pass" |
| 4 | "Pass" |
| 5 | "Fail" |
Example 2
| Call Number | Return Value |
|---|---|
| 1 | "Pass" |
| 2 | "Fail" |
| 3 | "Fail" |
/** * Simulates a game with a specified number of questions, * as described in part (a) * Precondition: numQuestions > 0 */ public TriviaSimulation(int numQuestions)
Write the countPasses method, which returns the number of times the string "Pass" appears in the instance variable resultHistory.
Example
| resultHistory Value | Return Value |
|---|---|
| "Pass Fail Pass Pass Fail " | 3 |
| "Fail Fail " | 0 |
/** * Returns the number of times "Pass" appears in resultHistory, * as described in part (b) */ public int countPasses()
00:00
public class TriviaSimulation
{
/** The history of question results, formatted as a string of statuses separated by spaces */
private String resultHistory; // To be initialized in part (a)
/** The total score earned in the simulation */
private int score; // To be initialized in part (a)
/**
* Simulates a game with a specified number of questions,
* as described in part (a)
* Precondition: numQuestions > 0
*/
public TriviaSimulation(int numQuestions)
{ /* to be implemented in part (a) */ }
/**
* Returns the result of the next question.
* Returns "Pass" if answered correctly, "Fail" otherwise.
*/
public String getQuestionResult()
{ /* implementation not shown */ }
/**
* Returns the point value associated with a result status.
* Precondition: status is "Pass" or "Fail"
*/
public int getPoints(String status)
{ /* implementation not shown */ }
/**
* Returns the number of times "Pass" appears in resultHistory,
* as described in part (b)
*/
public int countPasses()
{ /* to be implemented in part (b) */ }
}