Added some Javadocs
This commit is contained in:
@@ -5,26 +5,39 @@ import java.util.ArrayList;
|
|||||||
* and handles logic for calculating if the user passed, and displays incorrect answers
|
* and handles logic for calculating if the user passed, and displays incorrect answers
|
||||||
*/
|
*/
|
||||||
public class DriverExam {
|
public class DriverExam {
|
||||||
/** Array of correct exam answers. IStatic because it does not change or depends on which
|
/** Array of correct exam answers. Is Static because it does not change or depends on which user is taking
|
||||||
* user is taking the exam
|
* the exam **/
|
||||||
**/
|
|
||||||
private final static char[] correctAnswers =
|
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'};
|
{'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 **/
|
/** Array of the user's answers **/
|
||||||
private final char[] givenAnswers;
|
private final char[] givenAnswers;
|
||||||
|
|
||||||
/** Count of correct user answers **/
|
/** Count of correct user answers **/
|
||||||
private int totalCorrect;
|
private int totalCorrect;
|
||||||
|
|
||||||
/** Corresponds to each question, each true if correct answer **/
|
/** Corresponds to each question, each true if correct answer **/
|
||||||
private boolean[] missedQuestions;
|
private boolean[] missedQuestions;
|
||||||
|
|
||||||
|
// Constructors
|
||||||
|
public DriverExam(char[] givenAnswers) {
|
||||||
|
this.givenAnswers = givenAnswers;
|
||||||
|
// Initialize missedQuestions to length 20. By default, they will all be False
|
||||||
|
missedQuestions = new boolean[20];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Compares givenAnswers to correctAnswers, counting amount correct
|
* Compares givenAnswers to correctAnswers, counting amount correct
|
||||||
* Also updates missedQuestions with incorrect answers
|
* Also updates missedQuestions with incorrect answers
|
||||||
|
* 15 out of 20 is required to pass
|
||||||
* @return If user passed exam
|
* @return If user passed exam
|
||||||
*/
|
*/
|
||||||
public boolean passed() {
|
public boolean passed() {
|
||||||
|
// Count correct answers
|
||||||
int correct = 0;
|
int correct = 0;
|
||||||
|
// Loop through and compare givenAnswers to Correct Answer
|
||||||
|
// Updating correct count, or missedQuestions when needed
|
||||||
for(int i = 0; i < givenAnswers.length; i++) {
|
for(int i = 0; i < givenAnswers.length; i++) {
|
||||||
if(givenAnswers[i] == correctAnswers[i]) {
|
if(givenAnswers[i] == correctAnswers[i]) {
|
||||||
correct++;
|
correct++;
|
||||||
@@ -33,22 +46,34 @@ public class DriverExam {
|
|||||||
missedQuestions[i] = true;
|
missedQuestions[i] = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// Update member
|
||||||
totalCorrect = correct;
|
totalCorrect = correct;
|
||||||
return correct >= 15;
|
return correct >= 15;
|
||||||
}
|
}
|
||||||
|
|
||||||
public DriverExam(char[] givenAnswers) {
|
|
||||||
this.givenAnswers = givenAnswers;
|
|
||||||
missedQuestions = new boolean[20];
|
|
||||||
}
|
|
||||||
// Getter methods
|
// Getter methods
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns count of correct answers
|
||||||
|
* @return Count of correct answers
|
||||||
|
*/
|
||||||
public int totalCorrect() {
|
public int totalCorrect() {
|
||||||
return totalCorrect;
|
return totalCorrect;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns count of incorrect answers, by subtracting correct from 20
|
||||||
|
* @return Count of incorrect answers
|
||||||
|
*/
|
||||||
public int totalIncorrect() {
|
public int totalIncorrect() {
|
||||||
return 20 - totalCorrect;
|
return 20 - totalCorrect;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Appends the question number of the missed questions to an arraylist so we can have a list of the incorrect
|
||||||
|
* @return Arraylist with the numbers of the incorrectly answered questions
|
||||||
|
*/
|
||||||
public ArrayList<Integer> questionsMissed() {
|
public ArrayList<Integer> questionsMissed() {
|
||||||
ArrayList<Integer> missedNums = new ArrayList<>();
|
ArrayList<Integer> missedNums = new ArrayList<>();
|
||||||
for(int i = 0; i < missedQuestions.length; i++) {
|
for(int i = 0; i < missedQuestions.length; i++) {
|
||||||
|
|||||||
@@ -5,15 +5,17 @@ public class Main {
|
|||||||
public static void main(String[] args) {
|
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 = {'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];
|
char[] answers = new char[20];
|
||||||
|
|
||||||
|
|
||||||
/// ////////
|
/// ////////
|
||||||
int i = 0;
|
int i = 0;
|
||||||
String checkString = "ABCD";
|
String checkString = "ABCD";
|
||||||
Scanner scanner = new Scanner(System.in);
|
Scanner scanner = new Scanner(System.in);
|
||||||
do {
|
do {
|
||||||
System.out.print("Question " + (i+1) + " Student Answer: ");
|
System.out.print("Question " + (i+1) + " Student Answer: ");
|
||||||
String userIn = scanner.nextLine();
|
String userIn = scanner.nextLine().toUpperCase();
|
||||||
|
|
||||||
if(userIn.length() > 1){
|
if(userIn.length() != 1){
|
||||||
System.out.println("Please input only a single letter A,B,C,or D");
|
System.out.println("Please input only a single letter A,B,C,or D");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user