Class 13 — Wednesday, October 7
The if you wrote in Pyret gave a value back. Java’s does something instead, and that changes how a function hands its answer out.
Nothing was due today. [CU] 5.1 — Boolean Expressions, [CU] 5.2 — if Statements, [CU] 5.3 — if-else Statements and [CU] 5.5 — Logical Operators go out with this class and are due before Wednesday, October 14 — Monday is fall break. A5 was due last night.
Outline
if, word for word — predict, then run- Decisions that give a value back — pairs, at the keyboard
and,or,not— a stop in the middle- Decisions with two questions in them — pairs again
- Keep going — two more
- Before Wednesday the 14th
1. if, word for word
Write your prediction in the Journal before anyone runs anything.
Monday’s quiz had a fare in Pyret: 5 for two kilometres or less, and 5 plus 2 a kilometre beyond that. Pyret on the left; on the right, word for word in Java.
Does Java take it? If not, which line does it object to?
Java stops before running anything:
In Pyret, if is an expression: the whole if … end is 5, or 11, and that value is the function’s answer. In Java, if is a statement: it runs one branch or the other, and a branch holds statements too. 5; on its own does nothing, and Java says so.
What hands a value out of a Java function is return, a statement of its own:
Java also has an if that is an expression — ? :, three parts, one line, and a value:
Leave the else off the first form and Java says missing return statement: every way through the function has to reach a return.
The inventory, in short. Values: numbers, true and false, texts — an object that never changes can be treated as a value — and addresses. Expressions have a value: km <= 2, "#".repeat(6), roll(r), ? :. Statements do something and are nothing: int x = 3;, b.reverse();, IO.println(x);, return x;, if. What they do something to is the program’s state: the directory, the heap, the output. The whole of it, Pyret beside Java: Expressions, statements, and values.
2. Decisions that give a value back
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 Dice.java: a new file Decisions.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 examples to write first are the ones at the edge: the last number one branch takes, and the first number the next one takes.
fare
int fare(int km) — section 1’s fare, with if and else. fare(2) is 5.
Done when two checks pass, and one of them is fare(3).
nightFare
int nightFare(int km, boolean night) — 5 for two kilometres or less; after that, 5 plus 3 a kilometre at night and 5 plus 2 a kilometre by day. nightFare(3, true) is 14. Three branches: if, else if, else.
Done when three checks pass, and one of them is nightFare(2, true).
bigger
int bigger(int a, int b) — the larger of the two. bigger(3, 8) is 8. One line, with ? : and no if.
Done when two checks pass, and one of them has the larger number first.
3. and, or, not
Keyboards stop. Predict first.
Pyret’s and, or and not are Java’s &&, || and !:
km > 2 && night // both hold
day.equals("Sat") || day.equals("Sun") // at least one holds
!night // the oppositeA rule for passing: at least one of two scores over 90, and the lower of the two over 70. Here is the rule as a function, and a student with 95 and 65:
boolean passes(int s1, int s2) {
return s1 > 90 || s2 > 90 && (s1 < s2 ? s1 : s2) > 70;
}
void main() {
IO.println(passes(95, 65));
}The rule says no. What prints?
true. && goes first, like * before +, so Java read the line as s1 > 90 || (s2 > 90 && … > 70) — and 95 > 90 settles that on its own. The rule wanted the other grouping, and parentheses are how you say so:
Names. That line is right and still hard to read, because three questions sit in it unnamed. A name inside a function is a line of its own, int min = …;, as int first = roll(r); was on Monday. Name the lower score, then each of the two questions:
boolean passes(int s1, int s2) {
int min = s1 < s2 ? s1 : s2;
boolean oneHigh = s1 > 90 || s2 > 90;
boolean lowerOk = min > 70;
return oneHigh && lowerOk;
}Now the last line reads as the rule, and the grouping is in the names instead of in parentheses you have to remember.
4. Decisions with two questions in them
Pairs, the same way: the navigator’s examples and steps first, then the driver’s Java.
isWeekend
boolean isWeekend(String day) — whether day is "Sat" or "Sun". isWeekend("Sun") is true. Two texts are compared with .equals; == asks whether they are one thing (Class 10).
Done when two checks pass, one true and one false.
canRun
boolean canRun(boolean raining, int temp) — a run happens when it is not raining and it is at least 50 degrees. canRun(false, 50) is true.
Done when three checks pass, and one of them is at 50.
passes
boolean passes(int s1, int s2) — section 3’s rule, with a name for the lower score and one for each of the two questions. passes(95, 65) is false.
Done when three checks pass, and one of them is a pass.
isWeekday
boolean isWeekday(String day) — the opposite of isWeekend, in one line that calls it.
Done when two checks pass.
5. Keep going
letter
String letter(int score) — "A" from 90 up, "B" from 80 up, "C" below that. letter(90) is "A".
Done when checks at 90, 89, 80 and 79 pass.
clamp
int clamp(int x, int lo, int hi) — x, pulled up to lo if it is below and down to hi if it is above. clamp(12, 1, 10) is 10. Two ? : in one line, or an if-else if-else: write it both ways and keep the one you can read.
Done when three checks pass, and one of them has x inside the range.
6. Before Wednesday the 14th
No class Monday: fall break is Monday and Tuesday. A6 is out today and due Tuesday, October 13. The reading, [CU] 5.1 — Boolean Expressions to [CU] 5.3 — if-else Statements and [CU] 5.5 — Logical Operators, is due before the next class, Wednesday, October 14.