Class Quiz

java.lang.Object
Quiz

public class Quiz
extends java.lang.Object
The Quiz class allows the user to answer questions from a predefined question bank. The questions are displayed in a random order and a score is calculated at the end for how accurately the user answered the questions.

The questions are loaded from a CSV file that contains the following fields:

  1. Question type: defines the type of question; valid values are: FR, MC, or TF
  2. Prompt: the main text of the question
  3. Answer: the answer to the question
  4. Choices: (optional) a multiple choice (MC) question will have additional fields, one for each choice

Here is an example of a valid CSV question bank:

 FR,What is the capital of Massachusetts?,Boston
 TF,Water freezes at 32 degrees Fahrenheit,true
 MC,Which of the following is a primitive Java type?,C,String,Scanner,int,Rectangle
 
  • Field Summary

    Fields
    Modifier and Type Field Description
    static java.lang.String FREE_RESPONSE_QUESTION_TYPE
    Question type for free response (i.e., "standard") questions.
    static java.lang.String MULTIPLE_CHOICE_QUESTION_TYPE
    Question type for multiple choice questions.
    static java.lang.String TRUE_FALSE_QUESTION_TYPE
    Question type for true/false questions.
  • Constructor Summary

    Constructors
    Constructor Description
    Quiz​(java.lang.String resourceName)
    Constructs a Quiz with questions loaded from the given CSV file.
  • Method Summary

    Modifier and Type Method Description
    static void main​(java.lang.String[] args)
    Creates a Quiz object from a local CSV resource named questions.csv and runs the quiz.
    void run()
    Runs the quiz by asking each question and checking whether the user's response is correct.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • FREE_RESPONSE_QUESTION_TYPE

      public static final java.lang.String FREE_RESPONSE_QUESTION_TYPE
      Question type for free response (i.e., "standard") questions.
      See Also:
      Constant Field Values
    • MULTIPLE_CHOICE_QUESTION_TYPE

      public static final java.lang.String MULTIPLE_CHOICE_QUESTION_TYPE
      Question type for multiple choice questions.
      See Also:
      Constant Field Values
    • TRUE_FALSE_QUESTION_TYPE

      public static final java.lang.String TRUE_FALSE_QUESTION_TYPE
      Question type for true/false questions.
      See Also:
      Constant Field Values
  • Constructor Details

    • Quiz

      public Quiz​(java.lang.String resourceName)
      Constructs a Quiz with questions loaded from the given CSV file.
      Parameters:
      resourceName - the name of the CSV file that holds the questions for this quiz, this file must be located in the same package as this class
  • Method Details

    • main

      public static void main​(java.lang.String[] args)
      Creates a Quiz object from a local CSV resource named questions.csv and runs the quiz.
      Parameters:
      args - not used by this method
    • run

      public void run()
      Runs the quiz by asking each question and checking whether the user's response is correct. The order of the questions will be shuffled every time the quiz is run.