Page 116 - Introduction to Programming with Java: A Problem Solving Approach
P. 116

                82 Chapter 3 Java Basics
Suppose you’ve got a variable named interest that stores a bank account’s interest as a double. You’d like to extract the dollars portion of the interest and store it in a variable of type int that is named interestInDollars. To do that, use the int cast operator like this:
interestInDollars = (int) interest;
The int cast operator returns the whole number portion of the casted value, truncating the fractional por- tion. Thus, if interest contains the value 56.96, after the assignment, interestInDollars contains the value 56. Note that the cast operation does not change the value of interest. After the assignment, interest still contains 56.96.
Use Parentheses to Cast an Expression
If you ever need to cast more than just a single value or variable, then make sure to put parentheses around the entire expression that you want to cast. Note this example:
double interestRate;
double balance;
int interestInDollars;
.. .
interestInDollars = (int) (balance * interestRate);
IntheinterestInDollarsassignment,balance * interestRateistheformulaforcalculating
interest. This code fragment performs basically the same operation as the previous one-line code fragment.
It extracts the dollars portion of the interest and stores it in an int variable named interestInDollars.
Thedifferenceisthattheinterestthistimeisintheformofanexpression,balance * interestRate, Apago PDF Enhancer
rather than in the form of a simple variable, interest. Since we want the cast operator to apply to the en-
        tire expression, we need to put parentheses around balance
In the above code fragment, what would happen if there were no parentheses around the
expression, balance
balance, rather than the entire expression. That should make sense when you look at the operator prece- dence table. The operator precedence table shows that the cast operator has very high precedence. So with- out the parentheses, the cast operator would execute prior to the multiplication operator, and the cast would thus apply only to balance. And that leads to an incorrect calculation for interest in dollars.
Use a Floating-Point Cast to Force Floating-Point Division
Suppose you’ve got a variable named earnedPoints that stores a student’s earned grade points for a semester’s worth of classes. Suppose you’ve got a variable named numOfClasses that stores the number of classes taken by the student. The student’s grade point average (GPA) is calculated by dividing earned points by number of classes. In the following statement, earnedPoints and numOfClasses are ints and gpa is a double. Does the statement correctly calculate the student’s GPA?
gpa = earnedPoints / numOfClasses;
Compare out- Suppose earnedPoints holds 14 and numOfClasses holds 4. You’d like gpa to get
*
interestRate? The cast would then apply only to the first thing at its right,
*
interestRate.
        a value of 3.5 (because 14 ÷ 4 􏰀 3.5). But alas, gpa gets a value of 3. Why? Because the you expect. / operator performs integer division on its two int operands. Integer division means the quotient is returned. The quotient of 14 ÷ 4 is 3. The solution is to force floating-point divi-
sion by introducing the cast operator. Here’s the corrected code:
put with what
   gpa = (double) earnedPoints / numOfClasses;
P
P
a
ar
re
en
nt
th
he
es
se
es
s
a
ar
re
en
n
e
ec
ce
es
s
s
sa
ar
ry
yh
he
er
r
e
e


































   114   115   116   117   118