|
-
Feb 13th, 2007, 10:58 PM
#1
Addicted Member
Re: Stupid Prob
In your Student Class
the calculation
Code:
Gpa = cHours/pEarned;
is double = int/int
now the int/int will return an int value (in your case cHours is less than pEarned so the result is 0.something, but the something is being trimmed off since it is an int)
to prevent this you must first cast the variables to double and then divide, this can be done as follows
Code:
Gpa = (double)(cHours)/(double)(pEarned);
-
Feb 16th, 2007, 11:21 PM
#2
Lively Member
Re: Stupid Prob
Code:
Gpa = (double)(cHours)/(double)(pEarned);
Really you only need to cast one of the values. Java will automatically promote the other int to a double. Not a big deal, just being picky.
Also, it is good programming practice to check for possible division by zero exceptions before doing any division. You might just want to add a little if...else before you divide to see if pEarned is 0.
This doesn't necessarily pertain to your problem, but it might cause some down the line.
-
Feb 17th, 2007, 12:12 AM
#3
Addicted Member
Re: Stupid Prob
yea i guess you only have to do one of them, or multiplying one of them by 1.0 or something like that works too.. I guess we all have our own little way of doing things like this
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|