Class 15 — Monday, October 19

Records: a value made of named parts, the functions that read the parts they need, and a record made of records.

Quiz 5 first. Then a value made of named parts — a record — and the functions that read the parts they need.

[CU] 5.4 — switch Statements and Enums and records were due today. A7 is due Tuesday, October 20. A week from today: Progress Exam 2 — Java, Monday, October 26.

Outline

  1. A value made of named parts — keyboards down
  2. Stripes — pairs, at the keyboard
  3. A record inside a record — keyboards down
  4. Flags — pairs again
  5. What a record prints — keyboards down
  6. Keep going — two more
  7. Before Wednesday

1. A value made of named parts

Keyboards stop.

A stripe of a flag is a color and a height. A record is a type whose values are made of named parts — one line, at the top of the file:

record Stripe(String color, int height) {}

new Stripe("red", 10) makes one, with the parts in the order the line lists them, and s.height() reads a part back. Nothing changes a record once it is made. The reading has the rest: Enums and records.

record Stripe(String color, int height) {}

void main() {
    Stripe a = new Stripe("red", 10);
    Stripe b = new Stripe("red", 10);
    IO.println(a.height());
    IO.println(a == b);
    IO.println(a.equals(b));
}

The first two lines print 10 and false: two news make two objects at two addresses, and == asks whether the names hold one address. What does the third print?

true. A record’s .equals compares its parts, one by one: both are "red" and both are 10. That is what Java makes .equals mean for every record: a record is its parts, and since they never change, it can be treated as a value, like a text.

What .equals does is up to each type. A builder has none of its own: it keeps the .equals every object starts with, which asks what == asks:

IO.println(new StringBuilder("ab").equals(new StringBuilder("ab")));   // false

2. Stripes

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 Flags.java, with the record Stripe line at the top and a main that ends in Check.done(). How to run Java has the steps.

Each function gets a // line above it with a purpose statement and at least two Check.is lines in main. A check makes the stripe it needs: Check.is(area(new Stripe("red", 10), 60), 600);.

area

int area(Stripe s, int width) — how much of a flag the stripe covers when it is width wide: its height times the width. area(new Stripe("red", 10), 60) is 600.

Done when two checks pass.

isThin

boolean isThin(Stripe s) — whether the stripe is less than 5 tall.

Done when checks at 4 and 5 pass.

describe

If you are done, here are two more.

String describe(Stripe s) — the stripe in words: describe(new Stripe("red", 10)) is "red, 10 tall".

Done when two checks pass.

sameColor

boolean sameColor(Stripe a, Stripe b) — whether the two stripes have the same color, whatever their heights. Two texts are compared with .equals.

Done when checks pass for two reds of different heights, and for a red and a white.

3. A record inside a record

Keyboards stop.

A flag of three stripes is a record too, and its parts are records:

record Stripe(String color, int height) {}
record Flag(Stripe top, Stripe middle, Stripe bottom) {}

void main() {
    Flag f = new Flag(new Stripe("red", 10), new Stripe("white", 20), new Stripe("red", 10));
    IO.println(f.middle().height());
}

What prints?

20. f.middle() is a Stripe, and .height() reads that stripe’s part: one dot at a time, from the left. A function that takes a Flag reads the parts it needs the same way: f.top().color() is "red".

4. Flags

Pairs, the same way: the navigator’s examples and steps first, then the driver’s Java.

The record Flag line goes under record Stripe in Flags.java.

height

int height(Flag f) — the height of the whole flag. For section 3’s f, 40.

Done when two checks pass, one on a flag whose stripes are not all the same height.

hasColor

boolean hasColor(Flag f, String c) — whether any of the three stripes has the color c.

Done when checks pass for a color that is only in the middle stripe, and for a color in none of them.

isSymmetric

If you are done, here are two more.

boolean isSymmetric(Flag f) — whether the flag looks the same upside down: its top stripe and its bottom stripe are equal. One line; section 1 says what .equals does on a record.

Done when checks pass for Austria’s flag, red, white and red at the same height, and for a flag whose top and bottom are both red but of different heights.

colors

String colors(Flag f) — the three colors, top to bottom: "red/white/red" for section 3’s f.

Done when two checks pass.

5. What a record prints

Keyboards stop.

With section 3’s f:

IO.println(f);
IO.println(f.top() == f.bottom());
IO.println(f.top().equals(f.bottom()));

What do the three lines print?

Flag[top=Stripe[color=red, height=10], middle=Stripe[color=white, height=20], bottom=Stripe[color=red, height=10]]
false
true

A record prints its parts, and a record inside it prints its own. The top and the bottom stripe are two news, so == is false; their parts are equal, so .equals is true. It is section 1 again, one level down.

6. Keep going

upsideDown

Flag upsideDown(Flag f) — the same flag turned upside down: the bottom stripe on top. It gives back a record, and Check.is has no form for a record, so the check asks .equals and expects true:

Check.is(upsideDown(f).equals(new Flag(..., ..., ...)), true);

Done when that check passes on a flag whose top and bottom differ, and a second checks that turning it upside down twice gives the flag back.

stripesOf

int stripesOf(Flag f, String c) — how many of the three stripes have the color c, from 0 to 3. Three names inside the function, one per stripe, each a ? : that is 1 or 0; then add them.

Done when checks at 0 and at 2 pass.

height, taken apart by its parts

A switch can take a record apart, the way Pyret’s cases took a list apart into link(f, r): its case names the parts as it reads them.

int area(Stripe s, int width) {
    return switch (s) {
        case Stripe(String color, int height) -> height * width;
    };
}

_ stands for a part the case does not need, case Stripe(_, int height), and a part that is itself a record can be taken apart in the same case: Flag(Stripe(_, int a), ..., ...). Write heightByParts(Flag f), the flag’s height, as one case that takes the flag apart down to its three heights.

Done when height’s checks pass on heightByParts too.

7. Before Wednesday

A7 is due Tuesday, October 20. Progress Exam 2 — Java, Monday, October 26: its page says what it covers.