ITEC 120 2008fall aaray,ejderrick,ibarland,jmdymacek

homeinfolabshwsexams
textbookjava.lang docsjava.util docsarchive

# lect05aArithmetic

• 3+4*5. Compare to some alien arithmetic 3@4\$5, where @ and \$ are operators you can look up in a table (just like you can with +,*). We say, *,/ have higher precedence than +,- (and they are both “left-associative”).

In elementary school, you might have learned “PEMDAS”, to remind you: Parentheses, Exponentiation, Muliplication, Division, Addition, Subtraction. In Java it's similar:

• First, go left-to-right, doing all Parentheses.
• (Java does not have exponeniation as an operator; you have to call some method1 to do that.)
• Then, go left-to-right, doing each multiplcation and division (in the order you see them);
• Finally, go left-to-right, doing each addition and subtraction (in the order you see them).

• What does 2+3*4-5*6+7 evaluate to?
One more: 36-24/6/2 * 3-1 = ??
• Java has the remainder-function as a built-in operator: type 5 % 3, and you'll get the remainder after dividing 5 by 3. Try also: 4 % 3 and 6 % 3. (Maybe ... 0 % 3 and -1 % 3, too.)
• What is 6 * 7 % 3? (That is, what is the precedence of %, compared with the other operators?)
Upshot: You can count on *,/ (and %) happening before +,-, and furthermore it's left-to-right within each category.
But: % isn't the only new operator Java has; there are 46 operators with 15 levels of precedence in Java. Nobody memorizes all the precedence rules; once you're beyond basic arithmetic, use parentheses to make your operations clear. (Even if you know the precedence of some obscure operator, other people reading your code might not.)
• Evaluate 2/3.
Huh? What about 4/3? 5/3? 6/3?
What's going on?
There isn't a problem when we say 4.0 / 3.0 (well, not much of a problem).
It turns out: There are two different functions, each named /: real division, and integer division (quotient).
Why Java decided to give those two fairly different beasts the same name is a bit of a mystery. At any rate, beware the pitfall. Explain why the following is wrong, even though tier2fee is declared as a double:  ```double tier2fee = (salePrice - 25) * 3 / 100 ; ```
Habit of Programming: Whenever you use "/", think:
1. Could the denominator ever be 0?
2. Is this integer-division (quotient), or floating-point division?
• What is 36-24/7/2 * 3-1?
• Note that % and integer-division complement each other:
For all ints p and q where q != 0, we have p == (p/q)*q + (p%q).
• What do you do if you have two ints, and you want to do floating-point division on them? Well, if you have int literals like 4 and 3, you could write 4.0 / 3.0. But what if you had two int variables, like numPizzas and numPeople?

The solution, is that we call a built-in function, to convert an int into a double. You'd think that the function might have a familiar signature, like double Integer.toDouble( int ). Alas, no such functions exist2. It turns out, that's not the case -- Java has a whole special syntax for calling this particular conversion function, called casting. You write (double) in front of the int expresion:

 ```((double) numPizzas) / ((double) numPeople) ```
Note that two outer sets of parentheses are for clarity. The parentheses around the word double are required.

Casting is the only time you use the name of a type even though you aren't introducing a new name to the Java compiler. It is conceptually annoying that Java has a whole special syntax just for this small set3 of conversion functions.

Java calls these conversion functions for you all the time, behind your back. For instance, Math.sqrt(25) is silently re-written as Math.sqrt( (double) 25 ), since Math.sqrt's documentation shows it requires a double as input.

Beware that casting from double to int can induce arithmetic errors (discussed below).

If (somehow) time allows: show motivation for how public-key-crypto can possible work.

1Math.pow(2,5) = 32.

2Well, they do, but they're not static, and they require new which we haven't talked about yet. And anyway, the built-in conversion functions are tedious to write: (new Integer( numPizzas )).doubleValue().

3 Well, we haven't talked about classes yet, but Java also lets you cast one class to another (not just numbers). However, casting classes is poor style; it indicates your program isn't correctly mirroring your real-world problem.

homeinfolabshwsexams
textbookjava.lang docsjava.util docsarchive