0 Members and 1 Guest are viewing this topic.
import java.util.Scanner;class DriversExam{ private String[] corAns = {"B", "D", "A", "A", "C", "A", "B", "A", "C", "D", "B", "C", "D", "A", "D", "C", "C", "B", "D", "A"}; //STORE THE ANSWERS private String[] userAns; int[] missedAnswers = new int[corAns.length]; //process the user answers public DriversExam(String[] a) { userAns = new String[a.length]; for (int i = 0; i < a.length; i++) { userAns[i] = a[i]; } } public DriversExam() { } //returns boolean value if correct > 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 < corAns.length; i++) { if(userAns[i].equalsIgnoreCase(corAns[i])) { correctCount++; } } return correctCount; } //total incorrect answered public int totalIncorrect() { int wrong = 0; for (int i = 0; i < corAns.length; i++) { if (!userAns[i].equalsIgnoreCase(corAns[i])) { missedAnswers[wrong] = i; wrong++; } } return wrong; } //Missed questions public int[] questionsMissed() { return missedAnswers; } public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); System.out.println("Drivers Liscense Exam."); System.out.println("20 multiple choice questions."); //Inputting String String[] answers = new String[20]; String answer; for (int i = 0; i < 20; i++) { do { System.out.println((i +1) + ": "); answer = keyboard.nextLine(); }while (!isValid(answer)); answers[i] = answer; } //Process DriversExam drivers = new DriversExam(answers); //Result System.out.println("Totcal Correct: " + drivers.totalIncorrect()); System.out.println("Totcal Incorrect: " + drivers.totalIncorrect()); if (drivers.totalIncorrect() > 0) { System.out.println("The incorrect answers are: "); int missedIndex; for (int i = 0; i < drivers.totalIncorrect(); i++) { missedIndex = drivers.questionsMissed()[i] + 1; System.out.println(" " + missedIndex); } } DriversExam pass = new DriversExam(); if (pass.passed() == true) { System.out.println("You passed."); } else System.out.println("You failed."); } public static boolean isValid(String a) { return "A".equalsIgnoreCase(a) || "B".equalsIgnoreCase(a) || "C".equalsIgnoreCase(a) || "D".equalsIgnoreCase(a); }}