Welcome to Dream.In.Code
Become a Java Expert!

Join 149,521 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,401 people online right now. Registration is fast and FREE... Join Now!




calculate mean of all scores

 
Reply to this topicStart new topic

calculate mean of all scores, java gradebook

lessa124
1 Jul, 2007 - 01:18 PM
Post #1

New D.I.C Head
*

Joined: 28 Jun, 2007
Posts: 3


My Contributions
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();
}
}

User is offlineProfile CardPM
+Quote Post

keems21
RE: Calculate Mean Of All Scores
1 Jul, 2007 - 07:47 PM
Post #2

D.I.C Head
Group Icon

Joined: 3 Feb, 2007
Posts: 183



Thanked: 2 times
Dream Kudos: 25
My Contributions
Your program is only outputting the mean for each single array because that is all that you are telling it to do.

CODE
public double getMean( int setOfGrades[] )
{
int total = 0;

for (int grade : setOfGrades )
total += grade;

return (double) total + setOfGrades.length;
}


In this method, you only give access to a one dimensional array, so there is no way to get the mean of the entire 2d array. To fix this, make the method accept a 2d array then use a nested for loop in the method.
User is offlineProfile CardPM
+Quote Post

oubless
RE: Calculate Mean Of All Scores
2 Jul, 2007 - 06:51 AM
Post #3

New D.I.C Head
*

Joined: 28 May, 2007
Posts: 23


My Contributions
Something like this maybe:
CODE

int twoDimmensionalArray[][]
...
long sum=0;
int count=0;
for(int oneDim[] : twoDimmensionalArray){
     for( int elem : oneDim)
         sum += elem;
     count += oneDim.length;
}
return (double)sum / (double)length;

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 08:27PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month