![]() |
![]() |
|
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
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 ); } } |
In order to get more customers, Krusteaze Pizza Korp. has decided run a Valentine's Day Special:
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,
Hint 1: Remember that, in Java,7/2 is3 — if you give/ twoint s, 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: " + |
Write
Hint: Here are two possible approaches to the arithmetic:
- 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. So6/2 is three, and6%2 is zero (nobody left over). But7/2 is three, and7%2 is one (one person left over, who pays full-price).- If you do “real” division with
double s, you can then use a standard function,Math.ceil , to “go up to the next integer”:Math.ceil(3.5) is4.0 .
- If you need to convert an
int into adouble , you can useObject120.toDouble .- Likewise: if you need hto convert a
double into anint , you can useObject120.toInt .For example, You do not need antoInt( Math.ceil( toDouble(7)/toDouble(2) ) ) returns 4.if statement to do this (so don’t!).
Challenge/Optional II: Write the function
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. |
After getting this function working, go back and
re-factor your code for
This page licensed CC-BY 4.0 Ian Barland Page last generated | Please mail any suggestions (incl. typos, broken links) to ibarland ![]() |