Weird Problem with a Program

Marge Tao
Greenhorn
Posts: 5
I am working on a project. This project calculates the GPA of a series of values entered by the user, then displays how many grades they entered, the average of the grades and the standard deviation of the grades. All values are taken in by the program, but any value that is greater than 110 or less than 0 is ignored by it.

When I debugged my program, I realized that the method I use to check if the grade is valid somehow keeps the first value stuck in memory (?). So, if the first value is valid, all other values are treated as that value. For example, if I enter 80, the program will correctly evaluate that it is a valid grade. However, if I type in 110, the program still evaluates the value like it is still set to 80. I thought maybe I could use a getter method to tell it to change it to the current entered value, but it had no effect. I'm stumped, so any advice would be much appreciated.

//This takes the valid user input and
//ignores invalid user input. Any valid
//input is sorted into each counter
//depending on the grade. Then, the
//values of the counters and the values
//of the grades are added up.

public void addGrade(double aGrade)
{
if(isValidGrade(aGrade))
{
//increment my total number of grades

if(aGrade >= A_PLUS)
{
counterAplus++;
}

else if(aGrade >= A)
{
counterA++;
}

else if(aGrade >= A_MINUS)
{
counterAminus++;
}

else if(aGrade >= B_PLUS)
{
counterBplus++;
}

else if(aGrade >= B)
{
counterB++;
}

else if(aGrade >= B_MINUS)
{
counterBminus++;
}

else if(aGrade >= C_PLUS)
{
counterCplus++;
}

else if(aGrade >= C)
{
counterC++;
}

else if(aGrade >= D)
{
counterD++;
}

else
{
counterF++;
}

gradeNum= counterAplus + counterA + counterAminus +
counterBplus + counterB + counterBminus + counterCplus +counterC + counterD + counterF;

}

gradeSum= aGrade;

gradeSumSquares=Math.pow(gradeSum,2) ;

}

Marge Tao
Greenhorn
Posts: 5
^ I looked over my program. I realized that the parsed variable in my test class was not in my while loop to repeat user input.

Now, I have another question. If I want to sum up all the user values (ex. 90+90+95), what would be a good way to assign this in my addGrade method?

Campbell Ritchie
Sheriff
Posts: 50644
83
Not quote sure what you mean about adding values, but you can set a total field, initialise it to 0 and in the addGrade method, add the grade to the total.

By the way, if you go to the Sun Style Guide �10.5.2 you find out your isValidGrade method could be written much more simply.

Campbell Ritchie
Sheriff
Posts: 50644
83

Marge Tao
Greenhorn
Posts: 5
Sorry about the title. I would change my isValidGrade method, but that is how I am supposed to write it. I do not want to get in trouble for doing something else, even if it would be better and more efficient.

As for my earlier question, I figured it out on my own. Thanks for the advice though. I'll keep it mind in the future.

 With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.