Write a Java program that reads a student’s name together with his or her test s
ID: 3680409 • Letter: W
Question
Write a Java program that reads a student’s name together with his or her test scores. The program should then compute the average test score for each student. The grade scale is as follows: 90-100, A; 80-89, B; 70-79, C; 60-69, D: 0-59, F.
Your program must use the following methods:
a.A value-returning method, calculateAverage (), to determine and return theaverage of five test scores for each student. Use a loop to read and sum the five testscores. (this method does not output the average test score. That task must be done in themethod main. Please see the template code for explanation)
b.A value-returning method, calculateGrade (…), to determine and return eachstudent’s grade. (This method does not output the grade. That task must be done in themethod main. Please see the template code)
Test your program on the following data. Read the data from a file Assign5Input.txt (Note: download the compressed file in D2L and unzip it to the same directory of your program) and send the out to a file Assign5Output.txt (this is a new file generated from your program after running). Do not use any global variables. Use the appropriate parameters to pass values in and out of methods.
Sample Run: (download the template LastnameA5.java in D2L and then add your code) Input File (contents of the file Assign5Input.txt):
Johnson 85 83 77 91 75
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
Output File (contents of the file Assign5Output.txt):
Student Test1 Test2 Test3 Test4 Test5 Average Grade
Johnson 85 83 77 91 76 82.40 B
Aniston 80 90 95 93 48 81.20 B
Cooper 78 81 11 90 73 66.60 D
Gupta 92 83 30 69 87 72.20 C
Blair 23 45 96 38 59 52.20 F
Clark 60 85 45 39 67 59.20 F
Kennedy 77 31 52 74 83 63.40 D
Bronson 93 94 89 77 97 90.00 A
Sunny 79 85 28 93 82 73.40 C
Smith 85 72 49 75 63 68.80 D
Class Average = 70.94
Explanation / Answer
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.Scanner;
public class LastnameA5 {
/**
* @param testScores
* @return
*/
public static double calculateAverage(int testScores[]) {
double average, sum = 0;
for (int i = 0; i < testScores.length; i++)
sum += testScores[i];
average = sum / (double) testScores.length;
return average;
}
/**
* @param average
* @return
*/
public static char calculateGrade(double average) {
char grade;
if (average >= 90 && average <= 100)
grade = 'A';
else if (average >= 80 && average <= 89)
grade = 'B';
else if (average >= 70 && average <= 79)
grade = 'C';
else if (average >= 60 && average <= 69)
grade = 'D';
else
grade = 'E';
return grade;
}
public static void main(String[] args) {
Scanner scanner = null;
try {
File file = new File("Assign5Output.txt");
scanner = new Scanner(new File("Assign5Input.txt"));
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
int count = 0;
double classAverage = 0.0;
bw.write("Student Test1 Test2 Test3 Test4 Test5 Average Grade ");
while (scanner.hasNext()) {
String name = scanner.next();
int testScores[] = new int[5];
String line = name;
for (int i = 0; i < 5; i++) {
testScores[i] = scanner.nextInt();
line += " " + testScores[i];
}
double average = calculateAverage(testScores);
classAverage += average;
count++;
char grade = calculateGrade(average);
line += " " + average + " " + grade;
bw.write(line + " ");
}
bw.write("Class Average =" + (classAverage / (double) count));
bw.close();
} catch (Exception e) {
// TODO: handle exception
} finally {
scanner.close();
}
}
}
Assign5Input.txt
Johnson 85 83 77 91 75
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
Assign5Output.txt
Student Test1 Test2 Test3 Test4 Test5 Average Grade
Johnson 85 83 77 91 75 82.2 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 E
Clark 60 85 45 39 67 59.2 E
Kennedy 77 31 52 74 83 63.4 D
Bronson 93 94 89 77 97 90.0 A
Sunny 79 85 28 93 82 73.4 C
Smith 85 72 49 75 63 68.8 D
Class Average =70.92
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.