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.
Coding :
Trivia.java
import java.io.Serializable;
import java.util.Scanner;
public class Trivia implements Serializable {
private String question;
private String answer;
private int Points;
public Trivia(){
Scanner sc = new Scanner(System.in);
System.out.println("Enter the Question : ");
String q = sc.nextLine();
setQuestion(q);
System.out.println("Enter the Answer : ");
String ans = sc.nextLine();
setAnswer(ans);
System.out.println("Enter the Points (1 to 3) : ");
int p = sc.nextInt();
setPoints(p);
}
public Trivia(String q, String a, int p){
setQuestion(q);
setAnswer(a);
setPoints(p);
}
public String getQuestion() {
return question.trim();
}
public String getAnswer() {
return answer.toLowerCase();
}
public int getPoints() {
return Points;
}
public void setQuestion(String question) {
this.question = question.trim();
}
public void setAnswer(String answer) {
this.answer = answer.trim();
}
public void setPoints(int points) {
Points = points;
}
private boolean equal(String ans){
if(ans==getAnswer())
return true;
else return false;
}
public void display(String ans){
if (equal(ans)==true)
System.out.println("\nThe answer is correct");
else {
System.out.println("\nThe answer is incorrect");
System.out.println("\nCorrect Answer : " + answer);
}
}
public int calcPoints(int t,String ans){
if(equal(ans)==true)
return t + Points;
else return t;
}
}
Main.java
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
Trivia T [] = new Trivia[5];
Scanner sc = new Scanner(System.in);
System.out.println("Enter the Question : ");
String q = sc.nextLine();
System.out.println("\nEnter the Answer : ");
String ans = sc.nextLine();
System.out.println("\nEnter the Points (1 to 3) : ");
int p = sc.nextInt();
Trivia t = new Trivia(q,ans,p);
T[0] = t;
int j = 1;
while (j<5) {
T[j] = new Trivia();
j++;
}
File f = new File("sample.dat");
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(f));
out.writeObject(T);
out.close();
ObjectInputStream in = new ObjectInputStream(new FileInputStream(f));
Trivia Q [] = (Trivia[]) in.readObject();
List<Trivia> triviaList = Arrays.asList(Q);
Collections.shuffle(triviaList);
triviaList.toArray(Q);
int total = 0;
for (int i = 1; i <= 5; i++) {
System.out.println("\nQuestion " + i + " : " + Q[i-1].getQuestion());
System.out.println("\nType Your Answer : ");
String answer = sc.next();
Q[i].display(ans);
total = Q[i].calcPoints(total,ans);
}
System.out.println("Your Points are : " + total);
in.close();
}
}
Explanation :
Trivia.java :
Main.java:
out.writeObject(T);
in.writeObject(T);
Output :
"D:\Softwares\IntelliJ IDEA\IntelliJ IDEA Community Edition 2020.2.1\jbr\bin\java.exe" "-javaagent:D:\Softwares\IntelliJ IDEA\IntelliJ IDEA Community Edition 2020.2.1\lib\idea_rt.jar=63298:D:\Softwares\IntelliJ IDEA\IntelliJ IDEA Community Edition 2020.2.1\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\niran\IdeaProjects\Animal\out\production\Animal Main
Enter the Question :
Capital of India
Enter the Answer :
delhi
Enter the Points (1 to 3) :
1
Enter the Question :
capital of Pakistan
Enter the Answer :
karachi
Enter the Points (1 to 3) :
2
Enter the Question :
'1' is a
Enter the Answer :
int
Enter the Points (1 to 3) :
2
Enter the Question :
'a' is a
Enter the Answer :
char
Enter the Points (1 to 3) :
3
Enter the Question :
'2.0'is a
Enter the Answer :
float
Enter the Points (1 to 3) :
1
Question 1 : Capital of India
Type Your Answer :
delhi
The answer is correct
Question 1 : capital of Pakistan
Type Your Answer :
karachi
The answer is correct
Question 2 : 'a' is a
Type Your Answer :
int
The answer is incorrect
Correct Answer : char
Question 3 : '2.0'is a
Type Your Answer :
float
The answer is correct
Question 4 : '1' is a
Type Your Answer :
int
The answer is correct
Your Points are : 6
Process finished with exit code 0