For Java: Grading Schema: In a separate function, you will implement a grading s
ID: 3817602 • Letter: F
Question
For Java:
Grading Schema: In a separate function, you will implement a grading schema. Write a program that reads a student’s name together with his or her test score from a file given by the user. The first two values in the file will represent the number of students followed by the number of tests. The program should then compute the average test score for each student and assign the appropriate grade (A, B, C, D, E or F) as well as the average of each test. Your program must perform the following functions. a) A void function calculateAverage, to determine the average of the test scores for each student. b) A value-returning function, calculateGrade, to determine and return each student’s grade letter. c) A void function calculateTestAvg that calculates the average of all tests and overall average of students. d) A void function printGrades that prints the grades values, average marks and grade letter followed by the average of all tests and students.
Output:
This is what I have so far:
package readwritetofile;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
/**
*
* @author Jarvis
*/
public class Grades {
private static final String FILENAME2 = "/Users/Jarvis/Documents/Students.txt";
public static void main(String[] args) {
BufferedReader br = null;
FileReader fr = null;
String students [] = new String[10];
int [][] grades = new int [10][5];
String[] words;
String sCurrentLine;
int counter=0;
int [] tests = new int [10];
System.out.printf("Students Test1 Test2 Test3 Test4 test5 Average ");
try {
fr = new FileReader(FILENAME2);
br = new BufferedReader(fr);
br = new BufferedReader(new FileReader(FILENAME2));
while ((sCurrentLine = br.readLine()) != null) {
words = sCurrentLine.split(" ");
students[counter]=words[0];
System.out.printf(students[0]+ " ");
for (int i=1; i < words.length; i++){
grades[counter][i-1]=Integer.parseInt(words[i]);
// System.out.print(words[i]+" ");
} //end of outer for loop
} // end of while loop
} catch (IOException e) {
e.printStackTrace();
} // end of catch
counter++;
}//end of main
public static void calculateAverage (int [] grades){
ArrayList<Double> averageGrades = new ArrayList<>();
int sum = 0;
for (int i=0; i < grades.length; i++) {
sum += grades[i];
// Divides the sum of the numbers by the number of grades
averageGrades.add((sum/(double)grades.length));
} // end of inner for loop
for(int j = 0; j < grades.length; j++) {
System.out.print(grades[j]+" ");
} // end of for loop
System.out.println(averageGrades.get(4));
} // end of calculateAverage()
public static double calculateGrade(double grade) {
if (grade >=0 && grade <60) {
return 'F';
} else if (grade >= 60 && grade < 70){
return 'D';
} else if (grade >=70 && grade < 80) {
return 'C';
} else if (grade >=80 && grade < 90) {
return 'B';
} else if (grade >=90 && grade < 100)
return 'A';
return grade;
} // end of calculateGrade method
public static void calculateTestAverage (int students [][]){
int sumTotal = 0;
for (int i=0; i < students.length; i++) {
sumTotal += students[i][0];
System.out.print(sumTotal);
}
} // end of calculateTestAverage method
public static void printGrades () {
} // end of method printGrades
} // end of grades
output Assignment2 (run) test1 test2 test3 test4 test5 Avg grade 91 67 84 50 69 72 tom. 74 78 58 62 64 67 Suzy Peter 55 95 81 77 61 73 Paul. 91 95 92 77 86 88 Diane 91 54 52 53 92 68 Emily 82 71 66 68 95 76 Natalie 97 76 71 88 69 80 Ben 62 67 99 85 94 81 ark 53 61 72 83 73 68 Anna 64 91 61 53 68 67 test AVG: 76 75 73 69 77 74 BUILD SUCCESSFUL (total time: 0 secondsExplanation / Answer
Hi,
Please see below the updatedclass andinput/output samples.Please comment for any queries/feedbacks.
Thanks,
Anita
Grades.java
package readwritetofile;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
/**
*
* @author Jarvis
*/
public class Grades {
private static final String FILENAME2 = "/Users/Jarvis/Documents/Students.txt";
private static char [] grades = new char [10];
private static Double [] avgmarks = new Double [10];
private static String [] students = new String [10];
public static void main(String[] args) {
BufferedReader br = null;
FileReader fr = null;
String[] words;
String sCurrentLine;
int counter=0;
int countLine =0;
Integer noOfStudents =0;
Integer noOfTests =0;
int [] tests = new int [10];
int [][] marks = new int [10][5];
try {
fr = new FileReader(FILENAME2);
br = new BufferedReader(fr);
br = new BufferedReader(new FileReader(FILENAME2));
while ((sCurrentLine = br.readLine()) != null) {
if(countLine ==0 ){
//No of students
noOfStudents = new Integer(sCurrentLine);
countLine++;
continue;
}
else if(countLine ==1){
//No of tests
noOfTests = new Integer(sCurrentLine);
countLine++;
continue;
}
else{
words = sCurrentLine.split(" ");
students[counter]=words[0];
for (int i=1; i < words.length; i++){
marks[counter][i-1]=Integer.parseInt(words[i]);
// System.out.print(words[i]+" ");
} //end of outer for loop
counter++;
countLine++;
}
} // end of while loop
} catch (IOException e) {
e.printStackTrace();
} // end of catch
//Calculating the Average
for(int i=0;i<marks.length; i++){
calculateAverage(marks[i], i);
}
printGrades(marks);
}//end of main
public static void calculateAverage (int [] marks,int index){
Double averageMarks =null;
int sum = 0;
for (int i=0; i < marks.length; i++) {
sum += marks[i];
} // end of inner for loop
// Divides the sum of the numbers by the number of grades
averageMarks = sum/(double)marks.length;
avgmarks[index] = averageMarks;
double grade = calculateGrade(averageMarks, index);
} // end of calculateAverage()
public static char calculateGrade(double averageMarks,int index) {
char grade = 0;
if (averageMarks >=0 && averageMarks <60) {
grade = 'F';
} else if (averageMarks >= 60 && averageMarks < 70){
grade= 'D';
} else if (averageMarks >=70 && averageMarks < 80) {
grade = 'C';
} else if (averageMarks >=80 && averageMarks < 90) {
grade= 'B';
} else if (averageMarks >=90 && averageMarks < 100) {
grade= 'A';
}
grades[index] = grade;
return grade;
} // end of calculateGrade method
public static void calculateTestAverage (int students [][]){
int sumTotal = 0;
for (int i=0; i < students.length; i++) {
sumTotal += students[i][0];
System.out.print(sumTotal);
}
} // end of calculateTestAverage method
public static void printGrades (int [][] marks) {
System.out.printf("Students Test1 Test2 Test3 Test4 test5 Average Grade ");
for(int i=0;i<marks.length;i++){
System.out.print(students[i]+" ");
for(int j=0;j<5;j++){
System.out.print(marks[i][j]+" ");
}
System.out.print(" "+avgmarks[i]+" "+grades[i]);
System.out.println();
}
} // end of method printGrades
} // end of grades
Students.txt
10
5
sucy 85 60 80 95 96
john 70 83 65 98 95
tom 65 80 60 95 94
ted 30 43 45 48 92
lucy 45 40 40 35 92
robert 40 83 85 88 91
clare 45 60 90 95 90
matt 90 83 85 88 90
walter 85 70 80 95 90
mark 30 43 45 48 50
Sample output
Students Test1 Test2 Test3 Test4 test5 Average Grade
sucy 85 60 80 95 96 83.2 B
john 70 83 65 98 95 82.2 B
tom 65 80 60 95 94 78.8 C
ted 30 43 45 48 92 51.6 F
lucy 45 40 40 35 92 50.4 F
robert 40 83 85 88 91 77.4 C
clare 45 60 90 95 90 76.0 C
matt 90 83 85 88 90 87.2 B
walter 85 70 80 95 90 84.0 B
mark 30 43 45 48 50 43.2 F
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.