Description
*8.1 Write a method that returns the sum of all the elements in a specified column in a m(Sum
elements column by column) matrix using the following header:
public static double sumColumn(double[][] m, int columnIndex)
Write a test program that reads a 3-by-4 matrix and displays the sum of each column. Here is a
sample run:
Listing 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 = {
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’};
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 }
*8.3 (SORT STUDENTS ON GRADES) Rewrite Listing 8.2, GradeExam.java, to display the
students in increasing order of the number of correct answers.
**8.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 employees and their total hours in
decreasing order of the total hours.
*8.16 (SORT TWO-DIMENSIONAL ARRAY) Write a method to sort a two-dimensional
array using the following header:
public static void sort(int m[][])
The method performs a primary sort on rows, and a secondary sort on columns. For example, the
following array
{{4, 2},{1, 7},{4, 5},{1, 2},{1, 1},{4, 1}}
will be sorted to
{{1, 1},{1, 2},{1, 7},{4, 1},{4, 2},{4, 5}}.