Java Reference
In-Depth Information
32 }
33
34
return
total;
35 }
36 }
Enter 3 rows and 4 columns:
1 2 3 4
5 6 7 8
9 10 11 12
Sum of all elements is 78
The method
getArray
prompts the user to enter values for the array (lines 11-24) and
returns the array (line 23).
The method
sum
(lines 26-35) has a two-dimensional array argument. You can obtain the
number of rows using
m.length
(line 28) and the number of columns in a specified row
using
m[row].length
(line 29).
7.7
✓
✓
Show the printout of the following code:
Check
Point
public class
Test {
public static void
main(String[] args) {
int
[][] array = {{
1
,
2
,
3
,
4
}, {
5
,
6
,
7
,
8
}};
System.out.println(m1(array)[
0
]);
System.out.println(m1(array)[
1
]);
}
public static int[]
m1(
int
[][] m) {
int
[] result =
new int
[
2
];
result[
0
] = m.length;
result[
1
] = m[
0
].length;
return
result;
}
}
The problem is to write a program that will grade multiple-choice tests.
Key
Point
Suppose you need to write a program that grades multiple-choice tests. Assume there are
eight students and ten questions, and the answers are stored in a two-dimensional array. Each
row records a student's answers to the questions, as shown in the following array.
VideoNote
Grade multiple-choice test
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