Lesson 2.10
Capstone: several rules decide one answer
Part 2: DecisionsReading, about 4 min1 exercise in this lesson
Java answers can't run on Spine yet. You can still read Java, trace it, and put an answer's lines in order.
Planning a program where several rules decide one answer
By the end of this you will be able to plan a program where several rules work together to pick one answer, using only what you already know.
This lesson has no new idea. It puts the last nine lessons together. When several rules decide one answer, plan it in four steps, in this order:
- Read: put each input into its own variable, and turn the numbers into numbers.
- List the answers: write down every answer the program can print, and the rule that leads to each one.
- Order the rules: when two rules could both apply, decide which one wins, and test that one first.
- Write one chain, so exactly one answer prints. Then test each rule, on both sides of every boundary.
The four steps in one program
A ride at a fair has three rules. A rider shorter than 100 cm cannot ride at all. A rider who is 140 cm or taller can ride. A rider in between can ride only with an adult. The program reads the height in whole centimetres, then yes or no for whether an adult is with them, and prints one answer.
height = int(input())
adult = input()
if height < 100:
print("Too short")
elif height >= 140:
print("Ride")
elif adult == "yes":
print("Ride")
else:
print("Bring an adult")import java.util.Scanner;
public static void main(String[] args) {
Scanner scanner
= new Scanner(System.in);
int height = Integer.parseInt(
scanner.nextLine());
String adult = scanner.nextLine();
if (height < 100) {
System.out.println("Too short");
} else if (height >= 140) {
System.out.println("Ride");
} else if (adult.equals("yes")) {
System.out.println("Ride");
} else {
System.out.println(
"Bring an adult");
}
}If the user types 120 and then no, the program prints:
Bring an adult
Look at the order of the tests. A rider of 90 cm with an adult matches two rules: "shorter than 100" and "has an adult". The height rule must win, so it comes first. Once it is false, every later test knows the height is at least 100. By the time the adult test runs, the height is between 100 and 139, which is exactly the case where the adult matters.
Plan before you type
Before you write any code, make two lists on paper:
- The answers: every line the program can print.
- The rules: for each answer, the condition that leads to it, and which rule wins when two clash.
Then choose the shape. One else-if chain fits when exactly one answer prints and the rules have an order. A nested if fits when a second question only matters after the first one passes. A joined condition fits when two facts lead to the same answer.
Then test it. Pick values on both sides of each boundary: here 99 and 100, and 139 and 140. For the heights in between, try both yes and no. Each rule should win at least once.
A common mistake: testing the rules in the order the task lists them, without asking which one wins. If the adult test came first, a 90 cm rider with an adult would be told to ride.
Now try it yourself on the questions below.
Exercise 2.10.12.10.1 · Write itGrade report
Write a program that reads a mark (whole number) on the first line and an absence flag on the second line, and prints a grade report.
- Always print
Mark:followed by the mark on the first line. - If the absence flag is exactly
yes, printAbsenton the next line. Nothing else about the mark matters. - Otherwise, if the mark is 80 or above, print
Distinction. - Otherwise, if the mark is 50 or above, print
Pass. - Otherwise, print
Fail. - Print exactly one of those four result lines. Spaces at the end of a line are ignored.
The mark is always a whole number between 0 and 100. The absence flag is always yes or no.
Your program is run several times. Only what it prints is checked.
Running Java here comes later. You can put this answer's lines in order instead. You can still write your Java answer (it stays while this page is open) and compare it with our worked solution. Or try this exercise in Python.
Examples
Your program is run 8 times: the 4 examples below, and 4 hidden ones that check the rules above. Only what it prints is checked.
exactly 80 is Distinction, the boundary (rule 3)
You type
- typed: 80
- typed: no
It prints
Mark: 80 Distinction
a low mark, not absent (rule 5)
You type
- typed: 45
- typed: no
It prints
Mark: 45 Fail
absent overrides the mark (rule 2)
You type
- typed: 70
- typed: yes
It prints
Mark: 70 Absent
absent with a high mark still prints Absent (rule 2)
You type
- typed: 90
- typed: yes
It prints
Mark: 90 Absent