Practice: a pizza party splitter
Games night, and somebody has to work out how many pizzas to order and what everyone owes. It is the same argument every month. You are going to settle it with a program.
The shop sells one size: 8 slices, 13.50 a pizza, whole pizzas only.
Everything here uses Chapters 1 to 3: variables, IO.readln, Integer.parseInt, arithmetic, / and %, Math.pow, String.format, Math.round, and the comparison operators. There is still no if—that is Chapter 4—so where this asks a yes-or-no question, print the comparison itself and let it show true or false.
One trick you will need in step 2. Ordering whole pizzas means rounding up, and / on two ints drops the fraction, which for these numbers rounds down. Adding 7 before dividing by 8 fixes that: 15 slices becomes 22 / 8, which is 2. Any number of slices that is already a multiple of 8 is unaffected.
Steps
Step 1. Ask how many people are coming and how many slices each one eats, then print the total number of slices needed. Both answers are whole numbers.
Show a solution
void main() {
int people = Integer.parseInt(IO.readln("How many people? ").strip());
int each = Integer.parseInt(IO.readln("Slices each? ").strip());
int slices = people * each;
IO.println("You need " + slices + " slices");
}
Both answers go through Integer.parseInt. Without it, IO.readln gives back text, and javac refuses to multiply two Strings: bad operand types for binary operator '*'.
Step 2. Print how many whole pizzas to order, rounding up as described above.
Show a solution
void main() {
int people = Integer.parseInt(IO.readln("How many people? ").strip());
int each = Integer.parseInt(IO.readln("Slices each? ").strip());
int slices = people * each;
int pizzas = (slices + 7) / 8;
IO.println("Order " + pizzas + " pizzas");
}
Plain slices / 8 rounds down, which orders one pizza for fifteen slices and leaves someone hungry. The + 7 rounds up without needing an if.
Step 3. Print how many slices will be left in the boxes afterward—that is, the slices ordered minus the slices eaten.
Show a solution
void main() {
int people = Integer.parseInt(IO.readln("How many people? ").strip());
int each = Integer.parseInt(IO.readln("Slices each? ").strip());
int slices = people * each;
int pizzas = (slices + 7) / 8;
IO.println((pizzas * 8 - slices) + " slices left over");
}
What is left is what was ordered minus what is eaten: two pizzas are 16 slices, and 15 are eaten. slices % 8 answers a different question and happens to agree sometimes, which is worse than always being wrong.
Step 4. Print what the order costs at 13.50 a pizza, to two decimal places. String.format with %.2f does the formatting.
Show a solution
void main() {
int people = Integer.parseInt(IO.readln("How many people? ").strip());
int each = Integer.parseInt(IO.readln("Slices each? ").strip());
int slices = people * each;
int pizzas = (slices + 7) / 8;
double cost = pizzas * 13.50;
IO.println("That comes to " + String.format("%.2f", cost));
}
%.2f shows money properly, so 27.0 prints as 27.00.
Step 5. Print what each person owes, split evenly and rounded to the nearest penny.
Show a solution
void main() {
int people = Integer.parseInt(IO.readln("How many people? ").strip());
int each = Integer.parseInt(IO.readln("Slices each? ").strip());
int slices = people * each;
int pizzas = (slices + 7) / 8;
double cost = pizzas * 13.50;
IO.println("Each of you owes " + String.format("%.2f", cost / people));
}
Split between people, not between slices. Both are numbers the program has, which is exactly how that mistake happens. %.2f rounds to the nearest penny as it displays.
Step 6. Print whether a single pizza would have been enough, as a comparison that shows true or false.
Show a solution
void main() {
int people = Integer.parseInt(IO.readln("How many people? ").strip());
int each = Integer.parseInt(IO.readln("Slices each? ").strip());
int slices = people * each;
IO.println("Would one pizza have done? " + (slices <= 8));
}
A comparison is a value in its own right and can be printed. The parentheses make Java compare first and join the answer on afterward. Chapter 4 puts the same expression inside an if.
Step 7. The shop adds 15% for delivery. Print the new total and the new share each, both to two decimal places.
Show a solution
void main() {
int people = Integer.parseInt(IO.readln("How many people? ").strip());
int each = Integer.parseInt(IO.readln("Slices each? ").strip());
int slices = people * each;
int pizzas = (slices + 7) / 8;
double cost = pizzas * 13.50;
double delivered = cost * 1.15;
IO.println("With delivery: " + String.format("%.2f", delivered));
IO.println("Each: " + String.format("%.2f", delivered / people));
}
Multiplying by 1.15 adds the 15% and keeps the original. Multiplying by 0.15 throws the pizza away and bills for the delivery.
Step 8. Put it together: ask for the two numbers, then print the slices needed, the pizzas to order, the slices left over, the cost, the delivery total, and what each person owes with delivery included.
Show a solution
// Settles games night: pizzas to order, leftovers, and what everyone owes.
void main() {
int people = Integer.parseInt(IO.readln("How many people? ").strip());
int each = Integer.parseInt(IO.readln("Slices each? ").strip());
int slices = people * each;
int pizzas = (slices + 7) / 8;
double cost = pizzas * 13.50;
double delivered = cost * 1.15;
IO.println("Slices needed: " + slices);
IO.println("Pizzas to order: " + pizzas);
IO.println("Slices left: " + (pizzas * 8 - slices));
IO.println("Cost: " + String.format("%.2f", cost));
IO.println("With delivery: " + String.format("%.2f", delivered));
IO.println("Each of you: " + String.format("%.2f", delivered / people));
}
Nothing new, only arrangement. The labels are padded with spaces so the numbers line up in a column.