Class Average - Java While Loop Sample Program

 Suppose we are given a file consisting of students’ names and their test scores, a number
between 0 and 100 (inclusive). Each line in the file consists of a student name followed by
the test score. We want a program that outputs each student’s name followed by the test
score and the grade. The program also needs to output the average test score for the class.
Consider the following program.

// This program reads data from a file consisting of students'
// names and their test scores. The program outputs each
// student's name followed by the test score and the grade. The
// program also outputs the average test score for all students.

import java.io.*; //Line 1
import java.util.*; //Line 2

public class ClassAverage //Line 3
{ //Line 4
public static void main(String[] args) throws FileNotFoundException //Line 5
{ //Line 6
String firstName; //Line 7
String lastName; //Line 8

double testScore; //Line 9
char grade = ' '; //Line 10
double classAverage; //Line 11
double sum = 0; //Line 12
int count = 0; //Line 13

Scanner inFile = new Scanner(new FileReader("stData.txt")); //Line 14
PrintWriter outFile = new PrintWriter("stData.out"); //Line 15

while (inFile.hasNext()) //Line 16
{ //Line 17
firstName = inFile.next();//read the first name Line 18
lastName = inFile.next(); //read the last name Line 19
testScore = inFile.nextDouble(); //read the test score Line 20
sum = sum + testScore; //update sum Line 21
count++; //increment count Line 22

//determine the grade
switch ((int) testScore / 10) //Line 23
{ //Line 24
case 0: //Line 25
case 1: //Line 26
case 2: //Line 27
case 3: //Line 28
case 4: //Line 29
case 5: //Line 30
grade = 'F'; //Line 31
break; //Line 32
case 6: //Line 33
grade = 'D'; //Line 34
break; //Line 35
case 7: //Line 36
grade = 'C'; //Line 37
break; //Line 38
case 8: //Line 39
grade = 'B'; //Line 40
break; //Line 41
case 9: //Line 42
case 10: //Line 43
grade = 'A'; //Line 44
break; //Line 45
default: //Line 46
System.out.println("Invalid score."); //Line 47
}//end switch //Line 48

outFile.printf("%-12s %-12s %4.2f %c %n",firstName, lastName,testScore, grade); //Line 49
}//end while //Line 50

outFile.println(); //Line 51

if (count != 0) //Line 52
outFile.printf("Class Average: %.2f %n",  sum / count); //Line 53
else //Line 54
outFile.println("No data."); //Line 55
outFile.close(); //Line 56
} //Line 57
} //Line 58

Sample Output:
****************************************
Input File:
Steve Gill 89
Rita Johnson 91.5
Randy Brown 85.5
Seema Arora 76.5
Samir Mann 73
Samantha McCoy 88.5
Output File:
Steve Gill 89.00 B
Rita Johnson 91.50 A
Randy Brown 85.50 B
Seema Arora 76.50 C
Samir Mann 73.00 C
Samantha McCoy 88.50 B
Class Average: 84.00
****************************************

 The preceding program works as follows. The statements in Lines 7 to 11 declare
variables required by the program. The statements in Lines 12 and 13 initialize the
variables sum and count. The statement in Line 14 declares inFile to be a reference
variable of type Scanner and associates it with the input file. The statement in Line 15
declares outFile to be a reference variable of type PrintWriter and associates it with
the output file.

 The while loop from Lines 16 to 50 reads each student’s first name, last name, and test
score, and outputs the name followed by the test score and grade. Specifically, the
statement in Line 18 reads the first name, the statement in Line 19 reads the last name,
and the statement in Line 20 reads the test score. The statement in Line 21 updates the
value of sum. (After reading all the data, the value of sum stores the sum of all the test
scores.) The statement in Line 22 updates the value of count. (The variable count stores
the number of students in the class.) The switch statement from Lines 23 to 48
determines the grade from testScore and stores it in the variable grade.

The statement in Line 49 outputs a student’s first name, last name, test score, and grade.
The if...else statement in Lines 52 to 55 outputs the class average, and the statement
in Line 56 closes the file associated with outFile, which is stData.out.

No comments: