Add DriverExam.java
This commit is contained in:
63
DriverExam.java
Normal file
63
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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user