Class 23 — Monday, November 16

Progress Exam 3 in the first half, in the exam seats. Then Classroom 50: your own copy of a project, loops inside loops written on your own, numbers and pictures, and a checker that runs every time you push.

Progress Exam 3 — Lists and loops, Monday, November 16 first, in the exam seats. After the break: a project of your own from Classroom 50, functions each a loop inside a loop, numbers first and then pictures, written on your own, and a check that runs on GitHub every time you push.

A11 is due Tuesday, November 17.

Outline

  1. Progress Exam 3 — the first half
  2. Your own copy, from Classroom 50 — before the break
  3. The first function, and the mark beside your commit — on your own
  4. The inner bound is the outer counter — predict, then run
  5. The triangle, counted — on your own
  6. Pictures, checked the same way — on your own
  7. Before Wednesday

1. Progress Exam 3

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

2. Your own copy, from Classroom 50

Once your exam is in, before the break. On your own: everyone makes their own copy today, since the assignments from Wednesday on come this way.

  1. The invitation. Your Commonwealth email has an invitation to the GitHub organization cu-cs-labs, sent Sunday. Open it and accept. No GitHub account yet? The invitation walks you through making one; use the same email.
  2. The assignment link, in a Brightspace announcement. Sign in with GitHub, click Accept assignment, and wait for the checklist to finish: you now have a repository of your own named cmsc-120-c23-<your username>. Click Open repository.
  3. Clone it in IDEA: the green Code button on your repository, copy the address; in IDEA, the menu button (☰) → File → New → Project from Version Control… → paste → Clone. Say yes to loading the Maven project.

Done when src/Boards.java is open in IDEA with its empty main. The repository’s README says the same six functions as sections 3, 5 and 6 below.

3. The first function, and the mark beside your commit

On your own, in src/Boards.java. Every function today is a for inside a for, with a // line above it and at least two Check.is lines in main that you write first, the answers worked out by hand, one of them at 0: the design recipe, as every week. The checker on GitHub runs checks of its own after you push and shows a mark; that is a second opinion on a function your own checks already pass, never a replacement for them, and the exam has no checker.

countDivisorPairs

int countDivisorPairs(int n) — how many pairs of whole numbers i and j, with i no bigger than j, multiply to n: countDivisorPairs(12) is 3, for 1 and 12, 2 and 6, 3 and 4; countDivisorPairs(7) is 1; countDivisorPairs(1) is 1. The inner loop starts where the outer one stands.

Done when your own checks pass, one of them at 1. Then push.

Push, and read the mark

Git → Commit…, tick Boards.java, a line saying what you did, Commit and Push. Then open your repository on GitHub: within a minute or two a mark appears beside the latest commit, a green check or a red cross. Click it, then Details: six lines, one per function, with what each one’s checks said. Five are red now, since five functions are not there yet. That is the checker, and it runs the same way on every push: its checks are not yours, and it cannot tell you what to write, only whether what you wrote agrees with the page.

timesTable

If you are done before the room moves on, here is one more.

Lst<Lst<Integer>> timesTable(int n) — the times table up to n as a list of rows, row r holding r * 1 up to r * n: timesTable(3) is [list: [list: 1, 2, 3], [list: 2, 4, 6], [list: 3, 6, 9]]. A list built at the front comes out backwards, so count both loops down, from n to 1, and each list comes out in order.

Done when your checks pass on 3, on 1 and on 0, which is the empty list; then push.

4. The inner bound is the outer counter

Write your prediction in the Journal before anyone runs anything.

int count = 0;
for (int r = 1; r <= 4; r++) {
    for (int c = 1; c <= r; c++) {
        count++;
    }
}
IO.println(count);

What does it print, and how many times did the inner body run for each r?

10: once for r = 1, twice for r = 2, three times for r = 3, four for r = 4, and 1 + 2 + 3 + 4 is 10. Last Wednesday’s loop, r to 3 and c to 4, ran its body 12 times, which was 3 * 4 because the inner bound was the same on every round; here the inner bound is r, so it grows with every round, and the rounds are added up, not multiplied.

A picture of each, one dot for every time the inner body runs, one row for every round of the outer loop. Last Wednesday’s, c to 4 on every row:

r = 1   • • • •
r = 2   • • • •
r = 3   • • • •
                 3 rows of 4: 3 * 4 = 12

Today’s, c to r, so each row is one dot longer than the one above it:

r = 1   •
r = 2   • •
r = 3   • • •
r = 4   • • • •
                 1 + 2 + 3 + 4 = 10

The second is half of a 4 by 4 square with its diagonal kept: 16 dots in the square, 4 on the diagonal, and (16 + 4) / 2 is 10.

5. The triangle, counted

On your own, in src/Boards.java.

triangleCount

int triangleCount(int n) — section 4’s count, for any n: how many times the inner body runs when the inner bound is the outer counter, as the loop that counts it, not as a formula. triangleCount(4) is 10.

Done when your checks pass on 4, on 1 and on 0; then push, and the mark’s line for it turns green.

6. Pictures, checked the same way

On your own, when the lines above are green. A check on a picture is on its Img.width and Img.height, never on the picture; Img.rectangle(0, 0, "solid", "white") is the picture of nothing a loop starts from, and Img.square(unit, "outline", "black") one square, as Wednesday’s board.

border

Img border(int n, int unit) — n by n squares of side unit, solid along the edge of the board and outline inside. A square is on the edge when it is in the first or the last row, or in the first or the last column. Four questions about r and c, joined by ||; a ? : on the answer picks the mode.

Done when your checks say border(4, 10) is 40 by 40 and border(0, 10) is 0 wide, and it looks like a frame when you show it; then push.

cross

Img cross(int n, int unit) — n by n squares, solid on both diagonals and outline elsewhere: solid when r == c or r + c == n - 1.

Done when your checks say cross(5, 10) is 50 by 50, and it shows an X; then push.

lowerTriangle

Img lowerTriangle(int n, int unit) — section 4’s picture: n by n squares, solid where the column is at most the row, outline above.

Done when your checks say lowerTriangle(4, 10) is 40 by 40 and lowerTriangle(0, 10) is 0 wide; then push, and all six lines on GitHub are green.

7. Before Wednesday

A11 is due Tuesday, November 17, on Brightspace as before. From Wednesday’s assignment on, hand-ins go the way today’s did: a repository of your own from Classroom 50, and a push. Wednesday starts arrays, Java’s own fixed-size list; the reading for it, [CU] 7.1 — Linear Arrays, is due before the Monday after.