Question #56878

Write a program to calculate students average test scores and their grades. You may
assume the following data:

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


Use three arrays: a one-dimensional array to store the student names, a (parallel) twodimensional
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 store data into two arrays, a method to calculate the average test score and grade
and a method to output the results. Have your program also output the class average.
1

Expert's answer

2015-12-11T01:41:15-0500

Answer on Question 56878 Programming / Java | JSP | JSF


import java.util.Scanner;
public class Scores {
// one-dimensional array to store the student names
String[] names = {"Johnson", "Aniston", "Cooper", "Gupta", "Blair", "Clark", "Kennedy", "Bronson", "Sunny", "Smith"};
// parallel twodimensional array to store the test score
int[][] scores = {
{85, 83, 77, 91, 76},
{80, 90, 95, 93, 48},
{78, 81, 11, 90, 73},
{92, 83, 30, 69, 87},
{23, 45, 96, 38, 59},
{60, 85, 45, 39, 67},
{77, 31, 52, 74, 83},
{93, 94, 89, 77, 97},
{79, 85, 28, 93, 82},
{85, 72, 49, 75, 63}
};
// parallel one-dimensional array to store grades
char[] grades = new char[10];
// class average variable
double classAVG;
/**
* Main function
* @param args
*/
public static void main(String[] args) {
Scores s = new Scores();
s.readData(); // comment this whole line if you want to use predefined values for arrays
s.calculateAVG();
s.printGrades();
}
/**
* Reads and stores data into two arrays
*/
public void readData() {
Scanner kb = new Scanner(System.in);
for (int i = 0; i < names.length; i++) {
System.out.print("\nName #" + (i + 1) + ": ");
names[i] = kb.nextLine();
for (int j = 0; j < scores[0].length; j++) {
System.out.print("score #" + (j + 1) + ": ");
scores[i][j] = kb.nextInt();
}
kb.nextLine();
}
kb.close();
}
/**
* Ccalculates the average test score and grade
*/
public void calculateAVG() {
double avg = 0;
for (int i = 0; i < scores.length; i++) {
avg = 0;
for (int j = 0; j < scores[0].length; j++) {
avg += scores[i][j];
}
avg /= scores[0].length;
if (avg >= 0 && avg < 20)
grades[i] = 'E';
else if (avg >= 20 && avg < 40)
grades[i] = 'D';
else if (avg >= 40 && avg < 60)
grades[i] = 'C';
else if (avg >= 60 && avg < 80)
grades[i] = 'B';
else if (avg >= 80 && avg <= 100)
grades[i] = 'A';
else
grades[i] = '?';
classAVG += avg;
}
classAVG /= scores.length;
}
/**
* Outputs the results
*/
public void printGrades() {
System.out.println("\nGrades:");
for (int i = 0; i < grades.length; i++) {
System.out.println(names[i] + ": " + grades[i]);
}
System.out.println("Class average: " + classAVG);
}

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

No comments. Be the first!
LATEST TUTORIALS
APPROVED BY CLIENTS