Write a java program that calculates average of math, science grade as well as t
ID: 3817570 • Letter: W
Question
Write a java program that calculates average of math, science grade as well as total class average. The class gets following math and science grade of mid-term:
MATH
SCIENCE
80.5
25.5
100
92.5
75.0
65.5
82.3
79.6
35.9
22.7
73.6
66.2
88.9
98.7
46.2
62.3
100
100
97.8
99.5
Your program should meet following requiements:
1. The program must declare two-dimensional array to save all data above.
2. The program must use nested for loop to calculate math, and science average as well as total class average.
3. The program only allow two decimal palces. (Example: 25.54)
Sample Run:
Class Average: 74.64
Math Average: 78.02
Science Average: 71.25
MATH
SCIENCE
80.5
25.5
100
92.5
75.0
65.5
82.3
79.6
35.9
22.7
73.6
66.2
88.9
98.7
46.2
62.3
100
100
97.8
99.5
Explanation / Answer
Here is your code:-
public class ClassGrading {
public static void main(String[] args) {
Double[][] grades = new Double[2][10]; //two rows one for maths and other for science
//adding values as per the table given in question
grades[0][0] =80.5;
grades[0][1] =100.0;
grades[0][2] =75.0;
grades[0][3] =82.3;
grades[0][4] =35.9;
grades[0][5] =73.6;
grades[0][6] =88.9;
grades[0][7] =46.2;
grades[0][8] =100.0;
grades[0][9] =97.8;
grades[1][0] =25.5;
grades[1][1] =92.5;
grades[1][2] =65.5;
grades[1][3] =79.6;
grades[1][4] =22.7;
grades[1][5] =66.2;
grades[1][6] =98.7;
grades[1][7] =62.3;
grades[1][8] =100.0;
grades[1][9] =99.5;
//Calculating average for maths,science and total class
double mathAver=0.0,sciAver=0.0,classAver=0.0;
double mathSum=0.0,sciSum=0.0,classSum=0.0;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 10; j++) {
if(i==0) {//for maths
mathSum=mathSum+grades[i][j];
} else {//for science
sciSum=sciSum+grades[i][j];
}
classSum=classSum+grades[i][j]; // total class
}
}
mathAver =mathSum/10;
sciAver = sciSum/10;
classAver = classSum/20;
//Rounding to two decimal places
mathAver = Math.round(mathAver*100d)/100d;
sciAver = Math.round(sciAver*100d)/100d;
classAver = Math.round(classAver*100d)/100d;
//Printing the result
System.out.println("Class Average: "+classAver);
System.out.println("Math Average: "+mathAver);
System.out.println("Science Average: "+sciAver);
}
}
Output: -
Class Average: 74.64
Math Average: 78.02
Science Average: 71.25
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.