Results 1 to 4 of 4

Thread: Stupid Prob

Hybrid View

  1. #1
    Addicted Member TBeck's Avatar
    Join Date
    Apr 2006
    Location
    Ontario, Canada
    Posts
    254

    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);

  2. #2
    Lively Member
    Join Date
    Dec 2005
    Posts
    68

    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.

  3. #3
    Addicted Member TBeck's Avatar
    Join Date
    Apr 2006
    Location
    Ontario, Canada
    Posts
    254

    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
  •  



Click Here to Expand Forum to Full Width