Class 17 — Monday, October 26

Progress Exam 2 in the first half, in the exam seats. Then pictures made of pictures: a picture is a value, handed to a function and handed back.

Progress Exam 2 — Java, Monday, October 26 first, in the exam seats. After the break: pictures made of pictures, each one a value a function takes and gives back.

A8 is due Tuesday, October 27.

Outline

  1. Progress Exam 2 — the first half
  2. The graphics project — before the break
  3. Pictures from pictures — pairs, at the keyboard
  4. Does row3 change its picture? — keyboards down
  5. The checkerboard — pairs again
  6. Keep going: targets
  7. Before Wednesday

1. Progress Exam 2

On your own, in LockDown Browser: the quiz Progress Exam 2, and the link in its header. Its page says how it works.

2. The graphics project

Once your exam is in, before the break.

Open cmsc-120-graphics in IDEA. Not on this machine? Clone it, as on Wednesday, and confirm the Load Maven popup; IDEA fetches JavaFX during the break.

3. Pictures from pictures

Pairs protocol
  1. The navigator (not typing) says the examples first. Then they say the steps the function takes.
  2. The driver types the above, and decides how each step becomes Java.
  3. If either of you is stuck, drop the roles for a minute and talk it over.
  4. Swap at every function.

A new file in src/, Pictures.java, with a main that ends in Check.done() and then shows what you made.

Each function gets a // line above it with a purpose statement and at least two Check.is lines in main, on Img.width and Img.height.

nigeria

Img nigeria(int unit) — green, white and green stripes side by side, each unit wide and 2 * unit high. nigeria(20) is 60 by 40.

Done when its checks pass at two units and the flag shows.

row3

Img row3(Img i) — three copies of the picture i side by side. It takes a picture and gives back a picture: row3(nigeria(20)) is 180 by 40.

Done when checks pass on two different pictures, and a row shows.

grid3

If you are done, here is one more.

Img grid3(Img i) — nine copies of i in a square: three rows of three. row3 is already written.

Done when checks pass on two different pictures, and a grid shows.

4. Does row3 change its picture?

Keyboards stop. Predict first.

In week 6, flip turned a StringBuilder around in place: it gave nothing back, and its check looked at the caller’s builder after the call. row3 gives a picture back. What happens to the one it was given?

Img f = nigeria(10);
Img r = row3(f);
IO.println(Img.width(f));
IO.println(Img.width(r));

What do the two lines print?

30 and 90. row3 builds a new picture out of three copies of f and gives it back, and f is as it was. Every Img function that makes a picture works this way: it gives back a new one and changes none. That is why row3 can put the same i in three places, and quad its a in two.

A StringBuilder is the other kind of object: one builder that changes, so flip can give nothing back and the change is still there in the caller’s builder. The book’s shapes are that kind too: Wednesday’s RandomTricolorFlag makes each stripe, then changes its color with setFill.

5. The checkerboard

Pairs, the same way.

A1’s optional part asked for an 8 by 8 checkerboard and said you would not enjoy it with what you had then. With pictures made of pictures, it is four short functions.

quad

Img quad(Img a, Img b) — a and b side by side, above b and a side by side: four pictures in a square, the two as on a diagonal.

Done when a check on its width passes for two squares of 10, and it shows.

checker2

Img checker2(int side) — a black square and a white one, each side across, in a quad: 2 by 2. checker2(10) is 20 by 20.

Done when its checks pass at two sides and the four squares show.

checker4

If you are done, here are two more.

Img checker4(int side) — a quad of two checker2s: 4 by 4. Draw it on paper first: why does it come out right, when both of the pictures handed to quad are the same?

Done when Img.width(checker4(10)) is 40 and the board shows, four squares a side.

checker8

Img checker8(int side) — a quad of two checker4s: 8 by 8.

Done when Img.width(checker8(10)) is 80 and the board shows, eight squares a side.

6. Keep going: targets

target

Img target(int unit) — three circles, one on another: red of radius 3 * unit at the back, white of radius 2 * unit, red of radius unit in front. target(10) is 60 by 60.

Done when its checks pass at two units and the target shows.

targets

Img targets(int unit) — three targets in a row. One line.

Done when two checks pass, and the row shows.

7. Before Wednesday

A8 is due Tuesday, October 27.