|
home—info—lects—labs—exams—hws
tutor/PIs—Object120 + its docs—java.lang docs—java.util docs
When you have two
The solution is that we must call a function to
convert an
((double) numPizzas) / ((double) numPeople) |
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 set2 of conversion functions.
Java calls these conversion functions for you all the time, behind your back.
For instance,
Beware that casting can induce arithmetic errors (discussed yesterday).
Web traffic is sent on postcards. How to securely communicate your credit-card number to amazon, without any pre-arrangement? “Inconceivable!”?
Amazon might say on their web page:
Our public keyIs announcingpubKey = 37 . If you want to send us a secret messageprivMsg (a two-digit number), multiply your secret number by our public key, and tell us the last two digits (only) of the result: That is, tell us(privMsg * pubKey) % 100 . (We can name this resultpubMsg .)
Note that this used no pre-arrangement between the sender and the receiver. This is called “public key cryptography”, since eavesdroppers know as much about breaking the code as the sender.
You could break this code, if you could divide by 37 mod 100. (Or more precisely: find the multiplicative inverse of 37, mod 100.) Project: read about the extended Euclidean algorithm for calculating muliplicative inverses. Break this code!
Although this particular scheme is easy to break with a bit of math knowledge (this Extended Euclidean algorithm), the example is to motivate how a public-key system is even conceivable.
We have covered many topics over the last 4 weeks:
1Well, they do, but they're not static,
and they require
2 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. ↩
home—info—lects—labs—exams—hws
tutor/PIs—Object120 + its docs—java.lang docs—java.util docs
©2010, Ian Barland, Radford University Last modified 2010.Sep.29 (Wed) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |