In: Computer Science
Create a Java class named Trivia that contains three instance variables, question of type String that stores the question of the trivia, answer of type String that stores the answer to the question, and points of type integer that stores the points’ value between 1 and 3 based on the difficulty of the question.
Also create the following methods:
Create Java application that contains a main method that plays a simple Trivia game. The game should have 5 questions. Each question has a corresponding answer and point value between 1 and 3. Implement the game using an array of 5 Trivia objects.
Next, open a binary file and store those 5 Trivia objects into a binary file then close the file. Open the file again to read each question one at a time and store them into an array of objects.
Randomly, display a question and allow the player to enter an answer.
If the player’s answer matches the actual answer, 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. Make sure the answer is not case sensitive and it is only one word or two words at most. The program should show the correct answer if the player is incorrect. After the player has answered all five questions, the game is over and your program should display the player’s total score.
You need to turn in the code, a sample output of those 5 questions, the player's answers and the total.
Make sure it is readable, formatted and commented.
import java.awt.List;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;
import javax.sound.midi.Soundbank;
public class Solution {
public static void main(String[] args) {
Trivia[] question_fileTrivias = new
Trivia[5];
//Id of the question is same as the
question
question_fileTrivias[0] = new
Trivia("Question No 1",2,"Question No 1","Answer 1");
question_fileTrivias[1] = new
Trivia("Question No 2",3,"Question No 2","Answer 2");
question_fileTrivias[2] = new
Trivia("Question No 3",1,"Question No 3","Answer 3");
question_fileTrivias[3] = new
Trivia("Question No 4",2,"Question No 4","Answer 4");
question_fileTrivias[4] = new
Trivia("Question No 5",3,"Question No 5","Answer 5");
Scanner scanner = new
Scanner(System.in);
HashMap<String,Integer>
question_refeMap= new HashMap<>();
for(int i=0;i<5;i++)
{
question_refeMap.put(question_fileTrivias[i].getQuestion(),
i);
}
ArrayList<User> list = new
ArrayList<>();
for(int
i=0;i<question_fileTrivias.length;i++)
{
list.add(new
User(question_fileTrivias[i].getId(),question_fileTrivias[i].getQuestion()));
}
java.util.Collections.shuffle(list);
User[] user_answer = new
User[5];
int i=0;
int points=0;
for(User question: list)
{
System.out.println(question.getId());
String anString
= scanner.nextLine();
int idx =
question_refeMap.get(question.getId());
if(anString.toLowerCase().equals(question_fileTrivias[idx].getAnswer().toLowerCase()))
{
points= points+
question_fileTrivias[idx].getVal();
}
user_answer[i++]
= new User(question.getId(),anString);
}
System.out.println(points);
for(i=0;i<5;i++)
{
int idx =
question_refeMap.get(user_answer[i].getId());
System.out.println((i+1)+".:
"+question_fileTrivias[idx].getQuestion()+"\n");
System.out.println("User Answer: "+
user_answer[i].getAnswer());
System.out.println("Correct Answer:
"+question_fileTrivias[idx].getAnswer());
String anString
= user_answer[i].getAnswer().toLowerCase();
if(anString.equals(question_fileTrivias[idx].getAnswer().toLowerCase())){
System.out.println("Correct\n");
}
}
}
}
Hope this helps!!.Feel free to ask,I will be glad to resolve the
issue