aspose file tools*
The moose likes Beginning Java and the fly likes Weird Problem with a Program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Weird Problem with a Program" Watch "Weird Problem with a Program" New topic
Author

Weird Problem with a Program

Marge Tao
Greenhorn

Joined: Oct 12, 2008
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

Joined: Oct 12, 2008
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

Joined: Oct 13, 2005
Posts: 39791
    
  28
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

Joined: Oct 13, 2005
Posts: 39791
    
  28
And please don't use titles like "Weird Result for a Program".
Marge Tao
Greenhorn

Joined: Oct 12, 2008
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Weird Problem with a Program