RU beehive logo ITEC dept promo banner
ITEC 120
2012fall
dbraffitt
ibarland

homeinfolectslabsexamshws
tutor/PIsbreeze (snow day)
Object120 + its docsjava.lang docsjava.util docs

lab03a
if-else
donuts prices

Delicious Sugarbomb Donuts cost 65¢ each; if you order 10 or more you get a 5% discount on all your donuts. “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:Since 0.65 is less than 1, likewise 0.65¢ is less than 1¢. Don't confused a dollar with a cent!
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 ints, and measuring in cents instead of dollars, lets us use the right type.

(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: ints or doubles?)

By the way, this is all step 1 of the design recipe — decide which type best represents the data in our program!

Starting code

/** Some bakery price-calcuating functions.
 * @author ???
 * @see http://www.radford.edu/~itec120/2012fall-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 testPrices() {
    System.out.println( "0 donuts:" );
    System.out.println( "Actual:  " + donutOrder( 0 ) );
    System.out.println( "Desired: " + ... );
    System.out.println( "1 donut:" );
    System.out.println( "Actual:  " + donutOrder( 1 ) );
    System.out.println( "Desired: " + ... );
    System.out.println( "2 donuts:" );
    System.out.println( "Actual:  " + donutOrder( 2 ) );
    System.out.println( "Desired: " + ... );
    System.out.println( "10 donuts:" );
    System.out.println( "Actual:  " + donutOrder( 10 ) );
    System.out.println( "Desired: " + ... );
    System.out.println( "100 donuts:" );
    System.out.println( "Actual:  " + donutOrder(100 ) );
    System.out.println( "Desired: " + 6175 );  // 6500¢ less 5% (325¢)


  }

}
Notes:

homeinfolectslabsexamshws
tutor/PIsbreeze (snow day)
Object120 + its docsjava.lang docsjava.util docs


©2012, Ian Barland, Radford University
Last modified 2012.Sep.18 (Tue)
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Powered by PLT Scheme