This commit is contained in:
2025-03-13 20:46:31 -05:00
parent e1b7ade84e
commit ff94903a10
2 changed files with 22 additions and 13 deletions

View File

@@ -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<Integer> questionsMissed() {
ArrayList<Integer> 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);

View File

@@ -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<Integer> missed = test.questionsMissed();
for(var x : missed) {
System.out.print(x + " ");
}