Assignment 11 — for loops

Counting loops, a loop inside a loop, and map, filter and fold written by hand; then Pascal’s triangle, and the picture hiding in it.

Due Nov 17, 2026

One file, Tables.java, in src/ of your cmsc-120-lst project, beside Fors.java. Everything here needs Wednesday’s class.

AI: the course AI policy applies in full. No AI-generated code, and no asking a chatbot to solve these problems before you have.

Every function

A for loop, and no call to the function itself: no Lst.filter, Lst.map or Lst.fold either. A // line above it with a purpose statement, and at least two Check.is lines in main, worked out before the body is written, one of them at the edge: 0, or the empty list. 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.

Part A — counting

  1. sumOfSquares — 1*1 + 2*2 + ... + n*n: sumOfSquares(3) is 14, and sumOfSquares(0) is 0.

    int sumOfSquares(int n)
  2. countdown — n, n - 1, ..., 1 as a list: countdown(4) is [list: 4, 3, 2, 1], and countdown(0) is the empty list. A list built at the front comes out the other way round from the order it was built in; here that is the order you want.

    Lst<Integer> countdown(int n)
  3. evensUpTo — the even numbers from 2 up to n, in order: evensUpTo(7) is [list: 2, 4, 6], evensUpTo(8) ends in 8, and evensUpTo(1) is the empty list. The loop’s body has a question in it, and the list has to come out in order.

    Lst<Integer> evensUpTo(int n)

Part B — a loop inside a loop

  1. countPairs — how many pairs of numbers i, j there are with 1 <= i < j <= n: countPairs(4) is 6, the pairs being 1-2, 1-3, 1-4, 2-3, 2-4 and 3-4; countPairs(1) is 0. The inner loop starts where the outer one stands, plus one.

    int countPairs(int n)
  2. diagonal — n by n squares of side unit, solid on the diagonal and outline everywhere else: the square in row r and column c is solid when r == c. diagonal(3, 10) is 30 by 30.

    Img diagonal(int n, int unit)
  3. countDivisors — how many of 1, 2, ..., n divide n with no remainder: countDivisors(12) is 6, countDivisors(7) is 2, and countDivisors(1) is 1.

    int countDivisors(int n)
  4. countPrimesUpTo — how many of 2, 3, ..., n are prime, a prime being a number with exactly two divisors: countPrimesUpTo(10) is 4, countPrimesUpTo(2) is 1, and countPrimesUpTo(1) is 0. A loop with a loop inside it by way of a call: countDivisors does the inner one.

    int countPrimesUpTo(int n)

Part C — by hand

Wednesday’s section 7, on your own list functions: a for over the list, for (Lst<Integer> todo = xs; todo instanceof Lst.Link(var u, var rest); todo = rest), and an arrow handed in as a parameter.

  1. squaresOf — each element squared, in order: squaresOf(Lst.of(1, 2, 3)) is [list: 1, 4, 9].

    Lst<Integer> squaresOf(Lst<Integer> xs)
  2. countIf — how many elements keep says yes to: countIf(u -> u >= 10, Lst.of(4, 10, 20)) is 2, and the empty list gives 0.

    int countIf(Predicate<Integer> keep, Lst<Integer> xs)
  3. anyMatch — whether keep says yes to some element: anyMatch(u -> u >= 10, Lst.of(4, 10, 20)) is true, with u -> u > 100 it is false, and the empty list gives false. It stops at the first yes: a return inside the loop, and the answer for none* after it.*

    boolean anyMatch(Predicate<Integer> keep, Lst<Integer> xs)

Done when Check.done() prints Looks shipshape with every function checked, each at its edge.

Hand in

Upload Tables.java on Brightspace, under Assignments → A11. The file itself, not a paste, and not Lst.java, Check.java or Img.java.

Where it is: in IDEA, IdeaProjects inside your user folder, then cmsc-120-lst/src/. If you pushed it, it is on GitHub too, in your own cmsc-120-lst; otherwise it is on the machine you sat at, and no other.

Grading

Full credit for a file that runs and does 1 through 10. These cost points too: a file under any name but Tables.java, a file that does not run as you hand it in, and a Check.is line that still fails. Run it once more after your last edit.

The layout. One statement to a line, and a blank line between functions; IDEA’s Code → Reformat Code lays a file out that way in one keystroke. A file crammed onto a few long lines cannot be read, so it cannot be marked: it gets 0.

Most of the weight is on four things:

  • A for in every function, with no call to the function itself and no Lst.filter, Lst.map or Lst.fold.
  • A check at the edge for every function, 0 or the empty list, with its answer worked out first.
  • countPairs and countPrimesUpTo as a loop inside a loop, the second by way of a call.
  • The lists in order: evensUpTo and squaresOf come out as the page says, not backwards.

Presentation

Two of you will be asked on Wednesday, November 18 to put your file on the projector and walk the class through it. You will not be told in advance.

What else to do (optional part)

Ungraded. No points, and skipping it costs nothing. One problem bigger than anything above, in Tables.java, each function with its // line and checks.

Pascal’s triangle, and the picture in it

Each row of Pascal’s triangle starts and ends with 1, and every other number is the sum of the two above it. Row 0 is 1; row 1 is 1 1; row 4 is 1 4 6 4 1.

  1. nextRow(Lst<Integer> row) — the row after the one given: nextRow(Lst.of(1, 3, 3, 1)) is [list: 1, 4, 6, 4, 1]. Walk the row with a for; at each element, if there is a next one, link their sum in front of the list so far. Start the list so far as [list: 1], so the row’s last 1 is there before the walk, and put the first 1 in front after it. Built at the front and read in order: say why it comes out right anyway.
  2. pascalRow(int n) — row n: pascalRow(0) is [list: 1], and pascalRow(4) is [list: 1, 4, 6, 4, 1]. Start from row 0 and call nextRow n times.
  3. pascalPicture(int n, int unit) — rows 0 to n, one above the other and left-aligned, each number a square of side unit, solid when the number is odd and outline when it is even. pascalPicture(4, 10) is 50 by 50. Two loops: a for over the rows, and inside it a for over the row’s list.
  4. One line in a comment at the end of the file: what pascalPicture(31, 4) looks like when shown. It is a shape from the reading’s section 6.2, drawn there with randomness and here without any.