home—info—archive—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
lab04b
if-else-if, continued
We will spend most of today finishing up
the ebay lab from Tuesday
(including the three-tiered price structure for items
over $1000.)
Practicing with quotient, remainder
First though, we'll take ten minutes to look at
/ for doubles vs.
/ for ints vs.
% (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:
- 260.0 / 100.0
- 260 / 100
- 260 % 100
-
int m = 260;
int n = 100;
m/n
// Question:
// How to get the floating-point division of m/n, rather than quotient?
// We must convert m from an int into a double!
// Call a function to convert m to a double.
// Here's a version which converts it to a String,
// then converts the String to a double, yikes!
//
Double.parseDouble( Integer.toString(m) )
//
// That's roundabout and convoluted, but is entirely in terms
// of syntax and concepts we understand.
// Call a different function to convert m to a double,
// even though we haven't talked about 'new':
//
(new Integer(m)).doubleValue()
// Here is an entirely new syntax which doesn't look like a function-call
// but it really is. This is "casting", and the parentheses are required,
// and it's the only time you'll mention a type in Java w/o introducing
// a new name.
(double) m // Casting.
double m // Error (the parens are part of the syntax of casts)
(double) m / (double) n // integer division, or floating-point division?
((double) m) / ((double) n) // Casting (w/ additional parens, for clarity?)
|
-
Suppose somebody walks up into a change-machine and dumps in a bucket
of small change — n¢ worth.
You'll first calculate how many quarters to return,
and then calculate how much money you still owe the person,
and then calculate how many additional dimes to give them.
int n = 1234; // Or however much money was dumped in, in cents.
n // Type this in code pad, just to check its value.
int quartersBack; // How many quarters we'll return.
quartersBack = ;
int stillDue25; // How much money we still owe back, after quarters.
stillDue25 = ;
quartersBack // Type this in code pad, just to check its value.
stillDue25 // Type this in code pad, just to check its value.
int dimesBack;
dimesBack =
stillDue10 =
int nickelsBack;
nickelsBack =
stillDue5 =
|
(solution)
Two notes (optional):
-
Instead of expressing stillDue25 as
“n%25”, you could have written
“n - quartersBack*25”, which is also equivalent to
“n - (n/25)*25”.
In fact, this is a general fact:
For any ints p,q
(where q != 0),
it is true that p%q == p - (p/q)*q.
-
Could you write the above without any local variables — only using n?
Yes!
The amount of dimes back is ,
and the amount of nickels back will be .
(solution)
Okay, go ahead and finish up
the ebay lab.
A couple of points:
-
Don't repeat code;
computeEbayFee
shouldn't have any mention of the final-value-specific notions
like the low threshold of $25 or the rate 3.25%.
Instead,
computeEbayFee should call computeFVFee.
-
The magic number 25.00 shouldn't appear in your solution at all;
instead use FV_THRESHOLD_LOW.
This way, if ebay changes the lower threshold for the final value fee
from 25 to 20,
you only need to change one single line of your program.
-
The derived number 1.31 (or 1.3125) should not appear in your solution at all;
instead use FV_THRESHOLD_LOW * FV_RATE_LOW.
This way, if ebay changes the lower threshold for the final value fee
from 25 to 20,
you only need to change one single line of your program.
home—info—archive—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs