import java.io.*; // needed for FileReader and PrintWriter classes and FileNotFoundException import java.util.*; // needed for the Scanner class public class ClassAverage { static final int numTests=5; // number of tests for each student (5 from file) static final int numStudents=10; // number of students in the class (10 from file) static double totalScore=0; // holds the total score for a student static double classAverage=0; // holds the class average static int studentAverages[]=new int[numStudents]; // holds each student's 5 test scores // the method below reads in our test scores from the input file and calculates the student and class averages public static void calculateAverage(Scanner inFile, ClassAverage classAvgObj) { double score=0; // holds each test score for a student classAvgObj.totalScore=0; // reset totalScore to 0 for each new student read in before looping again for(int i=0;i=90) // if the score is greater than or equal to 90 then they get an A grade='A'; else if(finalScore>=80 && finalScore<=89) // if between 80 and 89, they get a B grade='B'; else if(finalScore>=70 && finalScore<=79) // if between 70 and 79, they get a C grade='C'; else if(finalScore>=60 && finalScore<=69) // if between 60 and 69, they get a D grade='D'; else // anything below that is a failing grade... (F) grade='F'; return grade; // return the grade character to the caller } public static void main(String args[]) throws FileNotFoundException { String studentName="John Doe"; // holds the student name from the input file //Double classAverage=new Double(0.0); // holds the average score of the entire class ClassAverage classAvgObj=new ClassAverage(); // instance a new object from our ClassAverage class Scanner gradeFile=new Scanner(new FileReader("gradebook.dat")); // our input file from current working directory PrintWriter outFile=new PrintWriter("output.txt"); // our output file to current working directory // below we will output equivalent information to both the screen and to a file for later review outFile.println("Student\t\tTest1\tTest2\tTest3\tTest4\tTest5\tAverage\tGrade"); System.out.println("Student\t\tTest1\tTest2\tTest3\tTest4\tTest5\tAverage\tGrade"); while(gradeFile.hasNext()) // keep going until we read our entire input file { studentName=gradeFile.next(); // read in the next student name from the input file calculateAverage(gradeFile, classAvgObj); // jump to this method to read in all test scores outFile.println(studentName + "\t\t" + String.format("%.2f", (classAvgObj.totalScore/numTests)) + "\t" + calculateGrade(classAvgObj.totalScore/numTests)); System.out.println(studentName + "\t\t" + classAvgObj.studentAverages[0] + "\t" + classAvgObj.studentAverages[1] + "\t" + classAvgObj.studentAverages[2] + "\t" + classAvgObj.studentAverages[3] + "\t" + classAvgObj.studentAverages[4] + "\t" + String.format("%.2f", (classAvgObj.totalScore/numTests)) + "\t" + calculateGrade(classAvgObj.totalScore/numTests)); } // end while gradeFile.close(); // close the input file since we are done reading from it // finish with the footer of the report which displays the class average outFile.println("\nClass Average = " + String.format("%.2f", (classAvgObj.classAverage/numStudents))); System.out.println("\nClass Average = " + String.format("%.2f", (classAvgObj.classAverage/numStudents))); outFile.close(); // also close the output file since we are done writing to it } }