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
sumOfSquares—1*1 + 2*2 + ... + n*n:sumOfSquares(3)is14, andsumOfSquares(0)is0.countdown—n, n - 1, ..., 1as a list:countdown(4)is[list: 4, 3, 2, 1], andcountdown(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.evensUpTo— the even numbers from2up ton, in order:evensUpTo(7)is[list: 2, 4, 6],evensUpTo(8)ends in8, andevensUpTo(1)is the empty list. The loop’s body has a question in it, and the list has to come out in order.
Part B — a loop inside a loop
countPairs— how many pairs of numbersi,jthere are with1 <= i < j <= n:countPairs(4)is6, the pairs being 1-2, 1-3, 1-4, 2-3, 2-4 and 3-4;countPairs(1)is0. The inner loop starts where the outer one stands, plus one.diagonal—nbynsquares of sideunit, solid on the diagonal and outline everywhere else: the square in rowrand columncis solid whenr == c.diagonal(3, 10)is30by30.countDivisors— how many of1, 2, ..., ndividenwith no remainder:countDivisors(12)is6,countDivisors(7)is2, andcountDivisors(1)is1.countPrimesUpTo— how many of2, 3, ..., nare prime, a prime being a number with exactly two divisors:countPrimesUpTo(10)is4,countPrimesUpTo(2)is1, andcountPrimesUpTo(1)is0. A loop with a loop inside it by way of a call:countDivisorsdoes the inner one.
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.
squaresOf— each element squared, in order:squaresOf(Lst.of(1, 2, 3))is[list: 1, 4, 9].countIf— how many elementskeepsays yes to:countIf(u -> u >= 10, Lst.of(4, 10, 20))is2, and the empty list gives0.anyMatch— whetherkeepsays yes to some element:anyMatch(u -> u >= 10, Lst.of(4, 10, 20))istrue, withu -> u > 100it isfalse, and the empty list givesfalse. It stops at the first yes: areturninside the loop, and the answer for none* after it.*
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
forin every function, with no call to the function itself and noLst.filter,Lst.maporLst.fold. - A check at the edge for every function,
0or the empty list, with its answer worked out first. countPairsandcountPrimesUpToas a loop inside a loop, the second by way of a call.- The lists in order:
evensUpToandsquaresOfcome 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.
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 afor; 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 last1is there before the walk, and put the first1in front after it. Built at the front and read in order: say why it comes out right anyway.pascalRow(int n)— rown:pascalRow(0)is[list: 1], andpascalRow(4)is[list: 1, 4, 6, 4, 1]. Start from row 0 and callnextRowntimes.pascalPicture(int n, int unit)— rows 0 ton, one above the other and left-aligned, each number a square of sideunit, solid when the number is odd and outline when it is even.pascalPicture(4, 10)is50by50. Two loops: aforover the rows, and inside it aforover the row’s list.- 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.