Driver\'s License Exam The local Driver\'s License Office has asked you to write
ID: 3939960 • Letter: D
Question
Driver's License Exam
The local Driver's License Office has asked you to 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
The class should have the following methods:
getStudentAnswer. Include an array to hold the student's answers. Ask the user to enter a student's answers. Return the answers to main().
passed. Returns true if the student passed the exam, or false if the student failed. A student must correctly answer 15 of 20 questions to pass the exam.
totalCorrect. Returns the total number of correctly answered questions
totalIncorrect. Returns the total number of incorrectly answered questions
questonsMissed. An array containing the question numbers of the questions that the student missed
Display the results from the method displayResults.
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
Explanation / Answer
DriveExaM.java
class DriveExam
{
// an array of student's answers.
private final 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 final String[] userAnswers;
int[] missed = new int [correctAnswers. length];
// Process the user answers
public DriveExam(String[] Answers)
{
userAnswers = new String[Answers.length];
for (int i= 0; i< Answers.length; i++)
{
userAnswers[i] = Answers[i];
}
}
//return boolean value if correct answers > 15
public boolean passed()
{
if(totalCorrect()>= 15)
return true;
else
return false;
}
// total Correct answers
public int totalCorrect()
{
int correctCount=0;
for (int i =0; i < correctAnswers.length;i++)
{
if (userAnswers[i].equalsIgnoreCase(correctAnswers[i]))
{
// missed [correctCount]= i;
correctCount++;
}
}
return correctCount;
}
// total Correct answers
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[] questionMissed()
{
return missed;
}
}
_______________________________
ExamApplication.java
import java.util.Scanner;
public class ExamApplication
{
public static void main(String[]args)
{
System.out.println("Driver's Lincense Exam");
//scanner class object is used to read the inputs entered by the user
Scanner kb=new Scanner(System.in);
System.out.println("20 Multiple choice questions ");
// Creating the array of type 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;
}
//Process
DriveExam exam= new DriveExam(answers);
displayResult(exam);
}
private static void displayResult(DriveExam exam) {
//Result
System.out.print ("Result");
// 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.questionMissed()[i]+1;
System.out.print(" "+missedIndex);
}
}
}
// return 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);
}
}
________________________________
Output:
Driver's Lincense Exam
20 Multiple choice questions
1:B
2:D
3:A
4:A
5:C
6:A
7:B
8:A
9:C
10:D
11:B
12:C
13:D
14:A
15:C
16:B
17:A
18:B
19:D
20:C
ResultTotal Correct:16
total incorrect:4
Passed :Yes
The incorrect answers are
15 16 17 20
________________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.