Question

java In this project you will implement a trivia game. It will ask random trivia questions,...

java

In this project you will implement a trivia game. It will ask random

trivia questions, evaluate their answers and keep score. The project

will also have an administrative module that will allow for managing

the question bank. Question bank management will include adding

new questions, deleting questions and displaying all of the questions,

answers and point values.

2. The project can be a GUI or a menu based command line program.

3. Project details

1. Create a class to administer the question bank with the following

functionality:

1. Stores question data in a text File (See details of file

structure below)

2. Enter new questions including answer and question point

value. Point value should be 1 to 5.

3. Delete questions by searching for a question in the text

file. Assign a question id number to each question using a

static variable.

4. Display all of the questions, associated answers and point

value.

5. The question bank must be stored in a text file with

structure of:

1. Questions ID Number

2. Question

3. Answer

4. Question point value.

5. see questions.dat for a sample.

6. For the purposes of this project the question bank will

contain at least 15 questions.

7. This class may be a separately run or part of the program

that runs the trivia game.

2. Create a class for the players information with the following

functionality

1. Player’s real name

2. Player’s gamer nick name

3. Current total score. Total score is a summation of all the

Player’s game sessions.

3. Create a class for questions including

1. question, answer, value and a question ID

2. Accessor methods to return all instance fields.

4. Create a class for the trivia game with the following functionality.

1. Stores a collection of Question objects.

2. Using random number generation to read 5 questions from

the question bank and stores them in the collection.

Remember the questions are stored in three rows,

questions, answer and score.

3. Returns the next question object in the collection.

4. Evaluate the answer and returns the outcome. The

evaluation should ignore case.

5. Create a class to run the game

1. GUI or menu based command line.

2. Create a new user object and ask for required user

information (name, nick name)

3. Create trivia game object

4. Start the game, the game sequence is

1. Display a question

2. Prompt for an answer

3. Evaluate the answer

4. Display the evaluation results

5. Update the users total score

6. Display the user current game score.

7. Continue to display new questions until current game

is over. (5 questions)

8. Once game is over, allow user to play again or quit

9. If user quit - Display his nick name, current game

score and total score.

6. Programmer Design information

1. As you may have noticed much of the details for this

project are left up to you.

2. You may create any additional classes, methods (especially

private utility methods) or instance fields you think may be

needed, however you will be graded on good object

oriented design, so points will be taken off for unnecessary

classes, methods or unused instance fields or instance

fields that should be local variables.

3. There is no need to add any additional functionality such

as checking for duplicate questions, or duplicate user files

etc, just focus on the required functionality.

4. Create 2 UML Class Diagram for this project.

1. Design Version - completed prior to coding the project to assist

with coding.

2. Final Version - completed after the coding that accurately

represents the final version of the code.

3. All instance variables, including type.

4. All methods, including parameter list, return type and access

specifier (+, -);

5. No need to include the class with main method in the UML

diagrams. Refer to the UML Distilled pdf on the content page as

a reference for creating class diagrams

question.dat

01

What is light as a feather, but even the strongest man cannot hold it more

than a few minutes?

His breath

3

02

What goes up and down, but still remains in the same place?

stairs

4

03

It's pouring cats and dogs outside and there's a man walking down the

street. Neither does he have an umbrella nor does he have a hat to cover

his head. Then why isn't his hair getting wet?

He is bald

2

04

How close of a relative would the sister-in-law of your dad's only brother

be to you?

Your mom

1

05

The State with the highest percentage of people who walk to work:

Alaska

5

06

Half of all Americans live within 50 miles of what?

Their birthplace

4

07

What is Scooby short for in the name 'Scooby Doo'?

Scoobert

3

08

The sentence "May I have a large container of coffee?" is used as a memory

aid for what?

Pi

5

09

Any fool can write code that a computer can understand. Good programmers

write code that who can understand?

Humans

1

10

You cannot teach beginners top-down programming, because?

they don't know which end is up

5

0 0
Add a comment Improve this question Transcribed image text
Answer #1

PlayTriviaGame.java


import java.util.Scanner;
import java.io.FileInputStream;
import java.io.FileNotFoundException;


public class PlayTriviaGame{
    private static String userInput(){
      Scanner userInput = new Scanner(System.in);
      String temp = "";
      temp = userInput.nextLine();
      return temp;
   }

