Prof. I.  Rudowsky                                                                                                                         CIS 26MW12

Homework 4                                                                                                                   Due 18-Oct-2004

_____________________________________________________________________________

Four students took exams and received these grades. 

 

Student

Exam 1

Exam 2

Exam 3

Exam 4

Sheila

77

68

86

73

Frank

96

87

92

 

Mark

70

90

86

81

Julie

100

99

 

 

Initialize a double-subscripted array grades with these grades, where the student did not take a test there is no entry in the array (do not use a -1 or any other special number to represent that test). Initialize a one-dimensional String array studentNames to hold the names of the students.

 

(a) Print out a line for each student showing

*       the student name

*       the grade for each exam taken by that student

*       the number of tests taken by that student

*       the student’s average

Write a method calcAvg to compute the average grade for each student based on how many exams the student took. It accepts a one-dimensional array containing the grades for a student and returns the average e.g., calcAvg(grades[0]) returns Sheila’s average.

 

(b) Print a line for each exam showing

*       the exam number

*       the number of students who took that exam

*       the average grade for that exam

 

Write a method calcExamAvg that has two parameters - a two dimensional array and an integer column number for a specific exam - and returns the average of that exam. For example, calcExamAvg(grades,0) returns the average for exam 1.

 

Note that you must check that an exam exists for that column by comparing the column number with the length of the array for that column. For example, Frank has only three exams so the length of grades[1] is 3  - the column values run from 0 to 2. There is no value for grades[1][3] – if you try to access it you will get an exception condition and the program will terminate. Thus, you must check each row to make sure the column you want to access exists. If it does not exist, skip it and go on to the next row.

 

(c) Print out the highest and lowest grade for all 13 exams. Write a method calcHighLow that accepts the entire double-subscripted array as a parameter and prints out the highest and lowest test score across all students. Do this as follows within calcHighLow.

(1) Compute the total length of all rows of grades and store it in totLen. Use that number to declare a one dimensional array named allGrades whose size is totLen. Do not hardcode the size of allGrades to be 13.

(2) Use arraycopy to copy each row of grades into adjacent portions of allGrades. For example, grades[0] starts at position 0 of allGrades and ends at position 3 (one less than it’s length). grades[1] starts at position 4 which is the length of grades[0]; grades[2] starts at position 7 (sum of the length of grades[0] and grades[1]) and grades[3] starts at position 11. Do not hardcode the starting positions, use a loop indexing through the rows of grades and using the length of all previous rows to tell arraycopy where to start.

(3) Use the sort method of class Arrays to sort allGrades into ascending order.

(4) Print the lowest grade from index 0 and the highest grade from the last position of allGrades