PLEASE ONLY JAVA PROGRAM 1-D ARRAY PLEASE WITH THE 4 METHODS: passed, totalCorre
ID: 3542218 • Letter: P
Question
PLEASE ONLY JAVA PROGRAM 1-D ARRAY PLEASE WITH THE 4 METHODS: passed, totalCorrect, totallncorrect, and questionsMissed.
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
2. D
3. A
4. A
5. B
6. A
7. B
8. A
9. C
10. 0
I I . B
12. B
13. D
14. A
15. D
16. C
17. C
18. B
19. D
20. A
. A student must correctly answer 15 of the 20 questions to pass the exam.
Write a class named DriverExam that holds the correct answers to the exam in an array field.
The class should also have an array field that holds the student's answers. The class should
have the following methods:
passed. Returns true if the student passed the exam, or false if the student failed
totalCorrect. Returns the total number of correctly answered questions
totallncorrect. Returns the total number of incorrectly answered questions
questionsMissed. An int array containing the question numbers of the questions that
the student missed
Demonstrate the class in a complete program that asks the user to enter a student's answers,
and then displays the results returned from the DriverExam class's methods.
Input Validation: only accept the letters A, B. C. or D as answers
Explanation / Answer
please rate - thanks
import java.util.*;
public class DriverExam1
{public static void main(String[] args)
{char[] key={'B','D','A','A','B','A','B','A','C','0','B','B','D','A','D','C','C','B','D','A'};
char []student=new char[20];
int[] questionsMissed=new int[20];
Scanner in=new Scanner(System.in);
int correct,incorrect,i;
System.out.println("Enter your answers: ");
for(i=0;i<20;i++)
{do
{System.out.print("Question "+(i+1)+": ");
student[i]=in.next().charAt(0);
if(student[i]<'A'||student[i]>'D')
System.out.println("must be A, B, C, or D");
}while(student[i]<'A'||student[i]>'D');
}
correct=totalCorrect(student,questionsMissed,key);
incorrect=totalIncorrect(correct);
System.out.println("correct answers: "+correct);
System.out.println("incorrect answers: "+incorrect);
System.out.println("Questions you missed");
System.out.println("quest your correct number answer answer");
for(i=0;i<incorrect;i++)
System.out.println(questionsMissed[i]+" "+student[questionsMissed[i]-1]+" "+key[questionsMissed[i]-1]);
if(passed(correct))
System.out.println("congrats :) you passed");
else
System.out.println("sorry :( you failed");
}
public static int totalCorrect(char[] student, int[]questionsMissed,char[] key)
{int i, correct=0,missed=0;
for(i=0;i<20;i++)
if(student[i]==key[i])
correct++;
else
{questionsMissed[missed]=i+1;
missed++;
}
return correct;
}
public static int totalIncorrect(int correct)
{return 20-correct;
}
public static boolean passed(int correct)
{if(correct>=15)
return true;
else
return false;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.