➤ How to Code a Game
➤ Array Programs in Java
➤ Java Inline Thread Creation
➤ Java Custom Exception
➤ Hibernate vs JDBC
➤ Object Relational Mapping
➤ Check Oracle DB Size
➤ Check Oracle DB Version
➤ Generation of Computers
➤ XML Pros & Cons
➤ Git Analytics & Its Uses
➤ Top Skills for Cloud Professional
➤ How to Hire Best Candidates
➤ Scrum Master Roles & Work
➤ CyberSecurity in Python
➤ Protect from Cyber-Attack
➤ Solve App Development Challenges
➤ Top Chrome Extensions for Twitch Users
➤ Mistakes That Can Ruin Your Test Metric Program
Drivers License Exam Java Program | In this section, we write the code for the driver’s license exam in Java.
Program Description:- The local Driver’s License Office has asked you to write a program that grades the written portion of the license exam. In the exam, there is a total of 20 questions where the student is required to answer at least 15 correct answers to pass the exam, else he/she is termed as fail.
These are usually multiple-choice questions that have four options ‘A’, ‘B’, ‘C’,’ D’ the student should select any one of these.
import java.util.ArrayList;
import java.util.List;
public class DriverExam {
// correct answers
private char ansKey[] =
{ 'B', 'D', 'A', 'A', 'C',
'A', 'B', 'A', 'C', 'D',
'B', 'C', 'D', 'A', 'D',
'C', 'C', 'B', 'D', 'A' };
// array to store user answers
private char userAns[] = new char[ansKey.length];
private double passing = 65.0;
// constructor
public DriverExam(char[] userAns) {
this.userAns = userAns;
}
// method to check whether user is passed or not
public boolean passed() {
double percentage = (totalCorrectAns()*100) / ansKey.length;
return (percentage >= passing);
}
// method to count total correct answers
public int totalCorrectAns() {
int countCorrect = 0;
for (int i = 0; i < userAns.length; i++) {
if (userAns[i] == ansKey[i]) {
countCorrect++;
}
}
return countCorrect;
}
// method to count total incorrect answers
public int totalIncorrectAns() {
int countIncorrectAns = 0;
for (int i = 0; i < userAns.length; i++) {
if (userAns[i] != ansKey[i]) {
countIncorrectAns++;
}
}
return countIncorrectAns;
}
// method to return array of questions
// having incorrect answers
public int[] questionsMissed() {
List<Integer> missedQuestions = new ArrayList<Integer>();
for (int i = 0; i < userAns.length; i++) {
if (userAns[i] != ansKey[i]) {
missedQuestions.add(i + 1);
}
}
int questions[] = new int[missedQuestions.size()];
for(int i=0; i<questions.length; i++) {
questions[i] = missedQuestions.get(i);
}
return questions;
}
}
import java.util.Arrays;
import java.util.Scanner;
public class Application {
public static void main(String[] args) {
System.out.println("******************"
+"********************************");
System.out.println("****Driver's License Exam****");
System.out.println("20 Multiple-Choice Questions");
System.out.println("Answer in A, B, C, D\n");
System.out.println("******************"
+"********************************");
Scanner scan = new Scanner(System.in);
char studentAns[] = new char[20];
System.out.println("Enter the answer of the " +
"following questions:- ");
for (int i = 0; i < studentAns.length; i++) {
System.out.print((i + 1) + ": ");
studentAns[i] = scan.nextLine().charAt(0);
}
// create object
DriverExam exam = new DriverExam(studentAns);
System.out.println("\n******************"
+"********************************");
System.out.println("Results:- ");
System.out.println("Total correct answers: "
+ exam.totalCorrectAns());
System.out.println("Total incorrect answers: "
+ exam.totalIncorrectAns());
int[] missedQuestions = exam.questionsMissed();
if (missedQuestions.length != 0) {
System.out.print("The following questions " +
"have incorrect answers: ");
System.out.println(Arrays.toString(missedQuestions));
} else {
System.out.println("All answers are corrects");
}
System.out.println("Passing is at 65%");
if (exam.passed()) {
System.out.print("Congratulations, "
+ "You have passed the exam!");
} else {
System.out.print("You failed. Better luck next time.");
}
}
}
Output:-
**************************************************
****Driver’s License Exam****
20 Multiple-Choice Questions
Answer in A, B, C, D
**************************************************
Enter the answer of the following questions:-
1: B
2: D
3: A
4: A
5: C
6: A
7: B
8: A
9: A
10: D
11: B
12: C
13: D
14: A
15: A
16: C
17: C
18: B
19: D
20: A
**************************************************
Results:-
Total correct answers: 18
Total incorrect answers: 2
The following questions have incorrect answers: [9, 15]
Passing is at 65%
Congratulations, You have passed the exam!
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!