204k views
4 votes
Create a class named TriviaGameV1 that plays a simple trivia game. The game should have five questions. Each question has a corresponding answer and point value between 1 and 3 based on the difficulty of the question. TriviaGameV1 will use three arrays. An array of type String should be used for the questions. Another array of type String should be used to store the answers. An array of type int should be used for the point values. All three arrays should be declared to be of size 5. The index into the three arrays can be used to tie the question, answer, and point value together. For example, the item at index 0 for each array would correspond to question 1, answer 1, and the point value for question 1. Manually hardcode the five questions, answers, and point values in the constructor of TriviaGameV1. The five questions and their corresponding answers are shown on page 3. The point values should be set to 1, 2, 2, 3, 1, respectively.

The class should also provide the following two public methods:

• public boolean askNextQuestion() - This method takes no argument and returns a boolean. If there are no more questions to ask, it returns false. Otherwise, it asks the next question and gets an answer from the user. If the player’s answer matches the actual answer (case insensitive comparison), the player wins the number of points for that question. If the player’s answer is incorrect, the player wins no points for the question and the method will show the correct answer. It returns true after Q & A have been processed.
• public void showScore() - This method takes no argument and returns void. It displays the current score the player receives thus far.

The test driver is provided below, which creates a TriviaGameV1 object. After the player has answered all five questions, the game is over.

public class TriviaGameV1Test {
public static void main(String[] args) { TriviaGameV1 game = new TriviaGameV1();
while (game.askNextQuestion()) game.showScore(); System.out.println("Game over! Thanks for playing!"); }
}

User Brygom
by
4.7k points

1 Answer

3 votes

Answer:

Step-by-step explanation:

The following code is written in Java, It creates the class for the Trivia game along with the arrays, variables, and methods as requested so that it works flawlessly with the provided main method/test driver. The attached picture shows the output of the code.

import java.util.Arrays;

import java.util.Scanner;

class TriviaGameV1 {

String[] questions = {"The first Pokemon that Ash receives from Professor Oak is?", "Erling Kagge skiied into here alone on January 7, 1993", "1997 British band that produced 'Tub Thumper'", "Who is the tallest person on record (8 ft. 11 in) that has lived?", "PT Barnum said \"This way to the _______\" to attract people to the exit."};

String[] answers = {"pikachu", "south pole", "chumbawumba", "robert wadlow", "egress"};

int[] points = { 1, 2, 2, 3, 1};

int score;

int count = 0;

public void TriviaGameV1() {

this.score = 0;

}

public boolean askNextQuestion() {

if (count != 5) {

Scanner in = new Scanner(System.in);

System.out.println(questions[count]);

String answer = in.nextLine().toLowerCase();

if (answer.equals(answers[count])) {

score += points[count];

} else {

System.out.println("Wrong, the correct answer is : " + answers[count]);

}

count += 1;

return true;

}

return false;

}

public void showScore() {

System.out.println("Your Score is: " + this.score);

}

public static void main(String[] args) {

TriviaGameV1 game = new TriviaGameV1();

while (game.askNextQuestion()) {

game.showScore();

}

System.out.println("Game over! Thanks for playing!");

}

}

Create a class named TriviaGameV1 that plays a simple trivia game. The game should-example-1
User Slampen
by
4.5k points