Java Reference
In-Depth Information
int
numberOfCourses;
double
avg1;
//average for a course in group 1
double
avg2;
//average for a course in group 2
double
avgGroup1;
double
avgGroup2;
Scanner group1 =
new
Scanner(
new
FileReader("group1.txt"));
Scanner group2 =
new
Scanner(
new
FileReader("group2.txt"));
PrintWriter outfile =
new
PrintWriter("student.out");
Next, we discuss the methods
calculateAverage
and
printResult
. Then, we
will put the method
main
together.
This method calculates the average for a course. Because the input is stored in a file
and the input file is opened in the method
main
, we must pass the variable associated
with the input file to this method. Furthermore, after calculating the course average,
this method must pass the course average to the method
main
. Therefore, this
method has one parameter.
To find the course average, we must first find the sum of all the scores for the
course and the number of students who took the course; we then divide the sum
by the number of students. Thus, we need a variable to find the sum of the scores,
a variable to find the number of students, a variable to find the course average, and
a variable to read and store a score. Of course, we must initialize the variables
to zero to find the sum and the number of students.
Method
calculate
Average
7
In the previous discussion of data manipulation, we identified four variables for the
method
calculateAverage
:
Local
Variables
(Method
calculate
Average)
double
totalScore;
//to store the sum of scores
int
numberOfStudents;
//to store the number of students
int
score;
//to read and store a course score
double
courseAvg;
//to store the course average
The preceding discussion translates into the following algorithm for the method
calculateAverage
:
a. Declare the variables.
b. Initialize
totalScore
to
0.0
.
c. Initialize
numberOfStudents
to
0
.
d. Get the (next) course score.
Search WWH ::
Custom Search