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:

Lst<Ride> day = Lst.of(new Ride("Danville", 12, false), new Ride("Berwick", 2, true),
                       new Ride("Millville", 9, true), new Ride("Bloomsburg", 1, false));

Part A — walking a list

  1. kmTotal — all the kilometers added up. kmTotal(day) is 24.

    int kmTotal(Lst<Ride> rides)
  2. countNights — how many rides are at night. countNights(day) is 2. The loop’s body has a question in it.

    int countNights(Lst<Ride> rides)
  3. fromDigits — the number a list of digits makes: fromDigits(Lst.of(1, 2, 3)) is 123, and no digits is 0. Each digit is added to ten times the number so far.

    int fromDigits(Lst<Integer> digits)
  4. kmBeforeNight — the kilometers driven before the first night ride: kmBeforeNight(day) is 12; a day with no night rides gives all its kilometers; a day that starts with a night ride gives 0. 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.

    int kmBeforeNight(Lst<Ride> rides)
  5. 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.

    Lst<String> placesBackwards(Lst<Ride> rides)
  6. 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.

    Lst<String> places(Lst<Ride> rides)
  7. 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.

    Lst<Integer> runningTakings(Lst<Ride> rides)

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.

  1. sumBetween — a + (a + 1) + ... + b: sumBetween(3, 6) is 18, sumBetween(5, 5) is 5, and sumBetween(6, 3) is 0.

    int sumBetween(int a, int b)
  2. digitSum — the digits of n added up, for n from 0 up: digitSum(2026) is 10, and digitSum(0) is 0. n % 10 is the last digit and n / 10 the rest.

    int digitSum(int n)
  3. halvings — how many times n is halved, in whole numbers, before it reaches 0: halvings(100) is 7 (100, 50, 25, 12, 6, 3, 1, 0), halvings(1) is 1, and halvings(0) is 0.

    int halvings(int n)

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 while in every function, with no call to the function itself and no Lst.filter, Lst.map or Lst.fold.
  • A check on the empty list for every list function, with its answer worked out first.
  • kmBeforeNight stopping at the first night ride, checked on a day that starts with one.
  • places and runningTakings in 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.

  1. next(int n) — the number after n in the run: next(6) is 3, next(3) is 10, next(1) is 4. No loop yet; one ? : with n % 2.
  2. collatzSteps(int n) — how many steps the run takes to reach 1: collatzSteps(6) is 8, collatzSteps(1) is 0, and collatzSteps(27) is 111. Two names change: the current number, which starts at n and moves by next, and the count of steps so far. The loop runs while the current number is not 1.
  3. collatzPeak(int n) — the largest value the run reaches: collatzPeak(6) is 16, collatzPeak(1) is 1, and collatzPeak(27) is 9232. The same loop, carrying the largest seen so far instead of a count; Math.max updates it.
  4. longestUnder(int limit) — the starting number below limit whose run takes the most steps: longestUnder(10) is 9 (19 steps), and longestUnder(100) is 97 (118 steps). A loop over the starting numbers from 1 up to limit - 1, calling collatzSteps for each: a loop with a loop inside it, by way of a call. Carry the best start so far and its steps.
  5. One line in a comment at the end of the file: longestUnder(1000), and how many steps its run takes.