From d860a26f075be46c882c1ebb2cd32e445ddc4c89 Mon Sep 17 00:00:00 2001 From: Alex Date: Wed, 12 Mar 2025 15:18:41 -0700 Subject: [PATCH] Add DriverExam.java --- DriverExam.java | 63 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 DriverExam.java diff --git a/DriverExam.java b/DriverExam.java new file mode 100644 index 0000000..7c04d01 --- /dev/null +++ b/DriverExam.java @@ -0,0 +1,63 @@ +import java.util.ArrayList; + +/** + * The DriverExam class holds the correct answers for the drivers test + * and handles logic for calculating if the user passed, and displays incorrect answers + */ +public class DriverExam { + /** Array of correct exam answers. IStatic because it does not change or depends on which + * user is taking the exam + **/ + private final static char[] correctAnswers = + {'B','D','A','A','C','A','B','A','C','D','B','C','D','A','D','C','C','B','D','A'}; + + /** Array of user's answers **/ + private final char[] givenAnswers; + /** Count of correct user answers **/ + private int totalCorrect; + /** Corresponds to each question, each true if correct answer **/ + private boolean[] missedQuestions; + + /** + * Compares givenAnswers to correctAnswers, counting amount correct + * Also updates missedQuestions with incorrect answers + * @return If user passed exam + */ + public boolean passed() { + int correct = 0; + for(int i = 0; i < givenAnswers.length; i++) { + if(givenAnswers[i] == correctAnswers[i]) { + correct++; + } + else{ + missedQuestions[i] = true; + } + } + totalCorrect = correct; + return correct >= 15; + } + + public DriverExam(char[] givenAnswers) { + this.givenAnswers = givenAnswers; + missedQuestions = new boolean[20]; + } + // Getter methods + public int totalCorrect() { + return totalCorrect; + } + public int totalIncorrect() { + return 20 - totalCorrect; + } + + public ArrayList questionsMissed() { + ArrayList missedNums = new ArrayList<>(); + for(int i = 0; i < missedQuestions.length; i++) { + if(missedQuestions[i]) { + missedNums.add(i + 1); + } + } + return missedNums; + } + + +}