Java program. 1. [4 Points] Declare following arrays i. Integer array of size 10
ID: 3685107 • Letter: J
Question
Java program.
1. [4 Points] Declare following arrays i. Integer array of size 10 ii. An array of size 5 with following initial vales (1.0, 34.6, 7.8, 8.9, 0) iii. String array cable of storing 5 names
Part – II 2. [16 Points] Using Arrays: Driver’s License Exam Grader System 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 10 multiple choice questions. Here, are the correct answers: 1. B 6. A 2. D 7. B 3. A 8. A 4. A 9. C 5. C 10. D Your program should store the correct answers shown above in a character array named “correctAnswers” (In this assignment you are not doing any string manipulation, so the “Answer” array can be a simple character array, not a string). Then, it should ask the user to enter the student’s answers for each of the 10 questions, and the answers should be stored in another array named “studentAnswers”. In order to pass the exam a student must answer at least 8 questions out of 10. So, the “grade” function checks the entries in both array and determine if the student has answered at least 8 questions correctly. If student has passed the exam, your program should display the following message Congratulations! You have passed exam. Total number of correct answers: < > Total number of incorrect answers: < > If student has failed the exam, your program should display the following message Sorry, you have not passed the exam! Total number of correct answers: < > Total number of incorrect answers: < > Input validation: Only accept the letters A, B, C, or D as answers. If user enters any other letter, your program should display a message indicating A, B, C, and D are the only valid inputs. If user enters invalid input more than three times display the message “ GOOD BYE “ and exit the program
Explanation / Answer
Question 1
int n[] = new int[10];
double d[]= {1.0, 34.6, 7.8, 8.9, 0};
String s[] = new String[5];
Question 2:
LicenseExam.java
public class LicenseExam {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
char correctAnswers[] = {'B', 'A','D','B','A','A','A','C','C','D'};
char studentAnswers[] = new char[10];
java.util.Scanner in = new java.util.Scanner(System.in);
int invalidEntryCounter = 0;
int quesCounter = 0;
while(true){
System.out.println("Please enter your Answer (A, B, C, and D are the only valid inputs)");
char c = in.next().charAt(0);
if(c == 'A' || c == 'a' || c == 'B' || c == 'b' || c == 'C' || c == 'c' || c == 'D' || c == 'd'){
studentAnswers[quesCounter] = c;
quesCounter++;
}
else{
invalidEntryCounter++;
}
if(invalidEntryCounter == 3){
System.out.println("GOOD BYE");
break;
}
if(quesCounter == 10){
grade(correctAnswers, studentAnswers);
break;
}
}
}
public static void grade(char[] correctAnswers, char[] studentAnswers){
int match = 0;
int nonmatch = 0;
for(int i=0; i<correctAnswers.length; i++){
if(correctAnswers[i] == Character.toUpperCase(studentAnswers[i])){
match++;
}
else{
nonmatch++;
}
}
if(match >= 8){
System.out.println("Congratulations! You have passed exam");
System.out.println("Total number of correct answers: "+match);
System.out.println("Total number of incorrect answers: "+nonmatch);
}
else{
System.out.println("Sorry, you have not passed the exam! ");
System.out.println("Total number of correct answers: "+match);
System.out.println("Total number of incorrect answers: "+nonmatch);
}
}
}
Output:
Please enter your Answer (A, B, C, and D are the only valid inputs)
d
Please enter your Answer (A, B, C, and D are the only valid inputs)
d
Please enter your Answer (A, B, C, and D are the only valid inputs)
d
dPlease enter your Answer (A, B, C, and D are the only valid inputs)
dPlease enter your Answer (A, B, C, and D are the only valid inputs)
Please enter your Answer (A, B, C, and D are the only valid inputs)
d
Please enter your Answer (A, B, C, and D are the only valid inputs)
d
dPlease enter your Answer (A, B, C, and D are the only valid inputs)
d
Please enter your Answer (A, B, C, and D are the only valid inputs)
d
Please enter your Answer (A, B, C, and D are the only valid inputs)
d
Sorry, you have not passed the exam!
Total number of correct answers: 2
Total number of incorrect answers: 8
Output:
Please enter your Answer (A, B, C, and D are the only valid inputs)
b
Please enter your Answer (A, B, C, and D are the only valid inputs)
a
Please enter your Answer (A, B, C, and D are the only valid inputs)
d
Please enter your Answer (A, B, C, and D are the only valid inputs)
b
Please enter your Answer (A, B, C, and D are the only valid inputs)
a
Please enter your Answer (A, B, C, and D are the only valid inputs)
a
Please enter your Answer (A, B, C, and D are the only valid inputs)
a
Please enter your Answer (A, B, C, and D are the only valid inputs)
c
Please enter your Answer (A, B, C, and D are the only valid inputs)
c
Please enter your Answer (A, B, C, and D are the only valid inputs)
a
Congratulations! You have passed exam
Total number of correct answers: 9
Total number of incorrect answers: 1
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.