Here they are
This commit is contained in:
63
src/DriverExam.java
Normal file
63
src/DriverExam.java
Normal file
@@ -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<Integer> questionsMissed() {
|
||||||
|
ArrayList<Integer> missedNums = new ArrayList<>();
|
||||||
|
for(int i = 0; i < missedQuestions.length; i++) {
|
||||||
|
if(missedQuestions[i]) {
|
||||||
|
missedNums.add(i + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return missedNums;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
42
src/Main.java
Normal file
42
src/Main.java
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
import java.util.ArrayList;
|
||||||
|
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'};
|
||||||
|
char[] answers = new char[20];
|
||||||
|
/// ////////
|
||||||
|
int i = 0;
|
||||||
|
String checkString = "ABCD";
|
||||||
|
Scanner scanner = new Scanner(System.in);
|
||||||
|
do {
|
||||||
|
System.out.print("Question " + (i+1) + " Student Answer: ");
|
||||||
|
String userIn = scanner.nextLine();
|
||||||
|
|
||||||
|
if(userIn.length() > 1){
|
||||||
|
System.out.println("Please input only a single letter A,B,C,or D");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if(!checkString.contains(userIn)) {
|
||||||
|
System.out.println("Please input only a single letter A,B,C,or D");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
answers[i] = userIn.charAt(0);
|
||||||
|
i++;
|
||||||
|
} while (i < 20);
|
||||||
|
|
||||||
|
/// ///////
|
||||||
|
|
||||||
|
DriverExam test = new DriverExam(answers);
|
||||||
|
|
||||||
|
System.out.println(test.passed());
|
||||||
|
System.out.println(test.totalCorrect());
|
||||||
|
System.out.println(test.totalIncorrect());
|
||||||
|
ArrayList<Integer> missed = test.questionsMissed();
|
||||||
|
|
||||||
|
for(var x : missed) {
|
||||||
|
System.out.print(x + " ");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user