Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA Assignment Quiz Grader Write a program that administers and grades quizzes.

ID: 3669129 • Letter: J

Question

JAVA Assignment

Quiz Grader

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 .txt file:

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.
Here is a sample program run:

Use the following class as your main class:

Explanation / Answer

Main.java

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

public class Main{
    public static void main(String[] args) throws IOException {
        Quiz q = new Quiz();
        Scanner in = new Scanner(new FileReader("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).getStringAnswer());
            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.");
        }
    }
}

Quiz.java

import java.util.ArrayList;
import java.util.Scanner;

public class Quiz {
    private ArrayList<Question> questions = new ArrayList<Question>();

    /**
     * Parses the input text file and stores questions/answers/options into correct variables.<br>
     *
     * @param in A scanner input that reads from a file.<br>
     */
    public void read(Scanner in) {
        while (in.hasNext()) {
            String line = in.nextLine();
            if (line.equals("T")) {
                String txt = in.nextLine();
                String ans = in.nextLine();
                TextQuestion textQuestion = new TextQuestion(txt, ans);
                questions.add(textQuestion);
            } else if (line.equals("S") || line.equals("M")) {
                String txt = in.nextLine();
                MultiChoiceQuestion multiChoiceQuestion = new MultiChoiceQuestion(txt);
                String check = in.nextLine();
                while (!check.equals("")) {
                    if (check.substring(0, 1).equalsIgnoreCase("+")) {
                        multiChoiceQuestion.addOption(check.substring(1).trim(), true);
                        System.out.println("Right: " + check.substring(1).trim());
                        check = in.nextLine();
                    } else if (check.substring(0, 1).equalsIgnoreCase("-")) {
                        multiChoiceQuestion.addOption(check.substring(1).trim(), false);
                        System.out.println("Wrong: " + check.substring(1).trim());
                        check = in.nextLine();
                    }
                }
                questions.add(multiChoiceQuestion);
            } else if (line.equals("N")) {
                String txt = in.nextLine();
                String answer = in.nextLine();
                float floatAnswer = Float.parseFloat(answer);
                answer = Util.cleanNumberString(answer);
                NumberQuestion numberQuestion = new NumberQuestion(txt, answer, floatAnswer);
                questions.add(numberQuestion);
            }
        }
    }

    public ArrayList<Question> getQuestions() {
        return questions;
    }

    /**
     * Checks users answers against the ArrayList of correct answers.<br> */
    public boolean[] checkAnswers(ArrayList<String> answers) {
        boolean[] isCorrect = new boolean[questions.size()];
        for (int x = 0; x < answers.size(); x++) {
            Question currentQuestion = questions.get(x);
            if (currentQuestion instanceof TextQuestion || currentQuestion instanceof NumberQuestion) {
                if (currentQuestion.getStringAnswer().equalsIgnoreCase(answers.get(x).trim())) {
                    isCorrect[x] = true;
                    continue;
                } else {
                    isCorrect[x] = false;
                    continue;
                }
            } else if (currentQuestion instanceof MultiChoiceQuestion) {
                String sortedAnswer = Util.cleanAndSortAnswer(answers.get(x));
                if (currentQuestion.getStringAnswer().equalsIgnoreCase(sortedAnswer)) {
                    isCorrect[x] = true;
                    continue;
                } else {
                    isCorrect[x] = false;
                }
            }
     }
        return isCorrect;
    }
}

Util.java
import java.util.Arrays;
public class Util {
    /**
     * Method to get a letter from an integer with 1 being associated with A, and 2 being associated with B etc.<br>*/
    public static String getLetterFromInt(int i) {
        return Character.toString((char) (i + 64));
    }

    /**
     * Sorts a string in alphabetical order and cleans the string.<br>*/
    public static String cleanAndSortAnswer(String unsortedAnswer) {
        unsortedAnswer
                .replace(" ", "")
                .replace(".", "")
                .replace(",", "")
                .replace("-", "");
        unsortedAnswer.toUpperCase();
        char[] charArray = unsortedAnswer.toCharArray();
        Arrays.sort(charArray);
        return new String(charArray);
    }

