Question

In: Computer Science

Create a Java class named Trivia that contains three instance variables, question of type String that...

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:

  1. getQuestion( ) – it will return the question.
  2. getAnswer( ) – it will return the answer.
  3. getPoints( ) – it will return the point
  4. setQuestion( String ) – sets the value of the question variable
  5. setAnswer( String ) – sets the value of the answer variable
  6. setPoints( int ) – sets the value of the points variable
  7. Constructor to initialize the instance variables.
  8. Overload Constructor to set the instance variables.
  9. Add any other methods you need such as equal( ), toString( ), display( ), calcPoints( )...

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.

Solutions

Expert Solution

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


Related Solutions

Define a class named Document that contains an instance variable of type String named text that...
Define a class named Document that contains an instance variable of type String named text that stores any textual content for the document. Create a method named toString that returns the text field and also include a method to set this value. Next, define a class for Email that is derived from Document and includes instance variables for the sender, recipient, and title of an email message. Implement appropriate set and get methods. The body of the email message should...
Create a Java class named Package that contains the following: Package should have three private instance...
Create a Java class named Package that contains the following: Package should have three private instance variables of type double named length, width, and height. Package should have one private instance variable of the type Scanner named input, initialized to System.in. No-args (explicit default) public constructor, which initializes all three double instance variables to 1.0.   Initial (parameterized) public constructor, which defines three parameters of type double, named length, width, and height, which are used to initialize the instance variables of...
with PHP Create a class called Employee that includes three instance variables—a first name (type String),...
with PHP Create a class called Employee that includes three instance variables—a first name (type String), a last name (type String) and a monthly salary int). Provide a constructor that initializes the three instance data member. Provide a set and a get method for each instance variable. If the monthly salary is not positive, do not set its 0. Write a test app named EmployeeTest that demonstrates class Employee’s capabilities. Create two Employee objects and display each object’s yearly salary....
CS 209 Data Structure 3. a. Create a class named Point3D that contains 3 instance variables...
CS 209 Data Structure 3. a. Create a class named Point3D that contains 3 instance variables x, y, and z. b. Create a constructor that sets the variables. Also, create get and set methods for each variable. c. Create a toString() method. d. Make Point3D implement Comparable. Also, create a compareTo(Point3D other) method that compares based on the x-coordinate, then y-coordinate for tiebreakers, then z-coordinate for tiebreakers. For example, (1, 2, 5) comes before (2, 1, 4), which comes before...
Define a class named Document that contains an instance variable of type String named text that stores any textual content for the document.
IN JavaDefine a class named Document that contains an instance variable of type String named text that stores any textual content for the document. Create a method named toString that returns the text field and also include a method to set this value. Next, define a class for Email that is derived from Document and includes instance variables for the sender, recipient, and title of an email message. Implement appropriate set and get methods. The body of the email message...
Define a class named Document that contains an instance variable of type String named text that stores any textual content for the document.
Code in Java Define a class named Document that contains an instance variable of type String named text that stores any textual content for the document. Create a method named toString that returns the text field and also include a method to set this value.Next, define a class for Email that is derived from Document and includes instance variables for the sender, recipient, and title of an email message. Implement appropriate set and get methods. The body of the email message...
Define a class named Document that contains an instance variable of type String named text that stores any textual content for the document.
IN JAVADefine a class named Document that contains an instance variable of type String named text that stores any textual content for the document. Create a method named toString that returns the text field and also include a method to set this value.Next, define a class for Email that is derived from Document and includes instance variables for the sender, recipient, and title of an email message. Implement appropriate set and get methods. The body of the email message should...
Create a class named Horse that contains the following data fields: name - of type String...
Create a class named Horse that contains the following data fields: name - of type String color - of type String birthYear - of type int Include get and set methods for these fields. Next, create a subclass named RaceHorse, which contains an additional field, races (of type int), that holds the number of races in which the horse has competed and additional methods to get and set the new field. ------------------------------------ DemoHorses.java public class DemoHorses {     public static void...
in Java, Create a class called EMPLOYEE that includes three instance variables – a first name...
in Java, Create a class called EMPLOYEE that includes three instance variables – a first name (type String), a last name (type String) and a monthly salary (double). Provide a constructor that initializes the three instance variables. Provide a set and a get method for each instance variable. If the monthly salary is not positive, do not set its value. Write a test app names EmployeeTest that demonstrates class EMLOYEE’s capabilities. Create two EMPLOYEE objects and display each object’s yearly...
(java) Write a class called CoinFlip. The class should have two instance variables: an int named...
(java) Write a class called CoinFlip. The class should have two instance variables: an int named coin and an object of the class Random called r. Coin will have a value of 0 or 1 (corresponding to heads or tails respectively). The constructor should take a single parameter, an int that indicates whether the coin is currently heads (0) or tails (1). There is no need to error check the input. The constructor should initialize the coin instance variable to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT