Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: GPA calc code... HELP

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default GPA calc code... HELP

    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!
    Last edited by helloworld922; October 13th, 2009 at 08:41 PM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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.

  3. #3
    Junior Member
    Join Date
    Oct 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: GPA calc code... HELP

    thats my whole problem?

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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:
    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;
         }
    }