Java Reference
In-Depth Information
e. while (score != -999)
i. Update totalScore by adding the course score read in
Step d.
ii. Increment numberOfStudents by 1 .
iii. Get the next course score.
f. courseAvg = totalScore / numberOfStudents;
g. return courseAvg;
We are now ready to write the definition of the method calculateAverage.
public static double calculateAverage(Scanner inp)
{
double totalScore = 0.0;
int numberOfStudents = 0;
int score = 0;
double courseAvg;
score = inp.nextInt();
while (score != -999)
{
totalScore = totalScore + score;
numberOfStudents++;
score = inp.nextInt();
} //end while
courseAvg = totalScore / numberOfStudents;
return courseAvg;
} //end calculate Average
The method printResult prints the group's course ID, group number, and course
average. The output is stored in a file. We must pass four parameters to this method:
the variable associated with the output file, the group number, the course ID, and the
course average for the group. Also, from the output, it is clear that we print the
course ID only before group 1. In pseudocode, the algorithm is:
Method
printResult
if (group number == 1)
print course ID
else
print a blank
print group number and course average
The definition of the method printResult follows:
public static void printResult(PrintWriter outp, String courseId,
int groupNo, double avg)
 
Search WWH ::




Custom Search