Assignment 10 — Loops
A taxi’s day again, walked with while loops: sums and counts with a name that changes, a loop that stops early, lists that come out backwards; then loops with no list behind them.
Due Nov 10, 2026
One file, Loops.java, in src/ of your cmsc-120-lst project, beside Lst.java and Check.java. Everything here needs Wednesday’s class; nothing needs Monday’s.
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 while 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 on the empty list, Lst.of(). A loop over a list has two names that change: the part still to walk, todo, and the value so far.
The file starts with a ride and its fare, as A9’s did. Copy both in as they are:
record Ride(String to, int km, boolean night) {}
// given: the fare, the reading's rule: 5 up to 2 km; longer, 5 plus 2 for every km, or 3 at night
int fare(Ride r) {
if (r.km() <= 2) {
return 5;
} else if (r.night()) {
return 5 + r.km() * 3;
} else {
return 5 + r.km() * 2;
}
}The examples below use one day:
Part A — walking a list
kmTotal— all the kilometers added up.kmTotal(day)is24.countNights— how many rides are at night.countNights(day)is2. The loop’s body has a question in it.fromDigits— the number a list of digits makes:fromDigits(Lst.of(1, 2, 3))is123, and no digits is0. Each digit is added to ten times the number so far.kmBeforeNight— the kilometers driven before the first night ride:kmBeforeNight(day)is12; a day with no night rides gives all its kilometers; a day that starts with a night ride gives0. The loop stops at the first night ride. Its condition can ask two things at once, joined by&&: that there is a ride to look at, and that it is not at night.placesBackwards— where each ride went, last ride first:placesBackwards(day)is[list: Bloomsburg, Millville, Berwick, Danville]. A loop that builds a list puts each new element in front of the list made so far, so it comes out backwards on its own.places— where each ride went, in order, as a loop.places(day)is[list: Danville, Berwick, Millville, Bloomsburg]. What comes out backwards twice comes out in order: a second loop over the first one’s answer.runningTakings— the takings after each ride, in order:runningTakings(day)is[list: 29, 34, 66, 71], and no rides is the empty list. Two names change in the loop, the takings so far and the list so far, and the list wants turning round at the end, as in 6.
Part B — no list
A loop with no list behind it has a number that changes in place of todo, and its condition looks at that number.
sumBetween—a + (a + 1) + ... + b:sumBetween(3, 6)is18,sumBetween(5, 5)is5, andsumBetween(6, 3)is0.digitSum— the digits ofnadded up, fornfrom0up:digitSum(2026)is10, anddigitSum(0)is0.n % 10is the last digit andn / 10the rest.halvings— how many timesnis halved, in whole numbers, before it reaches0:halvings(100)is7(100, 50, 25, 12, 6, 3, 1, 0),halvings(1)is1, andhalvings(0)is0.
Done when Check.done() prints Looks shipshape with every function checked, each list function on the empty list.
Hand in
Upload Loops.java on Brightspace, under Assignments → A10. The file itself, not a paste, and not Lst.java or Check.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 Loops.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
whilein every function, with no call to the function itself and noLst.filter,Lst.maporLst.fold. - A check on the empty list for every list function, with its answer worked out first.
kmBeforeNightstopping at the first night ride, checked on a day that starts with one.placesandrunningTakingsin order, not backwards.
Presentation
Two of you will be asked on Wednesday, November 11 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 Loops.java, each function with its // line and checks.
The Collatz run
Start from a whole number above 0. If it is even, halve it; if it is odd, triple it and add 1. Repeat. Every number anyone has tried reaches 1, and nobody has proved that every number does. From 6 the run is 6, 3, 10, 5, 16, 8, 4, 2, 1: eight steps, and the largest value on the way is 16.
next(int n)— the number afternin the run:next(6)is3,next(3)is10,next(1)is4. No loop yet; one? :withn % 2.collatzSteps(int n)— how many steps the run takes to reach1:collatzSteps(6)is8,collatzSteps(1)is0, andcollatzSteps(27)is111. Two names change: the current number, which starts atnand moves bynext, and the count of steps so far. The loop runs while the current number is not1.collatzPeak(int n)— the largest value the run reaches:collatzPeak(6)is16,collatzPeak(1)is1, andcollatzPeak(27)is9232. The same loop, carrying the largest seen so far instead of a count;Math.maxupdates it.longestUnder(int limit)— the starting number belowlimitwhose run takes the most steps:longestUnder(10)is9(19 steps), andlongestUnder(100)is97(118 steps). A loop over the starting numbers from1up tolimit - 1, callingcollatzStepsfor each: a loop with a loop inside it, by way of a call. Carry the best start so far and its steps.- One line in a comment at the end of the file:
longestUnder(1000), and how many steps its run takes.