   public static void main(String[] args){
    
      //All objects and variables needed for the program
      FileInputStream file = null;
      TriviaGame gameOne = null;
      Scanner userAnswer = new Scanner(System.in);
      String answer = "Y";
      boolean testUser = false;
      int numberEntry = 0;
      int startNumber = 0;
    
      //Test for the file that we are using
      try{
        file = new FileInputStream("questions.dat");
        gameOne = new TriviaGame(file);
      }
      catch (FileNotFoundException e){
         System.out.println("File not found.");
         System.exit(0);
      }
    
    
     //Ask the user for the amount of questions they want
      System.out.print("Enter the amount of questions you would like (10 or less): ");
      numberEntry = userAnswer.nextInt();
      // Sees if the amount of questions is less than 10
      while(numberEntry > 10){
         System.out.print("Enter the amount of questions you would like (10 or less): ");
         numberEntry = userAnswer.nextInt();
      }
       
      //Test for "Y" or "y" to continue with a new game
      while( answer.equalsIgnoreCase("Y")){
         testUser = false;
         gameOne.play(numberEntry);
       
         startNumber = 0;
      
         //Goes from the 0 to the amount of questions the player wanted
         while(startNumber < numberEntry){
            System.out.println(gameOne.nextQuestion());
            answer = userInput(); // RIGHT HERE LINE ENTER
            //As I said I tried a direct input but it would error for some odd reason
            if(gameOne.evaluateAnswer(answer) == true){
               System.out.println(answer + " is correct");
            }
            else{
               System.out.println(answer + " is incorrect");
            }
          
            startNumber++;
         }
       
         System.out.println("Your score this round is: " + gameOne.getScore());
          

    
       
       
         //Ask if the user wants to draw another fence. If so it checks if the input is valid "Y/y || N/n"
         System.out.print("Would you like to play again? (Y/N): ");
         while(testUser != true){
            answer = userAnswer.next();
          
            if(answer.equalsIgnoreCase("N") || answer.equalsIgnoreCase("Y")){
               testUser = true;
            }
            else{
               System.out.print("Please enter a valid response: ");
               testUser = false;
            }
         }
      }
      System.out.println("---------------------------------------------");
      System.out.println("--Thanks for playing, the answers are below--");
      System.out.println("---------------------------------------------");
      System.out.println(gameOne);  
   }
}
    
    
    
TriviaGame.java

import java.util.Scanner;
import java.io.FileInputStream;

public class TriviaGame{

   private Question[] gameQuestions;
   private int score;
   private int numberOfQuestions;
   private int currentQuestion;


//Constructor taking in a file input that contains the questions
   public TriviaGame(FileInputStream file){
      Scanner fileIn = new Scanner(file);
      gameQuestions = new Question[10];
    
      int i = 0;
      while(fileIn.hasNextLine()){
         gameQuestions[i] = new Question();
         gameQuestions[i].setQuestion(fileIn.nextLine());
         if(fileIn.hasNextLine() == true){
            gameQuestions[i].setAnswer(fileIn.nextLine());
            if(fileIn.hasNextLine() == true){
               gameQuestions[i].setValue(fileIn.nextInt());
               if(fileIn.hasNextLine() == true){
                  fileIn.nextLine();
               }
            }
         }
         i++;
      }
      currentQuestion = 0;
   }

   //Accessor methods for numberOfQuestions, currentQuestion, and score
   public int getNumberOfQuestions(){
      return numberOfQuestions;
   }

   public int getCurrentQuestion(){
      return currentQuestion;
   }

   public int getScore(){
      return score;
   }

   //Resets the game for a fresh game.
   public void play(int numQuestion){
      score = 0;
      currentQuestion = 0;
      numberOfQuestions = numQuestion;
   }

   //Returns the nextQuestion for the player
   public String nextQuestion(){
      return gameQuestions[currentQuestion].getQuestion();
   }

   //Test if the answer is correct or incorrect (MUST BE EXACTLY THE SAME)
   public boolean evaluateAnswer(String answer){
      if( answer.equals(gameQuestions[currentQuestion].getAnswer()) == true){
         score += gameQuestions[currentQuestion].getValue();
         currentQuestion++;
         return true;
      }
      else{
         currentQuestion++;
         return false;
      }
   }

   //Formats all the questions the player tried to answer with answers
   public String toString(){
      String s = "";
      for(int i = 0; i < numberOfQuestions; i++){
         s += gameQuestions[i].toString();
      }
    
      return s;
   }  
}

Question.java


public class Question{

   private static final int INVALIDINPUT = -1;
   private String question;
   private String answer;
   private int value;

   //Constructors Default and para
   public Question(){
      question = "";
      answer = "";
      value = 0;
   }

   public Question(String question, String answer, int value){
      this.question = question;
      this.answer = answer;
      setValue(value);
   }


   //Accesor methods for each of the instance variables
   public String getQuestion(){
      return question;
   }

   public String getAnswer(){
      return answer;
   }

   public int getValue(){
      return value;
   }

   //Mutator methods for each of the instance variables
   public void setQuestion(String question){
      this.question = question;
   }

   public void setAnswer(String answer){
      this.answer = answer;
   }

   public void setValue(int value){
      if(value >= 1 && value <= 5){
         this.value = value;
      }
      else{
         this.value = INVALIDINPUT;
         System.out.println("Error: Value must be between [1,5]");
      }
   }

   //Prints a formatted verison for all the variables
   public String toString(){
      String s = "Q: " + getQuestion() + " \nA: " + getAnswer() + " \n" + getValue() + "\n";
      return s;
   }
}

questions.dat

