Class 11 — Wednesday, September 30

Objects you make with new, and the first one that changes in place.

Monday’s texts could not be changed: every method gave a new one back. Today you make an object with new, and the first one you make can be changed.

Nothing was due today. [CU] 3.2 — Creating Objects and [CU] 3.3 — Random Numbers are due before Monday, and Quiz 4 is on them.

Outline

  1. Where Check lives — in the drawing, and not
  2. new: predict, then run — a builder and a text, side by side
  3. Builders that give a text back — pairs, at the keyboard
  4. Two names, again — a stop in the middle
  5. Builders that change in place — pairs again
  6. Keep going — three more
  7. Before Monday

1. Where Check lives

Screens off.

directory          heap
w  →  60
r  →  1001         1001: "######"

Check is in neither column. It is a class: the place is lives, the way String is the place repeat lives. A class has no address, because it is not a thing the program made — it is part of the program.

"#" is a thing: an instance of String, at an address. "#".repeat(6) calls a method on an instance; Check.is(...) calls a method on a class. Coles 3.1.3 calls the first an instance method and the second a static method.

2. new: predict, then run

Write your prediction in the Journal before anyone runs anything.

Coles 3.2: every object except a text is made with new. new makes a thing in the heap and gives back its address.

StringBuilder a = new StringBuilder("FINLAND");
directory          heap
a  →  1007         1007: FINLAND

Two names, one builder; then two names, one text. What prints?

StringBuilder a = new StringBuilder("FINLAND");
StringBuilder b = a;
b.reverse();
IO.println(a);

String s = "FINLAND";
String t = s;
t = t.toLowerCase();
IO.println(s);
IO.println(t);
directory          heap
a  →  1007         1007: DNALNIF      reverse changed the thing at 1007
b  →  1007

s  →  1008         1008: "FINLAND"    toLowerCase made a new text, and
t  →  1009         1009: "finland"    t = ... moved t to it

reverse changes the builder it is called on. Coles 3.2.4 calls a method like that a mutator, and says a text has none: every String method gives a new text back and leaves the old one where it was.

a == b is true: one address.

3. Builders that give a text back

Pairs. One keyboard. The person not typing writes the next Check.is line — the expected value, worked out by hand — while the typist writes the body. Swap at every function.

In IDEA, next to Stripe.java: a new file Builders.java, with a main that ends in Check.done(). Check.java is already there. No project? Clone the starter again with last Wednesday’s steps, or use Monday’s OneCompiler page with Stripe.java emptied out: https://onecompiler.com/java/4544z33u6.

Each function gets a // line above it and two Check.is lines in main, worked out before the body. A builder is not a text, so a check on one goes through toString(): Check.is(b.toString(), "cba").

What a builder can do, from Coles 3.2.4 and its documentation: append, insert, delete, deleteCharAt, replace, reverse, length, toString. Positions count from 0, like substring.

mirror

String mirror(String s) — s followed by s backwards. mirror("abc") is abccba. Make a builder from s, reverse it, and join the two with +.

Done when both checks pass, and one of them is on "".

frame

String frame(String s) — s inside square brackets: frame("abc") is [abc]. insert(0, "[") puts text at the front; append puts it at the back.

Done when both checks pass.

chop

String chop(String s) — s without its last character: chop("abc") is ab. deleteCharAt takes a position, and the last one is length() - 1.

Done when both checks pass, and one of them is on a one-character text.

rotate

String rotate(String s, int k) — the first k characters moved to the end: rotate("abcde", 2) is cdeab. append a substring, then delete(0, k).

Done when rotate("abcde", 0) checks out as well.

4. Two names, again

Keyboards stop. Predict first.

StringBuilder p = new StringBuilder("ab");
StringBuilder q = p;
q.append("c");
IO.println(p);
IO.println(p == q);

Then a question: flip, below, turns a builder around and gives nothing back. Where does its check look?

5. Builders that change in place

Pairs, the same way: the next Check.is line first, then the body.

flip

void flip(StringBuilder b) — turns b around, in place. Nothing comes back: void is Java’s word for that, and the function has no return.

StringBuilder x = new StringBuilder("abc");
flip(x);
Check.is(x.toString(), "cba");

The check is on x after the call. The caller’s builder changed.

Done when the check passes, and you can say why Check.is(flip(x), "cba") cannot be written.

shout

void shout(StringBuilder b) — puts b in capitals, in place. toUpperCase is a String method and a builder has no such thing, so b.toString().toUpperCase() is the text you need, and replace(0, b.length(), ...) puts it where the old one was.

Done when quokka comes out as QUOKKA.

twice

void twice(StringBuilder b) — doubles b in place: ab becomes abab, and a second call makes abababab.

Done when two calls in a row check out.

6. Keep going

isPalindrome

boolean isPalindrome(String s) — true when s reads the same backwards: racecar does, quokka does not. reverse gives a builder; the text is toString(), and texts compare with equals.

swapEnds

String swapEnds(String s) — the first and last characters exchanged: swapEnds("quokka") is auokkq, swapEnds("ab") is ba. Two replaces, or substrings and + — write it both ways and keep the one you can read.

dash

void dash(StringBuilder b) — a dash after the first character, in place: abc becomes a-bc.

7. Before Monday

[CU] 3.2 — Creating Objects and [CU] 3.3 — Random Numbers, before Monday: Quiz 4 is on them, twice. A5 is out today and due Tuesday, October 6.