Question: The local Driver\'s License Office has asked you towrite a program tha
ID: 3640764 • Letter: Q
Question
Question: The local Driver's License Office has asked you towrite
a program that grades the written portion of thedriver's
license exam. The exam has 20 multiple choicequestions.
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
A student must correctly answer 15 of the 20questions
to pass the exam.
Write a class named DriverExam that holds the correct answers
to the exam in an array field. The class should alsohave
an array field that holds the student's answers. Theclass
should have the following methods:
- passed. Returns true if the student passed theexam, or
false if the student failed
- totalCorrect. Returns the total number ofcorrectly answered
questions
- totalIncorrect. Returns the total numner ofincorrectly
answeredquestions
- questionsMissed. An int array containing thequestion numbers
of thequestions that the student missed
Demonstrate the class in a complete program that asksthe user to
enter a student's answers, and then displays theresults returned
from the DriverExam class's methods.
Input Validation: Only accept the letters A, B, C, or Das answers.
Explanation / Answer
import java.util.Scanner;
class DriverExam
{
// an array of student's answers.
private String[] correctAnswers =
{"B","D","A","A","C","A",
"B","A","C","D",
"B","C","D","A",
"D","C","C","B","D","A"};
// store the user answers.
private String[] userAnswers;
int[] missed = new int[correctAnswers.length];
// Process the user answers
public DriverExam(String[] Answers)
{
userAnswers=new String[Answers.length];
for (int i = 0; i < Answers.length; i++)
{
userAnswers[i]=Answers[i];
}
}
//returns boolean value if correct answers > 15
public boolean passed()
{
if(totalCorrect() >= 15)
return true;
else
return false;
}
// totalCorrect answers.
public int totalCorrect()
{
int correctCount=0;
for (int i = 0; i < correctAnswers.length; i++)
{
if (userAnswers[i].equalsIgnoreCase
(correctAnswers[i]))
{
correctCount++;
}
}
return correctCount;
}
// totalIncorrect answered.
public int totalIncorrect()
{
int incorrectCount=0;
for (int i = 0; i < correctAnswers.length; i++)
{
if (!userAnswers[i].
equalsIgnoreCase(correctAnswers[i]))
{
missed[incorrectCount] = i;
incorrectCount++;
}
}
return incorrectCount;
}
// Missed questions.
public int[] questionsMissed()
{
return missed;
}
}//end driver exam class
// Input Validation:
public class DriverExamApplication
{
public static void main(String[] args)
{
System.out.println(" 20 Multiple choice questions
Mark A,B,C,D .");
// Inputting string
String[] answers = new String[20];
String answer;
for (int i = 0; i < 20; i++)
{
do
{
System.out.print((i + 1) + ": ");
answer = kb.nextLine();
}while (!isValidAnswer(answer));
answers[i] = answer;
}
// PROCCESS
DriverExam exam = new DriverExam(answers);
//RESULT
System.out.println(" RESULTS ");
//outputting Total correct
System.out.println("Total Correct: " +
exam.totalCorrect());
//outputting Total incorrect
System.out.println("Total Incorrect: "
+ exam.totalIncorrect());
String passed = exam.passed() ? "YES" : "NO";
//result pass or fail
System.out.println("Passed: " + passed);
if (exam.totalIncorrect() > 0)
{
System.out.println("The incorrect answers are:");
int missedIndex;
for (int i = 0; i < exam.totalIncorrect(); i++)
{
missedIndex = exam.questionsMissed()[i] + 1;
System.out.print(" " + missedIndex);
}
}
}//end main
// Returns TRUE when the answer is valid.
public static boolean isValidAnswer(String answer)
{
return "A".equalsIgnoreCase(answer) ||
"B".equalsIgnoreCase(answer)
|| "C".equalsIgnoreCase(answer) ||
"D".equalsIgnoreCase(answer);
}
Driver's License Exam
20 Multiple choice questions Mark A,B,C,D.
1: B
2: C
3: A
4: A
5: A
6: A
7: B
8: A
9: C
10: D
11: B
12: C
13: D
14: A
15: D
16: C
17: C
18: B
19: D
20: A
RESULTS
Total Correct: 18
Total Incorrect: 2
Passed: YES
The incorrect answers are:
2 5
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.