RU beehive logo ITEC dept promo banner
ITEC 120
2019fall
asbrennem
ibarland

functions calling functions
buffet price, and valentine's day

  1. Krusteaze Pizza Korp. has an all-you-can-eat buffet meal. In order to encourage people to bring their friends, the price is $8 for the first person, plus $4.50 for each friend after that.

    We will write a function buffetPrice, which is given the size of the group (1 or bigger), and returns the total cost. (Your function will be programmed into the cash register.)

    Here is code you/template can paste in; you ust

    class Pizzeria {
    
    
        /* Your stub for buffetPrice will go here ... AFTER you fill-in-blanks in the test-cases below. */
    
    
    
    
    
    
    
        
        static void testBuffetPrice() {
            System.out.println("======== test buffetPrice ========");
    
            System.out.println( "Actual:  " + buffetPrice(2) );
            System.out.println( "Desired: " + (8.00 + 4.50) );
    
            System.out.println( "Actual:  " + buffetPrice(3) );
            System.out.println( "Desired: " + (8.00 + 4.50 + 4.50) ); // a.k.a. 8.00 + 4.50*2
    
            System.out.println( "Actual:  " + buffetPrice(17) );
            System.out.println( "Desired: " + (8.00 + 4.50*      ) );
    
            System.out.println( "Actual:  " + buffetPrice(    ) );
            System.out.println( "Desired: " + 8.00 );
        }
       
        
        
        /** A "top-level" tester function, which simply calls all our other test-functions.
         *  Mostly, this just makes it easier to call our tests.
         */
        static void testAll() {
            System.out.println("======== test all ========");
            testPizzaArea();
            testBuffetPrice();
        }
    
    
        
        
        /** Return the area of the of pizza (in sq.in), whose diameter is `diam` (in inches).
         */
        static double pizzaArea( double diam ) {
            double rad = diam/2.0;
            return Math.PI * Math.pow( rad, 2.0 );
        }
    
    
        static void testPizzaArea() {
            System.out.println("======== test pizzaArea ========");
    
            System.out.println( "Actual:  " + pizzaArea(0) );
            System.out.println( "Desired: " + 0.0 );
    
            System.out.println( "Actual:  " + pizzaArea(2) );
            System.out.println( "Desired: " + Math.PI );
    
            System.out.println( "Actual:  " + pizzaArea(20) );
            System.out.println( "Desired: " + Math.PI * 10.0 * 10.0 );
    
            System.out.println( "Actual:  " + pizzaArea(14) );
            System.out.println( "Desired: " + Math.PI * 7.0 * 7.0 );
        }
            
        
        
        
    }    
          

  2. In order to get more customers, Krusteaze Pizza Korp. has decided run a Valentine's Day Special:

    1. It's like two-people-for-the-price-of-one buffet, except that if there is an “odd person” extra, they get their buffet for free!;
    2. then, 14% is added to the total.

    For example, if 6 people go to the Valentine's Day buffet, the cost will just be what it normally is for 3 people. And even if 7 people go, the price is still just 3-normal-priced-buffets!
    Assume that there are at least two people for this deal. That is, valentinesDaySpecial(1) might give a mildly-strange result; we are simply not going to worry about that for now.

    Hint 1: Remember that, in Java, 7/2 is 3 — if you give / two ints, it does “integer division” a.k.a. “quotient” a.k.a. “playground division”.
        static void testValentinesDaySpecial() {
            System.out.println("======== test valentinesDaySpecial ========");
    
            System.out.println( "Actual:  " + valentinesDaySpecial(2) );
            System.out.println( "Desired: " + buffetPrice(1) * 1.14 );
    
            System.out.println( "Actual:  " + valentinesDaySpecial(6) );
            System.out.println( "Desired: " + buffetPrice(    /2)  * 1.14);
    
            System.out.println( "Actual:  " + valentinesDaySpecial(7) );
            System.out.println( "Desired: " +                                                                      );
        }
        
        
        static void testAll() {
            System.out.println( "======== run all our tests ========" );
            testBuffetPrice();
            testValentinesDaySpecial();
        }
          
  3. Make sure that the code for valentinesSpecial does not repeat any code that was already inside buffetPrice: It should call that function instead.
    So: if necessary, modify (refactor) valentinesSpecial so that it calls buffetPrice. Note that your test-cases shouldn't be modified at all.
  4. Once valentinesSpecial's is just one line (which includes a call to buffetPrice, you can submit your .java file to the lab02c dropbox on D2L.

  5. Challenge/Optional: It was weird, in the above, that the last/odd person got a free meal. More traditionally, restaurants have “buy 1, get 1 free" deals, where 6 people would pay the price of three regular-buffets, but 7 people would pay the price of four regular-buffets (as would 8 people with such a coupon).

    Write buy1get1free, which behaves the way such deals tend to work in the real world.

    Hint: Here are two possible approaches to the arithmetic:
    1. You could still use integer-division (“quotient”), and then you'd need to figure if there was anybody left over: recall that Java’s “%” computes the remainder. So 6/2 is three, and 6%2 is zero (nobody left over). But 7/2 is three, and 7%2 is one (one person left over, who pays full-price).
    2. If you do “real” division with doubles, you can then use a standard function, Math.ceil, to “go up to the next integer”: Math.ceil(3.5) is 4.0.
      • If you need to convert an int into a double, you can use Object120.toDouble.
      • Likewise: if you need hto convert a double into an int, you can use Object120.toInt.
    For example, toInt( Math.ceil( toDouble(7)/toDouble(2) ) ) returns 4. You do not need an if statement to do this (so don’t!).

  6. Challenge/Optional II: Write the function buyNget1free, which is a coupon “for every n you buy, you get 1 free”. This function takes two inputs: how many people are arriving at the Pizzeria (like before), but before that it includes n — how many you need to buy, to get 1 free.

      
            System.out.println( "Actual:  " + buyNGet1Free(1,6) ); // buy-1-get-1-free, for 6 people.
            System.out.println( "Desired: " + buffetPrice(3) );
            System.out.println( "Actual:  " + buyNGet1Free(1,7) ); // buy-1-get-1-free, for 6 people.
            System.out.println( "Desired: " + buffetPrice(4) );
    
            System.out.println( "Actual:  " + buyNGet1Free(2,6) ); // buy-2-get-1-free, for 6 people.
            System.out.println( "Desired: " + buffetPrice(4) );    // so we pay for 4, to get enough for 6!
            System.out.println( "Actual:  " + buyNGet1Free(2,7) ); // buy-2-get-1-free, for 7 people.
            System.out.println( "Desired: " + buffetPrice(5) );    // 7th person requires buying an add'l.
            System.out.println( "Actual:  " + buyNGet1Free(2,8) ); // buy-2-get-1-free, for 8 people.
            System.out.println( "Desired: " + buffetPrice(6) );    // 8th person also requires buying an add'l.
            System.out.println( "Actual:  " + buyNGet1Free(2,9) ); // buy-2-get-1-free, for 9 people.
            System.out.println( "Desired: " + buffetPrice(6) );    // 9th person gets theirs free!
            
            
            
            System.out.println( "Actual:  " + buyNGet1Free(17,4) );  // buy-17-get-1-free, w/ four people.
            System.out.println( "Desired: " + buffetPrice(4) );      // same as 4 people would be w/o the coupon!
            System.out.println( "Actual:  " + buyNGet1Free(17,17) ); // buy-17-get-1-free, w/ seventeen people.
            System.out.println( "Desired: " + buffetPrice(17) );     // same as 17 people would be w/o the coupon.
            System.out.println( "Actual:  " + buyNGet1Free(17,18) ); // buy-17-get-1-free, w/ eightteen people.
            System.out.println( "Desired: " + buffetPrice(17) );     // same as 17 people would be w/o the coupon.
            System.out.println( "Actual:  " + buyNGet1Free(17,19) ); // buy-17-get-1-free, w/ nineteen people.
            System.out.println( "Desired: " + buffetPrice(18) );     // same as 18 people would be w/o the coupon.
          
    Add the lines above inside a function testBuyNGet1Free, and you can also add a call to this from testAll

    After getting this function working, go back and re-factor your code for buy1get1free so that is a one-line call to buyNget1free! (It'll pass all its tests-cases, of course.)


logo for creative commons by-attribution license
This page licensed CC-BY 4.0 Ian Barland
Page last generated
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Rendered by Racket.