Exercise 3 of 10 · Arithmetic
Party Planner
What you will make
A pizza order summary that works itself out from the guest list — slices needed, whole pizzas, and the slices that spill past them.
The one new idea: Java splits division into two operators: / for whole groups, % for what's left over
Counting things into whole groups turns up everywhere: pizzas, pages, boxes, minutes into hours. Java has an operator for each half of that question, and reaching for the right one is what keeps a program from quietly promising a customer 0.625 of a pizza.
Go straight to the code ↓Arithmetic, and two kinds of division
Java does sums roughly as you would on paper, with a couple of symbols swapped in: + adds, - subtracts,
* multiplies using a star rather than an x. So guests * slicesEach means the guests multiplied by the
slices each, worked out the moment javac's compiled code runs that line.
Division is where Java hands you a choice, and that choice is what this exercise is about.
Picture seventeen apples shared between five friends. There are two sensible answers, not one. The first is "three each" — how many whole apples everybody walks away with. The second is "two left in the bowl" — the ones that could not be shared out evenly. Java gives you an operator for each:
/between twointvalues counts the whole groups, so17 / 5is3%reports what is left over, so17 % 5is2
Both hand back plain whole numbers, which is exactly what you want when the things being counted cannot
sensibly be cut in half — apples, chairs, boxes, pizzas. This is different from a language where dividing
always gives you a decimal by default: in Java, int / int gives an int, full stop, and the fractional part
is simply thrown away rather than rounded.
The trap: widening happens after, not before
It is tempting to think that storing the answer in a double will bring the fractional part back. It will
not, on its own:
int a = 17;
int b = 5;
double each = a / b;
System.out.println(each);3.0a / b is worked out first, as int division, and the answer is 3 before a decimal point has ever entered
the picture. Only after that whole-number 3 is worked out does Java widen it to fit in the double variable
— which is how you get 3.0 rather than 3.4. If you actually want the decimal answer, one of the two
numbers has to become a double before the division happens, for example (double) a / b. That is a detail
worth knowing rather than needing here — this exercise wants whole pizzas, and int division is exactly
right for it.
A worked example
public class Main {
public static void main(String[] args) {
int apples = 17;
int friends = 5;
int each = apples / friends;
int over = apples % friends;
System.out.println("Apples each .. " + each);
System.out.println("Left in bowl . " + over);
}
}Apples each .. 3
Left in bowl . 2Two operators, the same pair of numbers, two different and equally true answers. The rows of dots are nothing clever — they are part of the text inside the quotes, doing the job of the ruled line on a paper form.
Your turn
The editor has a party to plan. Twenty-three guests are coming, each one will eat three slices, and a pizza turns up cut into eight.
The program already multiplies the guests by the slices to get the total number of slices wanted, and it already prints the whole summary block. Two lines are unfinished:
int pizzas = 0;
int extra = 0;Replace each 0 with a sum built from slicesNeeded and perPizza. Give pizzas the number of whole
pizzas the order fills, and extra the slices that spill past them.
Then run it and read the block back. If the last two numbers still show 0, nothing has been changed yet — the program runs perfectly happily with zeros in it, it just plans a party with no food.
When it works you get a genuine answer: eight whole pizzas cover sixty-four slices, and five slices are still wanted on top. In real life you would order the ninth pizza and have three slices going spare.
If something goes wrong
The likeliest slip is reaching for a decimal to fix a whole-number answer. If a number turns up as 8.0
rather than a plain 8, a variable further up was probably declared double instead of int — and as the
box above explains, that alone will not bring the fractional part back, because the division already happened
as whole numbers before the widening.
The other thing worth watching is the two operators landing the wrong way round. Nothing breaks and the block prints as neatly as ever, but the figures stop hanging together — leftover slices should always come to fewer than eight, since eight slices is a whole pizza hiding inside the count.
Change it, run it, change it again. Nothing here can break.
Write your code
Runs in your browser. Press Run (or Ctrl/Cmd+Enter) and the output is checked for you.
Press Esc then Tab to move keyboard focus out of the code editor.
Output will appear here after you run your code.The runtime is starting in the background. You can type now — it will be ready before you are.
The answer appears here once you have run your code at least once.
Things that often go wrong here
- Declaring pizzas as a double instead of an int
- double pizzas = slicesNeeded / perPizza; still prints 8.0, not 8.625, because slicesNeeded and perPizza are both int — Java works out the whole division first and only widens the answer to a decimal afterwards. Making a decimal appear means converting a number before the division happens, not after it.
- Putting the two operators the wrong way round
- Nothing stops the program running, but the numbers stop making sense: swap them here and the summary claims 5 whole pizzas and 8 extra slices, and 5 pizzas cannot feed 23 people while 8 leftover slices is a whole pizza that should have been counted.
- Deleting the 0 and leaving the line unfinished
- A line reading int pizzas = ; is an unfinished statement. javac stops and reports illegal start of expression, pointing at the semicolon it found where a value was expected.
- Forgetting the int keyword on one of the two lines
- pizzas = slicesNeeded / perPizza; without int in front of it is an assignment to a variable that was never declared, so javac reports cannot find symbol — the type keyword is what introduces the name in the first place.
Want a blank editor instead? Open the Java playground.