Java Reference
In-Depth Information
Students' Answers to the Questions:
0 1 2 3 4 5 6 7 8 9
Student 0
A B A C C D E E A D
Student 1
D B A B C A E E A D
Student 2
E D D A C B E E A D
Student 3
C B A E D C E E A D
Student 4
A B D C C D E E A D
Student 5
B B E C C D E E A D
Student 6
B B A C C D E E A D
Student 7
E B E C C D E E A D
The key is stored in a one-dimensional array:
Key to the Questions:
0 1 2 3 4 5 6 7 8 9
Key D B D C C D A E A D
Your program grades the test and displays the result. It compares each student's answers
with the key, counts the number of correct answers, and displays it. Listing 8.2 gives the
program.
L ISTING 8.2
GradeExam.java
1 public class GradeExam {
2
/** Main method */
3
public static void main(String[] args) {
4
// Students' answers to the questions
5
char [][] answers = {
2-D array
6
{ 'A' , 'B' , 'A' , 'C' , 'C' , 'D' , 'E' , 'E' , 'A' , 'D' },
7
{ 'D' , 'B' , 'A' , 'B' , 'C' , 'A' , 'E' , 'E' , 'A' , 'D' },
8
{ 'E' , 'D' , 'D' , 'A' , 'C' , 'B' , 'E' , 'E' , 'A' , 'D' },
9
{ 'C' , 'B' , 'A' , 'E', 'D' , 'C' , 'E' , 'E' , 'A' , 'D' },
10
{ 'A' , 'B' , 'D' , 'C' , 'C' , 'D' , 'E' , 'E' , 'A' , 'D' },
11
{ 'B' , 'B' , 'E' , 'C' , 'C' , 'D' , 'E' , 'E' , 'A' , 'D' },
12
{ 'B' , 'B' , 'A' , 'C' , 'C' , 'D' , 'E' , 'E' , 'A' , 'D' },
13
{ 'E' , 'B' , 'E' , 'C' , 'C' , 'D' , 'E' , 'E' , 'A' , 'D' }};
14
15
// Key to the questions
16
char [] keys = { 'D' , 'B' , 'D' , 'C' , 'C' , 'D' , 'A' , 'E' , 'A' , 'D' };
1-D array
17
18 // Grade all answers
19 for ( int i = 0 ; i < answers.length; i++) {
20 // Grade one student
21 int correctCount = 0 ;
22 for ( int j = 0 ; j < answers[i].length; j++) {
23 if (answers[i][j] == keys[j])
24 correctCount++;
25 }
26
27 System.out.println( "Student " + i + "'s correct count is " +
28 correctCount);
29 }
30 }
31 }
compare with key
 
 
Search WWH ::




Custom Search