Java Reference
In-Depth Information
EXAMPLE: (continued)
All the raw data is in the array grade , but two other arrays hold computed data. The
array studentAverage is used to record the average quiz score for each of the students.
For example, the program sets studentAverage[0] equal to the average of the quiz
scores received by student 1 , studentAverage[1] equal to the average of the quiz
scores received by student 2 , and so forth. The array quizAverage is used to record
the average score for each quiz. For example, the program sets quizAverage[0]
equal to the average of all the student scores for quiz 1 , quizAverage[1] records the
average score for quiz 2 , and so forth. Display 6.19 illustrates the relationship between
the arrays grade , studentAverage , and quizAverage . In that display, we have
shown some sample data for the array grade . The data in grade , in turn, determines
the values that are stored in studentAverage and in quizAverage . Display 6.19
also shows these computed values for studentAverage and quizAverage . The two
arrays studentAverage and quizAverage are created and filled by the constructor
that creates the GradeBook object. (The constructors do this by calling private
helping methods.)
The no-argument constructor for the class GradeBook obtains the data for the
array instance variable grade via a dialog with the user. Although this is not my
favorite way to define a no-argument constructor, some programmers like it and you
should see an example of it. Another alternative would be to have a no-argument
constructor that essentially does nothing and then have an input method that sets all
the instance variables, including creating the array objects.
A very simple demonstration program along with the dialog it produces is given in
Display 6.20 .
Display 6.18
A Grade Book Class (part 1 of 4)
1 import java.util.Scanner;
2 public class GradeBook
3 {
4 private int numberOfStudents; // Same as studentAverage.length.
5 private int numberOfQuizzes; // Same as quizAverage.length.
6 private int [][] grade; //numberOfStudents rows and numberOfQuizzes
//columns.
7 private double [] studentAverage;
8 private double [] quizAverage;
9 public GradeBook( int [][] a)
10 {
11 if (a.length == 0 || a[0].length == 0)
12 {
13 System.out.println("Empty grade records. Aborting.");
14 System.exit(0);
15 }
(continued)
Search WWH ::




Custom Search