Java help.

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
TubaRiver
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 3
Joined: Fri Jun 19, 2009 4:24 pm
Programming Language of Choice: java
Location: Florida
Contact:

Java help.

Post 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
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Java help.

Post 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
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
The Great
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 17
Joined: Mon Nov 30, 2009 7:51 pm

Re: Java help.

Post 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).
Post Reply