Hello, i need some help with calculating the average score for each student at m
ID: 3904441 • Letter: H
Question
Hello, i need some help with calculating the average score for each student at method "calculateAVG" and printing the average score of each student at method "printGrades"
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 two dimensional 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;
public static void main(String[] args)
{
Scores s = new Scores();
s.readData();
s.calculateAVG();
s.printGrades();
}
public void readData()
{
Scanner keyboard = new Scanner(System.in);
for (int index = 0; index < names.length; index++)
{
System.out.print(" Student no" + (index + 1) + ": ");
names[index] = keyboard.nextLine();
for (int j = 0; j < scores[0].length; j++)
{
System.out.print("score no" + (j + 1) + ": ");
scores[index][j] = keyboard.nextInt();
}
keyboard.nextLine();
}
keyboard.close();
}
//Calculates the average test score and grade
public void calculateAVG()
{
double avg = 0;
for (int index = 0; index < scores.length; index++)
{
avg = 0;
for (int j = 0; j < scores[0].length; j++)
{
avg += scores[index][j];
}
avg /= scores[0].length;
if (avg >= 0 && avg < 50)
grades[index] = 'F';
else if (avg >= 50 && avg <= 64)
grades[index] = 'D';
else if (avg >= 65 && avg <= 74)
grades[index] = 'C';
else if (avg >= 75 && avg <= 84)
grades[index] = 'B';
else if (avg >= 85 && avg <= 100)
grades[index] = 'A';
else
grades[index] = '?';
classAVG += avg;
}
classAVG /= scores.length;
}
public void printGrades()
{
System.out.println(" Grades:");
for (int i = 0; i < grades.length; i++)
{
System.out.println(names[i] + ": " + grades[i]);
}
System.out.println("Class average: " + classAVG);
}
}
Explanation / Answer
class Scores {
// One dimensional array to store the student names
String[] names = {"Johnson", "Aniston", "Cooper", "Gupta", "Blair", "Clark", "Kennedy", "Bronson", "Sunny", "Smith"};
// Parallel two dimensional 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}
};
double[] avgOfStudent = new double[10];
// Parallel one-dimensional array to store grades
char[] grades = new char[10];
// class average variable
double classAVG;
public static void main(String[] args)
{
Scores s = new Scores();
s.readData();
s.calculateAVG();
s.printGrades();
}
public void readData()
{
Scanner keyboard = new Scanner(System.in);
for (int index = 0; index < names.length; index++)
{
int each = 0;
System.out.print(" Student no" + (index + 1) + ": ");
names[index] = keyboard.nextLine();
for (int j = 0; j < scores[0].length; j++)
{
System.out.print("score no" + (j + 1) + ": ");
scores[index][j] = keyboard.nextInt();
each += scores[index][j];
}
avgOfStudent[index] = each;
keyboard.nextLine();
}
keyboard.close();
}
//Calculates the average test score and grade
public void calculateAVG()
{
double avg = 0;
for (int index = 0; index < scores.length; index++)
{
avg = 0;
for (int j = 0; j < scores[0].length; j++)
{
avg += scores[index][j];
}
avg /= scores[0].length;
avgOfStudent[index] = avg;
if (avg >= 0 && avg < 50)
grades[index] = 'F';
else if (avg >= 50 && avg <= 64)
grades[index] = 'D';
else if (avg >= 65 && avg <= 74)
grades[index] = 'C';
else if (avg >= 75 && avg <= 84)
grades[index] = 'B';
else if (avg >= 85 && avg <= 100)
grades[index] = 'A';
else
grades[index] = '?';
classAVG += avg;
}
classAVG /= scores.length;
}
public void printGrades()
{
System.out.println(" Grades:");
for (int i = 0; i < grades.length; i++)
{
System.out.print(names[i] + ": " + grades[i]);
System.out.println(" Average: "+avgOfStudent[i]);
}
System.out.println("Class average: " + classAVG);
}
}
/*OUTPUT
Grades:
Johnson: F Average: 47.8
Aniston: D Average: 51.0
Cooper: D Average: 59.8
Gupta: D Average: 55.0
Blair: B Average: 82.6
Clark: F Average: 31.6
Kennedy: D Average: 56.6
Bronson: F Average: 44.0
Sunny: F Average: 38.0
Smith: D Average: 52.4
Class average: 51.88000000000001
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.