What is light as a feather, but even the strongest man cannot hold it more than a few minutes?
His breath
3
What goes up and down, but still remains in the same place?
stairs
4
It's pouring cats and dogs outside and there's a man walking down the street. Neither does he have an umbrella nor does he have a hat to cover his head. Then why isn't his hair getting wet?
He is bald
2
How close of a relative would the sister-in-law of your dad's only brother be to you?
Your mom
1
The State with the highest percentage of people who walk to work:
Alaska
5
Half of all Americans live within 50 miles of what?
Their birthplace
4
What is Scooby short for in the name 'Scooby Doo'?
Scoobert
3
The sentence "May I have a large container of coffee?" is used as a memory aid for what?
Pi
5
Any fool can write code that a computer can understand. Good programmers write code that who can understand?
Humans
1
You cannot teach beginners top-down programming, because?
they don't know which end is up
5

Add a comment
Know the answer?
Add Answer to:
java In this project you will implement a trivia game. It will ask random trivia questions,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • java In this project you will implement a trivia game. It will ask random trivia questions, evaluate their answers and keep score. The project will also have an administrative module that will allow f...

    java In this project you will implement a trivia game. It will ask random trivia questions, evaluate their answers and keep score. The project will also have an administrative module that will allow for managing the question bank. Question bank management will include adding new questions, deleting questions and displaying all of the questions, answers and point values. 2. The project can be a GUI or a menu based command line program. 3. Project details 1. Create a class to...

  • You will need to first create an object class encapsulating a Trivia Game which INHERITS from...

    You will need to first create an object class encapsulating a Trivia Game which INHERITS from Game. Game is the parent class with the following attributes: description - which is a string write the constructor, accessor, mutator and toString methods. Trivia is the subclass of Game with the additional attributes: 1. trivia game id - integer 2. ultimate prize money - double 3. number of questions that must be answered to win - integer. 4. write the accessor, mutator, constructor,...

  • In 6A, you created an object class encapsulating a Trivia Game which INHERITS from Game. Now...

    In 6A, you created an object class encapsulating a Trivia Game which INHERITS from Game. Now that you have successfully created Trivia objects, you will continue 6B by creating a linked list of trivia objects. Add the linked list code to the Trivia class. Your linked list code should include the following: a TriviaNode class with the attributes: 1. trivia game - Trivia object 2. next- TriviaNode 3. write the constructor, accessor, mutator and toString methods. A TriviaLinkedList Class which...

  • In this assignment, you will implement Address and Residence classes. Create a new java project. Part...

    In this assignment, you will implement Address and Residence classes. Create a new java project. Part A Implementation details of Address class: Add and implement a class named Address according to specifications in the UML class diagram. Data fields: street, city, province and zipCode. Constructors: A no-arg constructor that creates a default Address. A constructor that creates an address with the specified street, city, state, and zipCode Getters and setters for all the class fields. toString() to print out all...

  • JAVA We will write a psychic game program in which a player guesses a number randomly...

    JAVA We will write a psychic game program in which a player guesses a number randomly generated by a computer. For this project, we need 3 classes, Player, PsychicGame, and Driver. Description for each class is as following: Project Folder Name: LB05 1. Player class a.Input - nickName : nick name of the player - guessedNumber : a number picked for the player will be assigned randomly by a computer later. b. Process - constructor: asks a user player’s nickName...

  • In this programming exercise, you will create a simple trivia game for two players. The program...

    In this programming exercise, you will create a simple trivia game for two players. The program will work like this: Starting out with player 1, each player gets a turn at answering 5 trivia questions. (There should be a total of 10 questions.) When a question is displayed, 4 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 answers have been selected...

  • In JAVA, please In this module, you will combine your knowledge of class objects, inheritance and...

    In JAVA, please In this module, you will combine your knowledge of class objects, inheritance and linked lists. You will need to first create an object class encapsulating a Trivia Game which INHERITS from Game. Game is the parent class with the following attributes: 1. description - which is a string 2. write the constructor, accessor, mutator and toString methods. Trivia is the subclass of Game with the additional attributes: 1. trivia game id - integer 2. ultimate prize money...

  • JAVA program. I have created a game called GuessFive that generates a 5-digit random number, with...

    JAVA program. I have created a game called GuessFive that generates a 5-digit random number, with individual digits from 0 to 9 inclusive. (i.e. 12345, 09382, 33044, etc.) The player then tries to guess the number. With each guess the program displays two numbers, the first is the number of correct digits that are in the proper position and the second number is the sum of the correct digits. When the user enters the correct five-digit number the program returns...

  • You will write a two-class Java program that implements the Game of 21. This is a...

    You will write a two-class Java program that implements the Game of 21. This is a fairly simple game where a player plays against a “dealer”. The player will receive two and optionally three numbers. Each number is randomly generated in the range 1 to 11 inclusive (in notation: [1,11]). The player’s score is the sum of these numbers. The dealer will receive two random numbers, also in [1,11]. The player wins if its score is greater than the dealer’s...

  • Tic Tac Toe Game: Help, please. Design and implement a console based Tic Tac Toe game....

    Tic Tac Toe Game: Help, please. Design and implement a console based Tic Tac Toe game. The objective of this project is to demonstrate your understanding of various programming concepts including Object Oriented Programming (OOP) and design. Tic Tac Toe is a two player game. In your implementation one opponent will be a human player and the other a computer player. ? The game is played on a 3 x 3 game board. ? The first player is known as...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT