The objective of this assignment is to use arrays for storage and to learn how t
ID: 3758210 • Letter: T
Question
The objective of this assignment is to use arrays for storage and to learn how to pass arrays as arguments.
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 6. A 11. B 16.C
2. D 7. A 12. D 17.A
3. A 8. D 13. C 18.D
4. A 9. B 14. A 19.A
5. C 10. B 15. C 20.A
The program should have a class name of Exam. Your program should store the correct answers shown above in an array.
The program should include a looping mechanism to prompt the user to enter each of the 20 student answers. It should ask the user to enter the student’s answers for each of the 20 questions, which should be stored into a second array.
The program should contain a static public method Correct, which accepts the two arrays as arguments and returns the number of correct answers.
The program should display the total number of correctly answered questions, along with a message indicating whether the student passed or failed the exam. (A student must correctly answer 15 of the 20 questions to pass the exam).
The program should accept only valid responses for each question: A, B, C or D.
The Assignment is being graded based on:
Array of correct answers, initialized properly (5 points)
Array of student entered answers (5 points)
Looping mechanism for collecting user input (5 points)
Compare method, which compares the two arrays (5 points)
Proper compilation and source code documentation (5 points)
Explanation / Answer
import java.io.*;
import java.util.Scanner;
class Exam
{
private static final int numberOfQuestions = 20; //Initializing the number of questions to 20.
//This method takes 2 arrays as input, first array being the correct answers.
//second array being the answers entered by the user, and finds how many
//answers are correct. The number of correct answers is returned.
public static int Correct(char[] correctAnswers, char[] userAnswers)
{
int count = 0;
for(int i = 0; i < numberOfQuestions; i++)
if(correctAnswers[i] == userAnswers[i])
count++;
return count;
}
public static void main(String[] args)
{
char[] correctAnswers = {'B','D','A','A','C','A','A','D','B','B','B','D','C','A','C','C','A','D','A','A'};
char[] userAnswers = new char[numberOfQuestions];
Scanner in = new Scanner(System.in);
System.out.println("Start Answering...");
//Loop for reading user answers.
for(int i = 0; i < numberOfQuestions; i++)
{
userAnswers[i] = '0';
//Loop for validating user input.
while(userAnswers[i] != 'A' && userAnswers[i] != 'B' && userAnswers[i] != 'C' && userAnswers[i] != 'D')
{
System.out.print("Enter your choice for Q "+(i+1)+"(A/B/C/D): ");
userAnswers[i] = in.next().charAt(0);
}
}
//Calling the function to validate the user answers.
int numberOfCorrectAnswers = Correct(correctAnswers, userAnswers);
System.out.println("You answered "+numberOfCorrectAnswers+" out of "+numberOfQuestions+" correctly.");
//Generating the report after evaluating the test result.
if(numberOfCorrectAnswers >= 15)
System.out.println("Congrats. You passed the test. Your license is being generated.");
else
System.out.println("Oops. You failed the test. Please try attempting the test again.");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.