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
1

Expert's answer

2015-10-13T03:05:12-0400

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!

Comments

Assignment Expert
29.08.16, 18:33

Yes, it can be skipped.

keith
28.08.16, 18:03

char storeGrades[]=new char[5]; what is the function of this line? are not in use

Assignment Expert
29.02.16, 15:52

You need to change arrays capacity in main function from 5 to 10 and change maximal counter value in loops from 5 to 10 in functions, where arrays are processed.

jack
27.02.16, 13:24

what if i have 10 name instead ?

Assignment Expert
16.11.15, 10:21

Dear vinod put the file "student.txt" to the same directory student.txt 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

vinod
15.11.15, 17:32

i m gettting arrayindex bound exception

Assignment Expert
11.11.15, 17:00

Dear Louis, file students.txt contains such fields: 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

Louis
10.11.15, 06:17

So what is the format of the students.txt?

LATEST TUTORIALS
APPROVED BY CLIENTS