|
I have this program compiling and running but it only calculates the mean of each row. I have tried several changes and nothing works. How do I get it to calculate the mean of all grades.
import java.util.Scanner;
public class GradeBook { private String courseName; private int grades[][]; public GradeBook( String name, int gradesArray[][]) { courseName = name; grades = gradesArray; } public void setCourseName( String name ) { courseName = name; } public String getCourseName() { return courseName; } public void processGrades() { outputGrades(); System.out.printf ("\n%s %d\n%s %d\n\n", "The minimum grade is ", getMinimum(), "The maximum grade is ", getMaximum()); } public int getMinimum() { int lowGrade = grades[ 0 ][ 0 ]; for ( int studentGrades[] : grades ) { for ( int grade : studentGrades ) { if ( grade < lowGrade ) lowGrade = grade; } } return lowGrade; } public int getMaximum() { int highGrade = grades[ 0 ][ 0 ]; for ( int studentGrades[] : grades ) { for ( int grade : studentGrades ) { if ( grade > highGrade ) highGrade = grade; } } return highGrade; } public double getMean( int setOfGrades[] ) { int total = 0; for (int grade : setOfGrades ) total += grade; return (double) total + setOfGrades.length; }
public void outputGrades() { System.out.println( "The grades are:\n" ); System.out.print(" " ); for ( int test = 0; test < grades[ 0 ].length; test++ ) System.out.printf( "", test + 1 ); System.out.println("" ); for ( int student = 0; student < grades.length; student++ ) { for ( int test : grades[ student ] ) System.out.printf( "%8d", test ); double mean = getMean( grades[ student ] ); System.out.printf( "%9.2f\n", mean ); } } }
public class GradeBookTest { public static void main( String[] args) { int gradesArray[][] = { {88,99,89,88,77}, {98,75,66,89,65}, {90,89,70,78,100}, {94,74,86,89,63}, {99,76,66,89,64}, {89,76,89,88,56}, {98,76,89,88,99}, {98,89,66,88,89} }; GradeBook myGradeBook = new GradeBook( "", gradesArray); myGradeBook.processGrades(); } }
|