IN JAVA. CREATE 2 CLASSES. Write the data type class named DriverTestScoredriver
ID: 3770982 • Letter: I
Question
IN JAVA. CREATE 2 CLASSES. Write the data type class named DriverTestScoredriver AND the the driver class named DriverLicenseOnline .The local driver’s license office has asked you to write a program that grades the written portion of the driver’s license test. The driver license test has 25 multiple choice questions. The program will display the question numbers and read the answers from the keyboard. The driver should type a letter A, B, C or D to answer each question. The set of answers should be evaluated by comparing to the following key answers: 1.B 6.A 11. B 16. C 21.A 2.D 7.B 12.C 17.C 22.B 3.A 8.A 13.D 18.B 23.C 4.A 9.C 14.A 19.D 24.A 5.C 10.D 15.D 20.A 25.D A driver must have 20 correct questions to pass the test. The result will be displayed on the screen in the following format: Driver’s name: Jones, Becky Driver’s License: 123456789 Test date: 11/10/2014 (display current date) Result: PASSED Missed Questions: 5, 12, 15. CREATE 2 CLASS IN JAVA.
Explanation / Answer
public class DriverTestScoredriver
{
//An array containing a student's answers
private String[] correctAnswers =
{"B","A","B","C","A",
"D","B","C","C","B",
"A","A","D","B","C",
"A","C","A","D","A",
"C","D","D","A","D" };
//Store the user's answers
private String[] userAnswers;
int[] missed = new int[correctAnswers.length];
//Process the user's answers
public DriverTestScoredriver (String[] Answers)
{
userAnswers = new String[Answers.length];
for (int i = 0; i < Answers.length; i++)
{
userAnswers[i] = Answers[i];
}
}
//Returns a boolean value if correct answers > 20
public boolean passed()
{
if (totalCorrect() >= 20)
return true;
else
return false;
}
//Determines the total correct answers
public int totalCorrect()
{
int correctCount = 0;
for (int i = 0; i < correctAnswers.length; i++)
{
if (userAnswers[i].equalsIgnoreCase(correctAnswers[i]))
{
correctCount++;
}
}
return correctCount;
}
//Determines the total incorrect 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[] questionsMissed()
{
return missed;
}
}
//end of DriverTestScoredriver class
/* The DriverTestScoredriverApplication class demonstrates the methods of DriverTestScoredriver class. */
import java.util.Scanner;
public class DriverLicenseOnline
{
public static void main(String[] args)
{
System.out.println(" Driver's License Exam ");
Scanner input = new Scanner(System.in);
System.out.println(" Driver's Name ");
Scanner name = new Scanner(System.in);
System.out.println(" Driver's License Number");
Scanner license = new Scanner(System.in);
System.out.println(" Driver's License Exam Date ");
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
System.out.println(dateFormat.format(date)); //2014/08/06 15:59:48
System.out.println(" 25 Multiple-Choice Questions ");
System.out.println(" Mark A, B, C, D ");
//Inputting string
String[] answers = new String[20];
String answer;
for (int i = 0; i < 25; i++)
{
do
{
System.out.print((i+1) + ": ");
answer = input.nextLine();
} while (!isValidAnswer(answer));
answers[i] = answer;
}
//Process
DriverTestScoredriver exam = new DriverTestScoredriver(answers);
//Results
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);
}
System.out.println("Missed Questions are: "+ missedIndex);
}
} //end of main function
//Returns true when answer is valid
public static boolean isValidAnswer (String answer)
{
return "A".equalsIgnoreCase(answer) ||
"B".equalsIgnoreCase(answer)
|| "C".equalsIgnoreCase(answer) ||
"D".equalsIgnoreCase(answer);
}
} //end of Test class