January 2014
Intermediate to advanced
736 pages
70h 43m
English
If at least one grade was entered, line 72 of Fig. C.5 calculates the average of the grades. Recall from Fig. C.2 that integer division yields an integer result. Even though variable average is declared as a double (line 45), the calculation
average = total / gradeCounter;
loses the fractional part of the quotient before the result of the division is assigned to average. This occurs because total and gradeCounter are both integers, and integer division yields an integer result. To perform a floating-point calculation with integer values, we must temporarily treat these values as floating-point numbers for use in the calculation. Java provides the unary ...
Read now
Unlock full access