Class 14 — Wednesday, October 14
A chain of ifs asks its questions in order, so the order is part of the rule. Then a type whose values are all listed, and the decision that answers for each of them.
A6 was due last night, and [CU] 5.1 — Boolean Expressions to [CU] 5.3 — if-else Statements and [CU] 5.5 — Logical Operators today. [CU] 5.4 — switch Statements and Enums and records go out with this class and are due before Monday’s, which opens with Quiz 5.
Outline
- A chain asks in order — predict, then run
- Chains with edges — pairs, at the keyboard
- A type with seven values — keyboards down
- An answer for every value — pairs again
- Keep going — two more
- Before Monday
1. A chain asks in order
Write your prediction in the Journal before anyone runs anything.
A phone greets you by the hour: Good morning before noon, Good afternoon before six, Good evening after that. Here is that rule as a chain, asked at nine in the morning:
// the greeting for an hour of the day, 0 to 23
String greeting(int hour) {
if (hour < 18) {
return "Good afternoon";
} else if (hour < 12) {
return "Good morning";
} else {
return "Good evening";
}
}
void main() {
IO.println(greeting(9));
}What prints?
Good afternoon. A chain asks its questions from the top and stops at the first one that is true. 9 < 18 is, so return "Good afternoon" runs, and hour < 12 is never asked.
No hour reaches "Good morning". Every hour before 12 is also before 18, and the first line takes them all. Each line of a chain is asked only about what the lines above it let through — so the narrower question goes first, hour < 12; then hour < 18 is only ever asked about hours from 12 up, and means 12 to 17 without saying so.
The examples to write first are at the edges, one on each side: 11 and 12, 17 and 18.
The same order holds inside && and ||. Whether the quizzes so far average at least 70, from their total and how many there were:
passing(0, 0) is false: count > 0 is false, and && stops at the first false, so total / count is never worked out. Swap the two, (total / count >= 70) && (count > 0), and 0 / 0 stops the program before count > 0 is reached. The question on the right is asked only about what the question on the left let through, like the second line of a chain. || is the same the other way round: it stops at the first true.
2. Chains with edges
Pairs. One keyboard. The navigator, who is not typing, says the examples first — each Check.is line, its answer worked out before any of the function exists — and then the steps the function takes. The driver types both, and decides how each step becomes Java. Swap at every function. If either of you is stuck, put away the roles and the keyboard for a moment and discuss.
What you write with your partner in class may go in both your files.
In IDEA, next to Check.java: a new file Cinema.java, with a main that ends in Check.done(). How to run Java has the steps, and OneCompiler for anyone without the project.
Each function gets a // line above it with a purpose statement, and at least two Check.is lines in main: the edges first.
greeting
String greeting(int hour) — section 1’s, in an order that works, and with a fourth greeting: "Good night" from 22. greeting(9) is "Good morning".
Done when checks at 11, 12, 17, 18, 21 and 22 pass.
ticket
int ticket(int age) — the price of a cinema ticket: under 4, free; under 12, 6; 65 and over, 8; everyone else, 12. ticket(2) is 0.
Done when six checks pass, one on each side of every edge: 3 and 4, 11 and 12, 64 and 65.
isTeen
boolean isTeen(int age) — whether age is 13 to 19, both included. isTeen(19) is true. Write return 13 <= age <= 19; first, and read what Java says about it. Then: a range is two questions, joined by &&.
Done when checks at 12, 13, 19 and 20 pass.
plural
If you are done, here are two more.
String plural(int n, String word) — the number and the word, with an s on the word unless the number is 1. plural(3, "seat") is "3 seats".
Done when checks at 0, 1 and 2 pass.
outside
boolean outside(int x, int lo, int hi) — whether x is below lo or above hi. outside(10, 5, 9) is true. Write the body with ! in front of a question that has && in it, and run the checks; then write it again with || and no !, and run the same checks. Both pass. Keep the || one: it says the // line’s rule in the line’s own words. ! earns its place when the inside already has a name — !inside(x, lo, hi), as Class 13’s isWeekday was !isWeekend(day).
Done when checks at 4, 5, 9 and 10, with lo 5 and hi 9, pass on both bodies, and the || one is the body left in the file.
3. A type with seven values
Keyboards stop.
A6’s isWeekend takes the day as a text, and a text can be any text:
boolean isWeekend(String day) {
return day.equals("Sat") || day.equals("Sun");
}
void main() {
Check.is(isWeekend("Sat"), true);
Check.is(isWeekend("sat"), true);
Check.done();
}Java took "sat" without a word, as it would take "Saturday" or "Stuarday". Nothing in the program says which seven texts are days.
An enum lists the values a type has, and those are all the values it has. One line, at the top of the file:
Day.SAT is a Day. Day.sat is not, and Java says so before anything runs: cannot find symbol.
switch answers for each value:
// whether d is Saturday or Sunday
boolean isWeekend(Day d) {
return switch (d) {
case SAT, SUN -> true;
case MON, TUE, WED, THU, FRI -> false;
};
}Like ? :, this switch is an expression: it has a value, return hands it out, and the ; after its } ends the return.
Take FRI out of the last case. Does Java refuse the program — or run it, and if so, what is isWeekend(Day.FRI)?
Java refuses before anything runs:
Every Day has to reach an answer. It is the rule behind missing return statement — every way through a function reaches a return — now for a switch. So an enum’s switch lists every value and has no default: when one is missing, Java finds it. More in the reading, Enums and records.
4. An answer for every value
Pairs, the same way: the navigator’s examples and steps first, then the driver’s Java.
The enum Day line goes at the top of Cinema.java, above the functions.
isWeekend
boolean isWeekend(Day d) — section 3’s, with a switch. isWeekend(Day.SUN) is true.
Done when two checks pass, one true and one false.
openHour
int openHour(Day d) — the hour the cinema opens: 10 on Saturday and Sunday, 14 on Friday, 16 on the other days. openHour(Day.FRI) is 14.
Done when three checks pass, one for each hour.
dayName
If you are done, here are two more.
String dayName(Day d) — the day’s full name. dayName(Day.WED) is "Wednesday".
Done when two checks pass.
isOpenAt
boolean isOpenAt(Day d, int hour) — whether the cinema is open at hour on d: from openHour(d) up to 23, both included. isOpenAt(Day.SAT, 9) is false.
Done when checks pass at the opening hour and at the hour before it, on two different days.
5. Keep going
ordinal
String ordinal(int n) — n with its ending: "1st", "2nd", "3rd", "4th", then "11th", "12th", "13th", but "21st". n % 10 is the last digit and n % 100 the last two: 112 % 10 is 2, and 112 % 100 is 12. Which question has to come first in the chain?
Done when checks at 1, 2, 3, 4, 11, 12, 13, 21 and 112 pass.
daysIn
int daysIn(int month) — the number of days in a month, in a year that is not a leap year: 30 in April, June, September and November, 28 in February, 31 in the rest. A switch on the month, as in [CU] 5.4 — switch Statements, with case 4, 6, 9, 11 -> 30;. A month is a number, and no list of cases names every number, so this switch ends with a default.
Done when checks for February, April and December pass.
6. Before Monday
A7 is out today and due Tuesday, October 20. The reading, [CU] 5.4 — switch Statements and Enums and records, is due before Monday’s class, which opens with Quiz 5.