java code and pseudocode require: an array for the correct answers, an array for
ID: 3713096 • Letter: J
Question
java code and pseudocode
require:
an array for the correct answers,
an array for the answers given, and
a function passing an array by reference.
using an array for the answers.
Driver s License Exam
The local driver s license office has asked you to design 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 these correct answers in an array. (Store each question s correct answer in an element of a String array.) The program should ask the user to enter the student s answers for each of the 20 questions, which should be stored in another array. After the student s answers have 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 questions, the total number of incorrectly answered questions, and a list showing the question numbers of the incorrectly answered questions.
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
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.