Question #55470

Write a program to calculate students average test scores and their grades.
Use three arrays: a one-dimensional array to store the student names, a (parallel) two- dimensional array to store the test score, and a parallel one-dimensional array to store grades. Your program must contain at least the following methods: a method to read and to store data into two arrays, a method to calculate the average test score and grade and a method to output the result. Have your program also output the class average.

Johnson 85 83 77 91 76
Aniston 80 90 95 93 48
Cooper 78 81 11 90 73
Gupta 92 23 30 69 87
Blair 23 45 96 38 59

Grade calculation to be computed as follows:
Marks Grade
85 - 100 A
75 - 84 B
65- 74 C
50 - 64 D
<50 F

Expert's answer

Answer on Question #55470, Programming / Java

Solution


import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
/**
* Main class
*
*/
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
//one-dimensional array to store the student name
String studentNames[] = new String[5];
//(parallel) two-dimensional array to store the test score
int testScores[][] = new int[5][5];
//a parallel one-dimensional array to store grades
char storeGrades[] = new char[5];
//read data from file
int totalStudents = read(studentNames, testScores);
print(totalStudents, studentNames, testScores);
}
/**
* Read students from file students.txt
*/
}
private static int read(String studentNames[], int testScores[][]){
BufferedReader br = null;
int counter=0;
try {
String fileLine;
br = new BufferedReader(new FileReader("students.txt"));
while ((fileLine = br.readLine()) != null) {
//get name from file
studentNames[counter] = fileLine.split(" ")[0];
//get score 1 from file
testScores[counter][0] = Integer.parseInt(fileLine.split(" ")[1]);
//get score 2 from file
testScores[counter][1] = Integer.parseInt(fileLine.split(" ")[2]);
//get score 3 from file
testScores[counter][2] = Integer.parseInt(fileLine.split(" ")[3]);
//get score 4 from file
testScores[counter][3] = Integer.parseInt(fileLine.split(" ")[4]);
//get score 5 from file
testScores[counter][4] = Integer.parseInt(fileLine.split(" ")[5]);
counter++;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null) br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
return counter;
}
/**
* calculate the average test score
* @return
*/
private static double calculateAverageTestScore(int testScores[][], int studentIndex){
double sum=0;
for(int s=0;s<5;s++){
//calculate total sum of scores of selected student
sum+=testScores[studentIndex][s];
}
return sum/5;
}
/**
* getGrade
* @param averageTestScore
* @return
*/
private static char getGrade(double averageTestScore){
//85 - 100 A
if(averageTestScore>=85){
return 'A';
}
//75 - 84 B
if(averageTestScore>=75){
return 'B';
}
//65- 74 C
if(averageTestScore>=65){
return 'C';
}
//50 - 64 D
if(averageTestScore>=50){
return 'D';
}
//<50 F
return 'F';
}
/***
* calculate Class Average
* @param testScores
* @return
*/
private static double calculateClassAverage(int testScores[][]){
double sum=0;
for(int i=0;i<5;i++){
for(int s=0;s<5;s++){
//calculate total sum of all scores
sum+=testScores[i][s];
}
}
return sum/25;
}
/**
* method to output the result.
* @param totalStudents
* @param studentNames
* @param testScores
*/
private static void print(int totalStudents,String studentNames[],int testScores[][]){
//display information
for(int i=0;i<totalStudents;i++){
System.out.print(studentNames[i]+": ");
for(int s=0;s<5;s++){
System.out.print(testScores[i][s]+" ");
}
//calculate average Test Score
double averageTestScore=calculateAverageTestScore(testScores,i);
//display averageTestScore
System.out.println(" average test score: " + averageTestScore + " grade: " + getGrade(averageTestScore));
}
//display Class Average
System.out.println("\nClass Average: " + calculateClassAverage(testScores)+" = " + getGrade(calculateClassAverage(testScores)));
}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS