Java Reference
In-Depth Information
Enter a 4-by-4 matrix row by row:
1 2 3 4.0
5 6.5 7 8
9 10 11 12
13 14 15 16
Sum of the elements in the major diagonal is 34.5
*7.3
(
Sort students on grades
) Rewrite Listing 7.2, GradeExam.java, to display the stu-
dents in increasing order of the number of correct answers.
**7.4
(
Compute the weekly hours for each employee
) Suppose the weekly hours for all
employees are stored in a two-dimensional array. Each row records an employee's
seven-day work hours with seven columns. For example, the following array
stores the work hours for eight employees. Write a program that displays employ-
ees and their total hours in decreasing order of the total hours.
Su M T W Th F Sa
2434588
7343344
3343322
9347341
3543638
3446344
3748384
6359279
Employee 0
Employee 1
Employee 2
Employee 3
Employee 4
Employee 5
Employee 6
Employee 7
7.5
(
Algebra: add two matrices
) Write a method to add two matrices. The header of
the method is as follows:
public static double
[][] addMatrix(
double
[][] a,
double
[][] b)
In order to be added, the two matrices must have the same dimensions and the
same or compatible types of elements. Let
c
be the resulting matrix. Each element
is
c
ij
a
ij
+
b
ij
.
For example, for two
3
*
3
matrices
a
and
b
,
c
is
a
11
a
12
a
13
b
11
b
12
b
13
a
11
+
b
11
a
12
+
b
12
a
13
+
b
13
£
a
21
a
22
a
23
a
33
≥ + £
b
21
b
22
b
23
b
33
≥ = £
a
21
+
b
21
a
22
+
b
22
a
23
+
b
23
b
33
≥
VideoNote
a
31
a
32
b
31
b
32
a
31
+
b
31
a
32
+
b
32
a
33
+
Multiply two matrices
Write a test program that prompts the user to enter two
3
*
3
matrices and dis-
plays their sum. Here is a sample run:
1 2 3 4 5 6 7 8 9
Enter matrix1:
Enter matrix2:
The matrices are added as follows
1.0 2.0 3.0 0.0 2.0 4.0 1.0 4.0 7.0
4.0 5.0 6.0 + 1.0 4.5 2.2 = 5.0 9.5 8.2
7.0 8.0 9.0 1.1 4.3 5.2 8.1 12.3 14.2
0 2 4 1 4.5 2.2 1.1 4.3 5.2