Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Driver\'s License Exam: The local Driver\'s License Office has asked you to w

ID: 3683607 • Letter: 1

Question

1. 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.C 6.A 7.B 8.A 9.C 10.D 11.B 12.C 13.D 14.A 15.D 16.C 17.C 18.B 19.D 20.A A student must correctly answer 15 out of 20 to pass the exam. Write a class named DriverExam that holds the correct answer 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 passes the exam, or false if the student failed. totalCorrect: Returns the total number of correctly answered questions. totalIncorrect: 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 students' answers, and then displays the results returned from the DriverExam class's methods (multiple files). Input Validation: only accept the letters A, B, C, or D as answers.

Explanation / Answer

/**
* @author Srinivas Palli
*
*/
class DriverExam {
   private char[] key = { 'B', 'D', 'A', 'A', 'C', 'A', 'B', 'A', 'C', 'D',
           'B', 'C', 'D', 'A', 'D', 'C', 'C', 'B', 'D', 'A' };
   private char[] answers;

   /**
   * constructor to set the answers
   *
   * @param ans
   */
   public DriverExam(char[] ans) {
       answers = ans;
   }

   /**
   * method to check the exam pass or not
   *
   * @return
   */
   public boolean passed() {
       return (totalCorrect() > 14);
   }

   /**
   * method to get the total correct answers
   *
   * @return
   */
   public int totalCorrect() {
       int correct = 0;
       for (int i = 0; i < key.length; i++) {

           if (key[i] == answers[i])
               correct++;
       }
       return correct;
   }

   /**
   * method to get the total missed answer
   *
   * @return
   */
   public int totalMissed() {
       int tmissed = 0;
       tmissed = key.length - totalCorrect();
       return tmissed;
   }

   /**
   * method to get the list of total missed answers
   *
   * @return
   */
   public int[] questionsMissed() {

       int size = key.length - totalCorrect();
       int[] missed = {};
       if (size < 1)
           return missed;
       else
           missed = new int[size];

       int pos = 0;
       for (int i = 0; i < key.length; i++) {
           if (key[i] != answers[i]) {
               missed[pos] = (i + 1);
               pos = pos + 1;
           }
       }
       return missed;

   }
}

//driver

import java.util.Arrays;
import java.util.Scanner;

public class TestDrivingExam {

   public static void main(String[] args) {
       // accept keyboard input
       Scanner keyboard = new Scanner(System.in);

       System.out.println("Enter your answers below. ");

       char[] answers = new char[20];

       // request an answer for each question
       for (int i = 0; i < answers.length; i++) {
           // get input until input is valid
           char input;

           do {
               System.out.print(i + 1 + ". ");

               // get character and make it upper case
               input = Character.toUpperCase(keyboard.next().charAt(0));
           } while (input < 'A' || input > 'D');

           // store answer
           answers[i] = input;
       }

       // print output here
       DriverExam driver = new DriverExam(answers) {
       };
       System.out.println();
       System.out.println("You "
               + (driver.passed() ? "passed" : "did not pass") + ". ");
       System.out.println("Correct: " + driver.totalCorrect() + " ");
       System.out.println("Incorrect: " + driver.totalMissed() + " ");
       System.out.println("Questions missed: "
               + Arrays.toString(driver.questionsMissed()));

   }
}

OUTPUT:

Test 1:

Enter your answers below.

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. D
16. C
17. C
18. Q
18. A
19. C
20. B

You passed.

Correct: 17

Incorrect: 3

Questions missed: [18, 19, 20]

Test 2:
Enter your answers below.

1. A
2. D
3. B
4. C
5. D
6. B
7. A
8. B
9. D
10. C
11. A
12. B
13. D
14. C
15. A
16. B
17. C
18. D
19. A
20. B

You did not pass.

Correct: 3

Incorrect: 17

Questions missed: [1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 15, 16, 18, 19, 20]