Question
In Java, TriviaGame In this programming challenge, you will create a simple trivia game for two players. The program will work like this: Starting with
In Java,
TriviaGame
In this programming challenge, you will create a simple trivia game for two players. The program will work like this:
- Starting with player 1, each player gets a turn at answering 5 trivia questions. are 10 questions, 5 for each player. (For this game there will be 10 questions in the file, 5 per player.) When a question is displayed, four possible answers are also displayed. Only one of the answers is correct, and if the player selects the correct answer, he or she earns a point.
- After the answers have been selected for all of the questions, the program displays the number of points earned by each player and declares the player with the highest number of points the winner.
You are to design a Question class to hold the data for a trivia question. The Question class
should have String fields for the following data:
- A trivia question
- Possible Answer 1
- Possible Answer 2
- Possible Answer 3
- Possible Answer 4
- The Number Of The Correct Answer (1, 2, 3, 4)
The Question class should have appropriate constructor(s), accessor, and mutator methods.
The program should create an ArrayList of 10 Question objects, one for each trivia question. Make up your own trivia questions on the subject or subjects of your choice for the objects.
Detailed Description
This solution has 3 classes to develop. Player, Question, and Trivia Game. There is also an input file that you may use as input for testing found under the Projects Solution/Project 3 tab. Remember to include necessary import statements.
Player class
There are 3 instance variables of type int: playerNumber, points and currentAnswer.
There is one constructor with an int for the playerNmber as the parameter.
In the constructor there is a call to setPlayerNumber with the playerNumber as the actual parameter.
In the constructor there is a call to setPoints with 0 as the actual parameter.
There are two mutator and two accessor methods to implement:
setPlayerNumber with an int parameter and a void return
setPoints with an int parameter and a void return
getPlayerNumber with no parameters and an int return
getPoints with no parameters and an int return
There is a chooseAnswer method with a reference to a Scanner as the parameters and a void return
This method requests the number of the correctAnswer
It reads in the value using the Scanner referenced in the parameter and sets the currentAnswer to that value
There is a getCurrentAnswer method that has no parameters and returns the int value of currentAnswer
There is an incrementPoints methods with no parameters and a void return
This method increments the points for this player by 1 when called
Question class
The Question class has the following instance parameters:
public final int NUM_ANSWERS = 4; // Constant for the number of questions
private String questionText; // The trivia question
private String [] possibleAnswers = new String[NUM_ANSWERS]; // An array to hold possible answers.
private int correctAnswerNumber;// The number (1, 2, 3, or 4) of the correct answer
There is a single Question Constructor which is the default constructor
Calls setQuestionText with the empty String as the actual parameter value
Calls setCorrectAnswerNumber with an int 0 as the actual parameter value
For each possible answer in the array possibleAnswers call setPossibleAnswer with the empty String and the number of the answer (from 1 to 4) as the actual parameter values
There are two mutator and two accessor methods to implement:
setQuestionText with a String input parameter and return a void return
setCorrectAnswerNumber with an int parameter and a void return
getQuestionText with no parameters and a String return
getCorrectAnswerNumber with no parameters and an int return
There is a setPossibleAnswer method that accepts a String and an int as parameters with a void return
This method set the value of the possibleAnswers array for an index of one less than the int input parameter to the value of the String input parameter
There is a getPossibleAnswer method that accepts an int as parameters with a String return
This method get the value for the possibleAnswers array at an index of one less than the int input parameter and returns that value
There is a getCorrectAnswer method that accepts an int as parameters with a String return
This method get the value for the possibleAnswers array at an index of one less than the int input parameter and returns that value
TriviaGame class
This is the driver class. It includes the main method, the initQuestions method, the displayQuestion method, and showGameResults method. These are described below. This is a little more complex than a typical driver class, but this is done to minimize the number of classes in this exercise.
There is one static class variable keyboard, a Scanner class reference variable for a Scanner that reads from the keyboard.
main method (must throw an IOException since it calls a method that does so)
Has a the following constants and variables:
final int NUM_PLAYERS = 2;
int numQuestions = 0;
int playerTurn = 1; // The current player
int questionNum; // The current question number
int playerAnswer; // The player's chosen answer
int player1points = 0; // Player 1's points
int player2points = 0; // Player 2's points
An array of type Player called players with a length of 2
An ArrayList of type Question called questions (You can create the ArrayList here)
For each of the two Players
Create a Player object with the Player number and place a reference to it in players so that each player is referenced from the entry with an index one less than the player number.
Call initQuestions with a reference to the ArrayList called questions
//Now we play the game
For each question in the questions ArrayList
Call displayQuestion with the next question in the list and with the number of playerTurn (will be 1 or 2 here)
For the current player in player (index is playerTurn -1) call the players chooseAnswer method
If (the players currentAnswer == the questions correct answer number)//you need to figure which methods to call
Print out Correct
Increment the players points (using incrementPoints)
Else
Print Sorry, that is incorrect. The correct answer is: correctanswernumber
Switch between players (if playerTurn =1 next round playerTurn =2 , if playerTurn =2 next round is playerTurn =1
End the loop
Call showGameResults with a reference to the players array.
initQuestions method throws an IOException
This method accepts an ArrayList of type Question (call it questAL) with a void return
Request the name of the text file and create a Scanner to read from the file called inputReader (If the file doesnt exist have a soft exit) (Use my trivia.txt file which is correctly formatted
Create a temporary variable tempQuestion
While the inputReader has another line
Create tempQuestion a new Question
Read in the next line from the input file using inputReader and call setQuestionText for the Question with the question you have read in
For i from 1 to 4 inclusive
Read in the nextLine for a possible answer as a String and call setPossibleAnswer for tempQuestion with the String just read in and the value of i as the two parameters
Read in the next line as a String, parse out the integer value and call setCorrectAnswerNumber for tempQuestion with that integer value as the actual parameter value.
Add the tempQuest reference to the questAL ArrayList
End while loop
displayQuestion method
This method accepts a reference to a Question and an int for a playerNum and has a void return
Display the playerNum and the questions as shown in the sample output
showGameResults method
This method accepts a reference to an array of type Player and has a void return
It prints out the results as shown in the sample output.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started