Write a program to calculate students\' average test scores and their grades. Yo
ID: 3688906 • Letter: W
Question
Write a program to calculate students' average test scores and their grades. You may assume the following input data: 13. Johnson 85 83 77 91 76 Aniston 80 90 95 93 48 Cooper 78 81 11 90 73 Gupta 92 83 30 69 87 9 2 9 7 201 S Cagy Lern ng. All Right. Roevat My iew has deemed tha any sippressed content dees not maerially afflect the overall lesming experience Cengage Leaming eserves the right to remove additional conient at any time if s be copio4 Kan d. or d pliaed . whole or in pat. Due to e la tron ie fights, some thind pity caelant may be suppessed from th. elk ok or rights estr 606 I Chapter 8: Arrays and Strings Blair 23 45 96 38 59 Clark 60 85 45 39 67 Kennedy 77 31 52 74 83 Bronson 93 94 89 77 97 Sunny 79 85 28 93 82 Smith 85 72 49 75 63 8 5 6 2 9 3 2 9 2 5 7 5Explanation / Answer
import java.util.*;
import java.io.*;
import javax.swing.*;
public class testAverageIO
{
// Static variables
static double average, grade, sum;
static int count;
static double[] gradeArray;
static String outputfileName, inputfileName, name, gradeLetter, outputStr;
public static void main(String[] args)
throws FileNotFoundException
{
// Asking user for the input and output file names.
inputfileName = JOptionPane.showInputDialog("Please enter the name of the file
you would like to open. (c:\inputFile.txt)");
outputfileName = JOptionPane.showInputDialog("Please enter hte name of the file
you would like to output to. (c:\outputFile.txt)");
// inFile and outFile objects.
PrintWriter outFile = new PrintWriter(outputfileName);
Scanner inFile = new Scanner(new FileReader(inputfileName));
// Printing the column headers in the output text file.
outFile.printf("%8s %8s %8s %8
s %8s %8s %8s %8s %n", "Student", "Test1",
"Test2", "Test3", "Test4", "Test5", "Average", "Grade");
double[] gradeArray = new double[5];
while (inFile.hasNext())
// While loop reads input from a file
// as long as there is information to read.
{
name = inFile.next(); // Gets the name of the student
sum = 0;
count = 0;
while (count != 5)
// While loop creates the test score array
// and also the sum of grades for a single student.
{
grade = inFile.nextDouble();
gradeArray[count] = grade;
sum = sum + grade;
count++;
}
calculateAverage();
// Prints a single student's information before continuing to loop.
outFile.printf("%8s %8.2f
%8.2f %8.2f %8.2f %8.2f %8.2f %8s %n", name,
gradeArray[0], gradeArray[1],
gradeArray[2], gradeArray[3]
, gradeArray[4], average,
calculateGrade(average));
}
inFile.close();
outFile.close();
}
public static void calculateAverage()
// Divides the sum by 5, which is the count
// to arrive at the average.
{
if (count != 0)
{
average = sum / count;
}
else
{
average = 0;
}
}
public static String calculateGrade(double average)
// calculateGrade() method determines the grade letter
// to be attributed with the average.
{
String gradeLetter = "";
if (average >= 90 && average <= 100)
{
gradeLetter = "A";
}
else if (average >= 80 && average <= 89)
{
gradeLetter = "B";
}
else if (average >= 70 && average <= 79)
{
gradeLetter = "C";
}
else if (average >= 60 && average <= 69)
{
gradeLetter = "D";
}
else if (average >= 0 && average <= 59)
{
gradeLetter = "F";
}
return gradeLetter;
}// ... End of calculateGrade()
}// ... End of testAverageIO()
The Sample Input
Johnson 85 83 77 91 76
Aniston 80 90 95 93 48
Cooper 78 81 11 90 73
Gupta 92 83 30 69 87
Blair 23 45 96 38 59
Clark 60 85 45 39 67
Kennedy 77 31 52 74 83
Bronson 93 94 89 77 97
Sunny 79 85 28 93 82
Smith 85 72 49 75 63
The Sample Output
Name Test1 Test2 Test3 Test4 Test5 Average Grade
Johnson 85 83 77 91 76 82.4 B
Aniston 80 90 95 93 48 81.2 B
Cooper 78 81 11 90 73 66.6 D
Gupta 92 83 30 69 87 72.2 C
Blair 23 45 96 38 59 52.2 F
Clark 60 85 45 39 67 59.2 F
Kennedy 77 31 52 74 83 63.4 D
Bronson 93 94 89 77 97 90 A
Sunny 79 85 28 93 82 73.4 C
Smith 85 72 49 75 63 68.8 D
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.