RU beehive logo ITEC Department logo
ITEC 120
2007fall
ibarland,
jdymacek

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs

lab04a
lab04a
witnessing bad arithmetic

We start by trying some expressions from lecture, to confirm that Java doesn't always give correct arithmetic answers. Predict the result of each expression to your partner, then try them out:

Challenge: For each of above, classify the error as

  1. round-off error (only possible with floating-point arithmetic)
  2. overflow (can be integer or floating-point arithmetic)
  3. underflow (only possible with floating-point arithmetic)

Order of operations

Challenge question:

Practicing with quotient, remainder

Remember that /, if given two integers, returns the quotient. The remainder can be obtained by % (pronounced “mod” or “modulo” in addition to “remainder”). Predict the result of each expression to your partner, then try them out:

Task: Delicious SugarBomb donuts cost 65 cents each. A robot wants to be cashier, and needs to know what to say to customers, after the customer has told it how many donuts are desired. Complete the following function to help the robot out.

/** 
 * @param ____  The number of donuts ordered.
 * @return A String telling the customer how many 
 *    dollars and cents their order comes to.
 *  donutOrder(4)  = "4 donuts cost 2 dollars and 60 cents."
 *  donutOrder(__) = ______________________________________
 *  donutOrder(__) = ______________________________________
 */
String donutOrder( ___  ____ ) {





  }

Include comments and test cases, of course. Use a NAMED_CONSTANT to represent the price of a single donut.

Comparing doubles

If you finish the preceding problem,

Comparing doubles

Edict of Programming: Do not use == with doubles.
After all, what is the value of this expression?
(2.0 - 1.1) == 0.9
So how do we see if two doubles are practically-equal? One immediate imponderable is “how close is close enough to be considered practically equal”? Well, for the moment let's say being within 0.001 is close enough: (y-0.001) < x < (y+0.001) is a start. (How do we write this math expression in Java? We'll need to use &&!) The amount “0.001” is called the “tolerance” of the comparison.

Task, part 1: Write a function nearlyEqual which takes in two doubles, and returns whether or not they are within 0.001 of each other. (Use a named-constant for the tolerance.)

Task, part 2: Actually, we should look at an relative difference between two terms, rather than an absolute difference. After all, 4.000000e6 dollars is practically equal to 4.000001e6 dollars, even though the difference between these two is one (which is far exceeds our tolerance).

Modify your program so that it returns true if the ratio of the two numbers is close to 1: that is, if (1-0.0001) < x/y < (1+0.0001). What is a test case for which this formula doesn't make work? Can you fix your code to handle that test case?1

We won't finish this last problem in lab today, but it is worth completing as quiz1-study-material.


1 A more subtle wrinkle: is your function symmetric — that is, if your program says that a is nearly equal to b, does that mean it will always say that b is nearly equal to a? If not, is this easy to fix? Is is important to fix it?      

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs


©2007, Ian Barland, Radford University
Last modified 2007.Aug.25 (Sat)
Please mail any suggestions
(incl. typos, broken links)
to iba�rlandrad�ford.edu
Powered by PLT Scheme