From ff94903a104ec203f4642012f7c0fe057be22e71 Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 13 Mar 2025 20:46:31 -0500 Subject: [PATCH] Finished --- src/DriverExam.java | 9 ++++++++- src/Main.java | 26 ++++++++++++++------------ 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/src/DriverExam.java b/src/DriverExam.java index 2163db0..65d6a93 100644 --- a/src/DriverExam.java +++ b/src/DriverExam.java @@ -19,13 +19,19 @@ public class DriverExam { /** Corresponds to each question, each true if correct answer **/ private boolean[] missedQuestions; - // Constructors + // Constructor + /** + * Constructs DE object using an array of given user answers + * @param givenAnswers Array of the users given answers + */ public DriverExam(char[] givenAnswers) { + // Initialzie givenAnswers array with passed array this.givenAnswers = givenAnswers; // Initialize missedQuestions to length 20. By default, they will all be False missedQuestions = new boolean[20]; } + // Methods /** * Compares givenAnswers to correctAnswers, counting amount correct @@ -76,6 +82,7 @@ public class DriverExam { */ public ArrayList questionsMissed() { ArrayList missedNums = new ArrayList<>(); + // Loop and add the index+1 of missed questions for(int i = 0; i < missedQuestions.length; i++) { if(missedQuestions[i]) { missedNums.add(i + 1); diff --git a/src/Main.java b/src/Main.java index e9a51cd..8b7a154 100644 --- a/src/Main.java +++ b/src/Main.java @@ -3,39 +3,41 @@ import java.util.Scanner; public class Main { public static void main(String[] args) { - //char[] answers = {'A','B','F','F','F','F','F','A','C','D','B','C','D','A','D','C','C','B','D','A'}; + // To hold the users inputted answers char[] answers = new char[20]; - - /// //////// - int i = 0; + // Compare against this string for input validation String checkString = "ABCD"; + int i = 0; // Loop variable Scanner scanner = new Scanner(System.in); + + // Loop until all 20 answers are given do { System.out.print("Question " + (i+1) + " Student Answer: "); - String userIn = scanner.nextLine().toUpperCase(); + String userIn = scanner.nextLine().toUpperCase(); // toUpperCase allows us to accept lowercase letters + // Reject the answer if it isnt just a letter if(userIn.length() != 1){ System.out.println("Please input only a single letter A,B,C,or D"); continue; } + // Reject the answer if it isnt A, B, C, or D if(!checkString.contains(userIn)) { System.out.println("Please input only a single letter A,B,C,or D"); continue; } + // Add to array and iterate if the answer is good answers[i] = userIn.charAt(0); i++; } while (i < 20); - /// /////// - + // Test methods for class DriverExam test = new DriverExam(answers); - - System.out.println(test.passed()); - System.out.println(test.totalCorrect()); - System.out.println(test.totalIncorrect()); + System.out.println("Passed: " + test.passed()); + System.out.println("Total Correct: " + test.totalCorrect()); + System.out.println("Total Incorrect: " + test.totalIncorrect()); + System.out.print("Questions Numbers Missed: "); ArrayList missed = test.questionsMissed(); - for(var x : missed) { System.out.print(x + " "); }