# GPA calc code... HELP

• October 13th, 2009, 08:39 PM
ber1023
GPA calc code... HELP
Code :

```public class GPA_Calc { public void addGrade(double x) { grade += x; numtest++;   if (x >= 90) gpagrade += 4; else if (80 >= x && x <=89) gpagrade += 3; else if (70 >= x && x <=79) gpagrade += 2; else if (60 >= x && x <=69) gpagrade += 1; else if (x <= 59) gpagrade += 0; }   public GPA_Calc() { grade = 0; numtest = 0; gpagrade = 0; } public String getGrade() { if (grade >= 90) return "A"; else if (80 >= grade && grade <=89) return "b"; else if (70 >= grade && grade <=79) return "c"; else if (60 >= grade && grade <=69) return "D"; else return "F"; } public double getGPA() { return gpagrade; } private double grade; private double numtest; private double gpagrade;```

// no errors, when i enter grades likie 87 in i return F .. only when i return grades like 90+ do i get an A, also the GPA score comes out wrong because of it i think
help!
• October 13th, 2009, 09:44 PM
helloworld922
Re: GPA calc code... HELP
You're constructor's not quite right. You need to initialize it the the values inputted, not just to 0.
• October 13th, 2009, 10:03 PM
ber1023
Re: GPA calc code... HELP
thats my whole problem?
• October 14th, 2009, 12:20 AM
helloworld922
Re: GPA calc code... HELP
Oh wait, no, that's not a problem. I see what it is you're doing. hmm... I don't understand why you're getting the results you are. It seems to me that you should be getting A's almost all the time because you keep adding to grades, and never find an average grade.

This should work:
Code :

```public void addGrade(double x) { grade = grade * numTest + x; numTest++; grade /= numTest; if (grade >= 90) { gpa = 4; } else if (grade >= 80) { gpa = 3; } else if (grade >= 70) { gpa = 2; } else if (grade >= 60) { gpa = 1; } else { gpa = 0; } }```