    /**
     * Removes any additional characters, that could potentially cause problems, from a string number.*/
    public static String cleanNumberString(String numberString) {
        if (numberString.contains(",")) {
            numberString = numberString.replace(",", "");
        }
        if (numberString.contains(" ")) {
            numberString = numberString.replace(" ", "");
        }

        return numberString;
    }
}
MultiChoiceQuestion.java
import java.util.Arrays;

public class Util {
    /**
     * Method to get a letter from an integer with 1 being associated with A, and 2 being associated with B etc.<br> */
    public static String getLetterFromInt(int i) {
        return Character.toString((char) (i + 64));
    }

    /**
     * Sorts a string in alphabetical order and cleans the string.<br>*/
    public static String cleanAndSortAnswer(String unsortedAnswer) {
        unsortedAnswer
                .replace(" ", "")
                .replace(".", "")
                .replace(",", "")
                .replace("-", "");
        unsortedAnswer.toUpperCase();
        char[] charArray = unsortedAnswer.toCharArray();
        Arrays.sort(charArray);
        return new String(charArray);
    }

    /**
     * Removes any additional characters, that could potentially cause problems, from a string number. */
    public static String cleanNumberString(String numberString) {
        if (numberString.contains(",")) {
            numberString = numberString.replace(",", "");
        }
        if (numberString.contains(" ")) {
            numberString = numberString.replace(" ", "");
        }

        return numberString;
    }
}

MultiChoiceQuestionOption.java

public class MultiChoiceQuestionOption {
    private boolean isCorrect;
    private String optionText;

    public MultiChoiceQuestionOption(String optionText, boolean isCorrect) {
        this.optionText = optionText;
        this.isCorrect = isCorrect;
    }

    public boolean isCorrectOption() {
        return isCorrect;
    }

    public String getOptionText() {
        return optionText;
    }
}

NumberQuestion.java

public class NumberQuestion extends Question {
    protected float floatAnswer;

    public NumberQuestion(String txt, String stringAnswer, float floatAnswer) {
        this.text = txt;
        this.stringAnswer = stringAnswer;
        //this.floatAnswer = floatAnswer;
    }
}

Question.java

public class Question {
    protected String text;
    protected String stringAnswer;

    public String getText() {
        return text;
    }

    public String getStringAnswer() {
        return stringAnswer;
    }
}


TextQuestion.java
public class TextQuestion extends Question {
    public TextQuestion(String text, String answer) {
        this.text = text;
        this.stringAnswer = answer;
    }
}

Quiz.txt
T
Which Java keyword is used to define 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.41

output
                                                                                                                   
Wrong: *7                                                                                                                                                    
Wrong: C--                                                                                                                                                   
Right: Oak                                                                                                                                                   
Wrong: Gosling                                                                                                                                               
Wrong: PrintStream                                                                                                                                           
Right: Shape                                                                                                                                                 
Right: RectangularShape                                                                                                                                      
Right: Object                                                                                                                                                
Wrong: String                                                                                                                                                
Which Java keyword is used to define a subclass?                                                                                                             
extends                                                                                                                                                      
What is the original name of the Java language?                                                                                                              
Option A: *7                                                                                                                                                 
Option B: C--                                                                                                                                                
Option C: Oak                                                                                                                                                
Option D: Gosling                                                                                                                                            
C                                                                                                                                                            
Which of the following types are supertypes of Rectangle?                                                                                                    
Option A: PrintStream                                                                                                                                        
Option B: Shape                                                                                                                                              
Option C: RectangularShape                                                                                                                                   
Option D: Object                                                                                                                                             
Option E: String                                                                                                                                             
B                                                                                                                                                            
What is the square root of 2?                                                                                                                                
1                                                                                                                                                            
                                                                                                                                                             
Which Java keyword is used to define a subclass?                                                                                                             
Correct answer: extends                                                                                                                                      
Your answer: extends                                                                                                                                         
Your answer was correct.                                                                                                                                     
                                                                                                                                                             
What is the original name of the Java language?                                                                                                              
Option A: *7                                                                                                                                                 
Option B: C--                                                                                                                                                
Option C: Oak                                                                                                                                                
Option D: Gosling                                                                                                                                            
Correct answer: C                                                                                                                                            
Your answer: C                                                                                                                                               
Your answer was correct.                                                                                                                                     

Which of the following types are supertypes of Rectangle?                                                                                                    
Option A: PrintStream                                                                                                                                        
Option B: Shape                                                                                                                                              
Option C: RectangularShape                                                                                                                                   
Option D: Object                                                                                                                                             
Option E: String                                                                                                                                             
Correct answer: BCD                                                                                                                                          
Your answer: B                                                                                                                                               
Your answer was not correct.