Write a program that administers and grades quizzes. A quiz consists of questions. There are four types of questions: text questions, number questions, choice questions with a single answer, and choice questions with multiple answers. When grading a text question, ignore leading or trailing spaces and letter case. When grading a numeric question, accept a response that is approximately the same as the answer.

A quiz is specified in a text file. Each question starts with a letter indicating the question type (T, N, S, M), followed by a line containing the question text. The next line of a non-choice question contains the answer. Choice questions have a list of choices that is terminated by a blank line. Each choice starts with + (correct) or (incorrect). Here is a sample file:

T
Which Java reserved word is used to declare a subclass?
extends
S
What is the original name of the Java language?
− *7
− C−−
+ Oak
− Gosling
M
Which of the following types are supertypes of Rectangle?
− PrintStream
+ Shape
+ RectangularShape
+ Object
− String
N
What is the square root of 2?
1.41421356

Your program should read in a quiz file, prompt the user for responses to all questions, and grade the responses. Follow the design process that was described in this chapter.

Complete the following files:

ChoiceQuestion.java

MultiChoiceQuestion.java

NumberQuestion.java

Question.java

Quiz.java

SingleChoiceQuestion.java

TextQuestion.java

Use the following file:

QuizRunner.java

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

public class QuizRunner
{
   public static void main(String[] args) throws IOException
   {
      Quiz q = new Quiz();
      Scanner in = new Scanner(new File("quiz.txt"));
      q.read(in);
      in.close();

      in = new Scanner(System.in);

      ArrayList<Question> questions = q.getQuestions();
      ArrayList<String> answers = new ArrayList<String>();

      for (Question qu : questions)
      {
         System.out.println(qu.getText());
         String answer = in.nextLine();
         answers.add(answer);
      }

      boolean[] results = q.checkAnswers(answers);
      
      for (int i = 0; i < results.length; i++)
      {
         System.out.println();
         System.out.println(questions.get(i).getText());
         System.out.println("Correct answer: " + questions.get(i).getAnswer());
         System.out.println("Your answer: " + answers.get(i));
         System.out.print("Your answer was ");
         if (!results[i])
         {
            System.out.print("not ");
         }
         System.out.println("correct.");
      }     
   }
}