Class 9 — Wednesday, September 23
Monday’s Java, in a file in an editor instead of a browser tab, with Check.is instead of printing true. Then one new thing: text that can draw.
Outline
- Screens off first — one more thing caught before running
- Get the starter — pairs, at the keyboard
Check.is— Monday’s==, as a line that explains itself- Draw the stripe —
flagWidth, then a stripe made of characters - If you got there early — the whole flag
- Before Monday
1. Screens off first
Monday ended with a call that never runs: Pyret ran the whole program and said nothing, Java refused before running. One more mistake, in both languages — the function’s name spelled wrong:
print("starting")
stripe-widht(10)IO.println("starting");
stripeWidht(10);Both refuse. Pyret: The name stripe-widht is unbound, and starting never prints. Java: cannot find symbol, and starting never prints.
So checking ahead of time is not a Java thing. Pyret does it for names. Java does it for names and for types, and that is the change from Monday. In Pyret, u :: Number was yours to write or to leave out. In Java, every parameter and every result has a type, written down, no choice, and Java reads it before the program runs. The int on u is what refused stripeWidth("JAVA").
2. Get the starter
Watch it once on the projector first. Then everyone clones, on their own keyboard.
The starter is a repository: https://github.com/cu-cs-labs/cmsc-120-stripe.
- Open IntelliJ IDEA. On the welcome screen, press Get from VCS.
- Stay on the Repository URL tab, paste the address above. Leave the folder IDEA suggests:
IdeaProjectsinside your own user folder. That is where your files live on this machine. Press Clone. - When IDEA asks, trust the project, and take the JDK it offers.
- Open
src/Stripe.javaand press the green Run arrow besidemain.
Done when the output says Looks shipshape, all 2 checks passed.
Then, while others finish: in main, change stripeWidth to stripeWidht and watch what IDEA does before you press anything. Fix it.
No Run arrow beside main? Right-click src, Mark Directory as, Sources Root.
Git is only how the files reach you. Nothing to commit, no account, and nothing more about it today.
Somewhere without IDEA? The same two files are on OneCompiler: https://onecompiler.com/java/4544z33u6.
3. Check.is
Together, at your keyboard.
On Monday you checked stripeWidth by printing stripeWidth(10) == 60 and reading true. Four of those print true true false false, and you count lines to find the broken one.
Check.java sits beside your file, and it is Pyret’s is:
stripe-width(10) is 60Check.is(stripeWidth(10), 60);A check that fails says so, with what it got and what it expected.
There is no where: in Java. Your examples are lines in main, and they run when the program runs, top to bottom. That is why Check.done() is the last line: it counts what ran before it.
Change one expected value to something wrong, run, read the line, change it back. Do not edit Check.java.
4. Draw the stripe
Pairs from here. One keyboard; you will both get a turn — I will say when to swap.
Each function gets a // line above it and two Check.is lines in main, worked out before the body.
flagWidth
A flag is two stripes wide. Monday’s, and the warm-up.
int flagWidth(int u)flagWidth(10) is 120, flagWidth(5) is 60. It may call stripeWidth.
stripeRow
A stripe, drawn as a row of characters as wide as the stripe. Text can do this itself:
"#".repeat(6)is ######. repeat is something the text knows how to do; the dot is how you ask it. Same shape as Check.is: a thing, a dot, what to do.
String stripeRow(int u)stripeRow(1) is ######. Text knows how long it is, too:
Check.is(stripeRow(2).length(), 12);flagRow
One row of the flag: two stripes side by side, in two characters. + joins text.
String flagRow(int u)flagRow(1) is ######....... One of your checks is Check.is(flagRow(10).length(), flagWidth(10)) — the row is exactly as wide as Monday said a flag is.
Done when Check.done() prints Looks shipshape, and IO.println(flagRow(3)) after it shows a row of the flag.
If Java stops with a red error, read the line number first. ';' expected means that line needs a semicolon at the end. cannot find symbol means a name is spelled differently in two places.
5. If you got there early
flag
A stripe is u tall, so the flag is u rows. "\n" is the character that ends a line, and a row with one on the end can be repeated like any other text:
String flag(int u)flag(1) is "######......\n". Print flag(4) after Check.done() and look at it.
Work out flag(3).length() by hand, then check it.
row
flagRow with the two characters as parameters:
String row(int u, String a, String b)row(1, "#", ".") is ######....... Then rewrite flagRow as one line that calls row. Your checks for flagRow still pass if you kept its meaning; that is what they are for.
board
The flag, then under it the flag with its stripes swapped: a checkerboard of four.
String board(int u)board(1) is "######......\n......######\n". Print board(3).
6. Before Monday
Reading: [CU] 3.1 — Strings and [DCIC] 12.2 — Understanding Equality, before Monday’s class.
A4 is out today, due Tuesday, September 29. It opens with a short setup survey; do that part first.