Java Reference
In-Depth Information
String firstName;
String lastName;
Scanner inFile =
new Scanner( new FileReader("test.txt"));
//Step 2
PrintWriter outFile = new
PrintWriter("testavg.out");
//Step 3
firstName = inFile.next();
//Step 4
lastName = inFile.next();
//Step 4
outFile.println("Student Name: "
+ firstName + " " + lastName);
//Step 5
//Step 6 - retrieve the five test scores
test1 = inFile.nextDouble();
test2 = inFile.nextDouble();
test3 = inFile.nextDouble();
test4 = inFile.nextDouble();
test5 = inFile.nextDouble();
outFile.printf("Test scores: %5.2f %5.2f %5.2f "
+ "%5.2f %5.2f %n", test1, test2,
test3, test4, test5);
//Step 7
average = (test1 + test2 + test3 + test4
+ test5) / 5.0;
//Step 8
outFile.printf("Average test score: %5.2f %n",
average);
//Step 9
inFile.close();
//Step 10
outFile.close();
//Step 10
}
}
Sample Run:
Input File (contents of the file test.txt ):
Andrew Miller 87.50 89 65.75 37 98.50
Output File (contents of the file testavg.out ):
Student Name: Andrew Miller
Test scores: 87.50 89.00 65.75 37.00 98.50
Average test score: 75.55
Theprecedingprogramusesfivevariables: test1 , test2 , test3 , test4 ,and test5 to
read the five test scores and then find the average test score. The Additional Student Files
folder at www.cengagebrain.com contains a modified version of this program that uses only
one variable, testscore , to read the test scores and another variable, sum ,tofindthesum
of the test scores. The program is named StudentGradeVersion2.java .
 
Search WWH ::




Custom Search