write a program that grades the written portion of the drive
write a
program that grades the written portion of the driver\'s license
exam. The exam has 20 multiple
-
choice questions. Here are the
correct answers:
1.B
6.A
11.B
16.C
2.D
7.B
12.C
17.C
3.A
8.A
13.D
18.B
4.A
9.C
14.A
19.D
5.C
10.D
15.D
20.A
Your program should store the correct answers shown above in an
array. It should ask the user to enter the student\'s answers for
each of the 20 questions, and the answers should be stored in
another array. After the student\'s answers hav
e been entered, the
program should display a message indicating whether the student
passed or failed the exam. (A student must correctly answer 15 of
the 20 questions to pass the exam.)
It should then display the total number of correctly answered
questio
ns, the total number of incorrectly answered questions,
and a list showing the question numbers of the incorrectly
answered questions.
Input Validation: Only accept the letters A, B, C, or D as
answers.
IMPORTANT:
Your program must display informative messages
for both input inquiry and output display. Vague messages
and bad display of results will result in losing points.
Solution
DrivingLicenseTest.java
public class DrivingLicenseTest {
public static void main(String[] args) {
char correctAnswers[] = {\'B\', \'D\',\'A\',\'A\',\'C\',\'A\',\'B\',\'A\',\'C\',\'D\', \'B\', \'C\', \'D\', \'A\', \'D\', \'C\', \'C\', \'B\', \'D\', \'A\'};
char studentAnswers[] = new char[20];
java.util.Scanner in = new java.util.Scanner(System.in);
int quesCounter = 0;
for(int i=0; i<studentAnswers.length; i++){
System.out.print(\"Please enter your Answer (A, B, C, and D are the only valid inputs) for Question \"+(i+1)+\": \");
char c = in.next().charAt(0);
if(c == \'A\' || c == \'a\' || c == \'B\' || c == \'b\' || c == \'C\' || c == \'c\' || c == \'D\' || c == \'d\'){
studentAnswers[quesCounter] = c;
quesCounter++;
}
else{
System.out.print(\"Invalid entry. \");
i--;
}
}
grade(correctAnswers, studentAnswers);
}
public static void grade(char[] correctAnswers, char[] studentAnswers){
int match = 0;
int nonmatch = 0;
for(int i=0; i<correctAnswers.length; i++){
if(correctAnswers[i] == Character.toUpperCase(studentAnswers[i])){
match++;
}
else{
nonmatch++;
}
}
if(match >= 8){
System.out.println(\"Congratulations! You have passed exam\");
System.out.println(\"Total number of correct answers: \"+match);
System.out.println(\"Total number of incorrect answers: \"+nonmatch);
}
else{
System.out.println(\"Sorry, you have not passed the exam! \");
System.out.println(\"Total number of correct answers: \"+match);
System.out.println(\"Total number of incorrect answers: \"+nonmatch);
}
}
}
Output:
Please enter your Answer (A, B, C, and D are the only valid inputs) for Question 1: A
Please enter your Answer (A, B, C, and D are the only valid inputs) for Question 2: B
Please enter your Answer (A, B, C, and D are the only valid inputs) for Question 3: C
Please enter your Answer (A, B, C, and D are the only valid inputs) for Question 4: D
Please enter your Answer (A, B, C, and D are the only valid inputs) for Question 5: E
Invalid entry. Please enter your Answer (A, B, C, and D are the only valid inputs) for Question 5: A
Please enter your Answer (A, B, C, and D are the only valid inputs) for Question 6: B
Please enter your Answer (A, B, C, and D are the only valid inputs) for Question 7: C
Please enter your Answer (A, B, C, and D are the only valid inputs) for Question 8: D
Please enter your Answer (A, B, C, and D are the only valid inputs) for Question 9: A
Please enter your Answer (A, B, C, and D are the only valid inputs) for Question 10: C
Please enter your Answer (A, B, C, and D are the only valid inputs) for Question 11: B
Please enter your Answer (A, B, C, and D are the only valid inputs) for Question 12: D
Please enter your Answer (A, B, C, and D are the only valid inputs) for Question 13: A
Please enter your Answer (A, B, C, and D are the only valid inputs) for Question 14: A
Please enter your Answer (A, B, C, and D are the only valid inputs) for Question 15: A
Please enter your Answer (A, B, C, and D are the only valid inputs) for Question 16: A
Please enter your Answer (A, B, C, and D are the only valid inputs) for Question 17: A
Please enter your Answer (A, B, C, and D are the only valid inputs) for Question 18: A
Please enter your Answer (A, B, C, and D are the only valid inputs) for Question 19: A
Please enter your Answer (A, B, C, and D are the only valid inputs) for Question 20: A
Sorry, you have not passed the exam!
Total number of correct answers: 3
Total number of incorrect answers: 17
---------------------------------------------------------------
Please enter your Answer (A, B, C, and D are the only valid inputs) for Question 1: B
Please enter your Answer (A, B, C, and D are the only valid inputs) for Question 2: D
Please enter your Answer (A, B, C, and D are the only valid inputs) for Question 3: A
Please enter your Answer (A, B, C, and D are the only valid inputs) for Question 4: A
Please enter your Answer (A, B, C, and D are the only valid inputs) for Question 5: C
Please enter your Answer (A, B, C, and D are the only valid inputs) for Question 6: A
Please enter your Answer (A, B, C, and D are the only valid inputs) for Question 7: B
Please enter your Answer (A, B, C, and D are the only valid inputs) for Question 8: A
Please enter your Answer (A, B, C, and D are the only valid inputs) for Question 9: C
Please enter your Answer (A, B, C, and D are the only valid inputs) for Question 10: D
Please enter your Answer (A, B, C, and D are the only valid inputs) for Question 11: B
Please enter your Answer (A, B, C, and D are the only valid inputs) for Question 12: A
Please enter your Answer (A, B, C, and D are the only valid inputs) for Question 13: D
Please enter your Answer (A, B, C, and D are the only valid inputs) for Question 14: A
Please enter your Answer (A, B, C, and D are the only valid inputs) for Question 15: D
Please enter your Answer (A, B, C, and D are the only valid inputs) for Question 16: C
Please enter your Answer (A, B, C, and D are the only valid inputs) for Question 17: C
Please enter your Answer (A, B, C, and D are the only valid inputs) for Question 18: B
Please enter your Answer (A, B, C, and D are the only valid inputs) for Question 19: D
Please enter your Answer (A, B, C, and D are the only valid inputs) for Question 20: B
Congratulations! You have passed exam
Total number of correct answers: 18
Total number of incorrect answers: 2



