Page 1 of 1

Java help.

Posted: Wed Dec 02, 2009 12:52 pm
by TubaRiver
Hi, i need help with this code just a bit. I am wondering what am i doing wrong with my "average" I don't expect you guys to just give me my answer but if you can point me in the right direction i would love you all!! underneath the code is what i am SUPPOSED to have and only the average section is wrong.

Code: Select all

public class GradesV2
{
    public static void main(String[ ] args)
    {
       //Local variables
       int totalPoints = 0;
       int numTests = 0;
       int testGrade = 0;
       double average = 0.0;
       
       
       testGrade = 97;
       totalPoints += testGrade;
       numTests ++;
       average = totalPoints / testGrade;
       System.out.print("n = " + numTests + " New test grade: " + testGrade + " total points: " + totalPoints + " average: " + average);
    
    
       testGrade = 79;
       totalPoints += testGrade;
       numTests++;
       average = totalPoints / testGrade;
       System.out.println();
       System.out.print("n = " + numTests + " New test grade: " + testGrade + " total points: " + totalPoints + " average: " + average);
    
       
       testGrade = 83;
       totalPoints += testGrade;
       numTests++;
       average = totalPoints / testGrade;
       System.out.println();
       System.out.print("n = " + numTests + " New test grade: " + testGrade + " total points: " + totalPoints + " average: " + average);
     
       
       testGrade = 88;
       totalPoints += testGrade;
       numTests++;
       average = totalPoints / testGrade;
       System.out.println();
       System.out.print("n = " + numTests + " New test grade: " + testGrade + " total points: " + totalPoints + " average: " + average);
    
    }
}

This is what my result should be but it's not.

n = 1 New test grade: 97 total points: 97 average 97.0
n = 2 New test grade: 79 total points: 176 average 88.0
n = 3 New test grade: 83 total points: 259 average 86.3333333
n = 4 New test grade: 88 total points: 347 average 86.75

Re: Java help.

Posted: Wed Dec 02, 2009 1:07 pm
by avansc
you might wanna look at how averages are calculated. im assuming you just want the arithmetic mean.
but i looks something like this

(Σ x)/n

Re: Java help.

Posted: Thu Dec 03, 2009 6:32 pm
by The Great
This looked like a nice little challenge lol.
You want to make something like this:

Code: Select all

public class GradesV2
{
    public static void main(String[ ] args)
    {
       //Local variables
       int totalPoints = 0;
       int numTests = 0;
       int testGrade = 0;
       double average = 0.0;
       
       
       testGrade = 97;
       totalPoints += testGrade;
       numTests ++;
       average = (double)totalPoints/numTests;
       System.out.print("n = " + numTests + " New test grade: " + testGrade + " total points: " + totalPoints + " average: " + average);
    
    
       testGrade = 79;
       totalPoints += testGrade;
       numTests++;
       average = (double)totalPoints/numTests;
       System.out.println();
       System.out.print("n = " + numTests + " New test grade: " + testGrade + " total points: " + totalPoints + " average: " + average);
    
       
       testGrade = 83;
       totalPoints += testGrade;
       numTests++;
       average = (double)totalPoints/numTests;
       System.out.println();
       System.out.print("n = " + numTests + " New test grade: " + testGrade + " total points: " + totalPoints + " average: " + average);
     
       
       testGrade = 88;
       totalPoints += testGrade;
       numTests++;
       average = (double)totalPoints/numTests;
       System.out.println();
       System.out.print("n = " + numTests + " New test grade: " + testGrade + " total points: " + totalPoints + " average: " + average);
    
    }
}
Your errors:
1.) average should always be the total points divided by the number of tests. That's just how you get averages.
2.) you need to use a cast (for converting int to double) on one of the int variables so that you produce real doubles (instead of just .0 on the end).