The local driver\'s license office has asked you to create an application that g
ID: 3861218 • Letter: T
Question
The local driver's license office has asked you to create an application that grades the written portion of the driver's license exam. The exam has 20 multiple-choice questions. Here are the correct answers: Your program should store these correct answers in an array. The program should read the student's answers for each of the 20 questions from a text file and store the answers in another array. (Create your own text file to test the application.) After the student's answers have been read from the file, 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 questions, the total number of incorrectly answered questions, and a list showing the question numbers of the incorrectly answered questions.Explanation / Answer
answers.txt
B
D
A
A
C
A
B
A
C
D
B
C
D
A
C
B
A
B
D
C
________________
DriveTest.java
public class DriveTest {
// 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 DriveTest(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;
}
}
_____________________
TestApplication.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class TestApplication {
public static void main(String[] args) throws FileNotFoundException {
System.out.println("** Driver's Lincense Test **");
//scanner class object is used to read the inputs entered by the user
Scanner kb = new Scanner(new File("answers.txt"));
// Creating the array of type String
String[] answers = new String[20];
int i = 0;
String answer;
while (kb.hasNext()) {
answers[i++] = kb.nextLine();
}
kb.close();
//Process
DriveTest xam = new DriveTest(answers);
displayResult(xam);
}
private static void displayResult(DriveTest xam) {
//Result
System.out.println("Result ::");
// outputting Total correct
System.out.println("Total Correct:" + xam.totalCorrect());
//outputting total incorrect
System.out.println("total incorrect:" + xam.totalInCorrect());
String passed = xam.passed() ? "Yes " : " No";
// result pass or fail
System.out.println("Passed :" + passed);
if (xam.totalInCorrect() > 0) {
System.out.println("The incorrect answers are");
int missedIndex;
for (int i = 0; i < xam.totalInCorrect(); i++) {
missedIndex = xam.questionMissed()[i] + 1;
System.out.print(" " + missedIndex);
}
}
}
}
________________________
Output:
** Driver's Lincense Test **
Result ::
Total Correct:16
total incorrect:4
Passed :Yes
The incorrect answers are
15 16 17 20
_____________Could you rate me well.Plz .Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.