![]() |
![]() |
|
Delicious Sugarbomb Donuts cost 65¢ each; if you order 10 or more you get a 5% discount on all your donuts.
Their motto: It pays to buy in bulk, at Bulk-o-Mart's bakery!
First, one point:
we will calculate the price of donuts not in dollars (e.g. two donuts cost 1.6 dollars), but in cents (e.g. two donuts cost 165 cents).
(Think pennies
if it helps.)
Note: Be sure not to confuse $0.65 with 0.65¢ — one of those is much more than a penny, one is less than a single penny! Just like 65 is more than 1, and 0.65 is less than one. (You knew that!)Why do we do this? The fundamental reason is that we can never charge a customer a fraction of a cent, so we shouldn't choose a data-type that allows a fraction of a cent. Using
(Another justification, if it helps:
Suppose we were opening a bakery off in the exotic kingdom of Barlandia.
The smallest coin in Barlandia is the thunk; it can't be subdivided.
One donut costs 65 thunks.
In this case, which is more appropriate:
By the way, this is all step 1 of the design recipe — decide which type best represents the data in our program!
/** Some bakery price-calculating functions. * @author ??? * @see http://www.radford.edu/~itec120/2019fall-ibarland/Labs/lab03a.html */ class Bakery extends Object120 { /** Return the cost of a donut order, in cents. * @param count The number of donuts being ordered. * @return the list price for `count` donuts, including any discounts, *in cents*. */ // (Add your code here, *after* filling in the test cases below.) /** Test the above function. */ static void testDonutOrder() { System.out.println( "== donutOrder ==" ); System.out.println( "Actual: " + donutOrder( 0 ) ); System.out.println( "Desired: " + … ); System.out.println( "Actual: " + donutOrder( 1 ) ); System.out.println( "Desired: " + … ); System.out.println( "Actual: " + donutOrder( 2 ) ); System.out.println( "Desired: " + … ); System.out.println( "Actual: " + donutOrder( 9 ) ); System.out.println( "Desired: " + … ); System.out.println( "Actual: " + donutOrder( 10 ) ); System.out.println( "Desired: " + … ); System.out.println( "Actual: " + donutOrder(100 ) ); System.out.println( "Desired: " + 6175 ); // 6500¢ less 5% (325¢) } static void testAll() { testDonutOrder(); } } |
Hint: Recall the note at start of lab, booleans and if-else, how we can use a variable to hold the discount (either 5% or 0% (!!)), and then anif -else to initialize that variable, and then just a singlereturn statement which multiplies by the discount (or more technically, one minus the discount).
This page licensed CC-BY 4.0 Ian Barland Page last generated | Please mail any suggestions (incl. typos, broken links) to ibarland ![]() |