Assignment 9 — Lists in Java

A taxi’s day as a list of rides: filter and map for which rides and where to, fold for the takings and the route, each a line with an arrow in it; then seven by hand, with a switch on the list, four of them carrying a value down the calls.

Due Nov 3, 2026

One file, Taxi.java, in src/ of your cmsc-120-lst project from class on Wednesday, beside Lst.java and Check.java. Part A needs Wednesday’s class; Part B needs fold, which Wednesday’s class also met, and the reading, Lists in Java. Part C needs Monday’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 // 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(). Check.is takes two lists, and compares them element by element. A function whose body this page calls one line is one return with Lst and an arrow in it.

The file starts with a ride and its fare. A ride has a place it goes to, a length and whether it is at night; the fare is the reading’s rule from week 8. 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 — which rides, and where to

  1. longer — the rides longer than n kilometers, in order. One line.

    Lst<Ride> longer(Lst<Ride> rides, int n)

    longer(day, 5) is the Danville and the Millville rides. A check on a list of rides compares rides, and two rides are equal when their parts are.

  2. nights — the rides at night. One line.

    Lst<Ride> nights(Lst<Ride> rides)
  3. places — where each ride went, in order. One line.

    Lst<String> places(Lst<Ride> rides)

    places(day) is [list: Danville, Berwick, Millville, Bloomsburg], and Check.is compares it with Lst.of("Danville", ...).

Part B — the takings, and the route

  1. fares — each ride’s fare, in order, from fare. One line.

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

    fares(day) is [list: 29, 5, 32, 5].

  2. takings — the day’s takings: every fare, added up. One line, a fold.

    int takings(Lst<Ride> rides)

    takings(day) is 71. What is the takings of a day with no rides?

  3. nightTakings — the takings of the night rides only. One line, which calls two of the functions above.

    int nightTakings(Lst<Ride> rides)
  4. route — the places in order, joined by a comma and a space. A fold whose value so far is a text.

    String route(Lst<Ride> rides)

    route(day) is "Danville, Berwick, Millville, Bloomsburg", a single ride’s route is its place alone, and no rides is "". The first place has no comma in front of it: the arrow can ask whether the text so far is empty, s.isEmpty(), with a ? :.

Part C — by hand

Monday’s class, with week 3’s cases as a switch. Each of these is a switch on the list with a case for each kind, case Lst.Empty() -> ... and case Lst.Link(var r, var rest) -> ..., and calls itself on rest, or has a helper that does: no Lst.filter, Lst.map or Lst.fold.

  1. rideCount — how many rides there are. rideCount(day) is 4.

    int rideCount(Lst<Ride> rides)
  2. longest — the most kilometers of any ride, and 0 for no rides. longest(day) is 12. The larger of two numbers is Math.max(a, b). Check it once on a list whose longest ride is not the first.

    int longest(Lst<Ride> rides)
  3. anyNight — whether some ride is at night. anyNight(day) is true; a day with no night rides gives false, and so does no rides at all.

    boolean anyNight(Lst<Ride> rides)
  4. runningTakings — the takings after each ride, in order. runningTakings(day) is [list: 29, 34, 66, 71], and no rides is the empty list. The rest’s answer cannot know what was taken before it, so a helper carries the takings so far, as Monday’s totalFrom carries the sum: takingsFrom(int soFar, Lst<Ride> rides), the running takings of rides when soFar was taken before them. Its Empty case is the empty list; its Link case links the takings after the first ride in front of the running takings of the rest, with that new total carried. Check the helper too, once with a soFar that is not 0.

    Lst<Integer> runningTakings(Lst<Ride> rides)
  5. longestCarried — longest a second way: a helper longestFrom(int best, Lst<Ride> rides) carries the most kilometers seen so far, and the Empty case hands it back. longestCarried(day) is 12, as longest(day) is. Check the helper once with a best the rides never reach, longestFrom(100, day).

    int longestCarried(Lst<Ride> rides)
  6. kmBeforeNight — the kilometers driven before the first night ride: kmBeforeNight(day) is 12, a day with no night rides gives all its kilometers, and no rides gives 0. The helper carries the kilometers so far and stops at the first night ride, handing them back there instead of going on.

    int kmBeforeNight(Lst<Ride> rides)
  7. everyOther — every other ride, the first one included: everyOther(day) is the Danville and the Millville rides. What is carried here is not a number: a boolean, whether the next ride is kept, which flips at every ride. Check the helper once started the other way, which gives the Berwick and the Bloomsburg rides.

    Lst<Ride> everyOther(Lst<Ride> rides)

Done when Check.done() prints Looks shipshape with every function checked, each on the empty list.

Hand in

Upload Taxi.java on Brightspace, under Assignments → A9. 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 14. These cost points too: a file under any name but Taxi.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 check on the empty list for every function, with its answer worked out first.
  • One line where the page says one line: Lst and an arrow, no loop and no if.
  • route’s first place, with no comma in front of it, and its empty route.
  • Part C’s switches: a case for each kind of list and no default, each function, or its helper, calling itself on rest.

Presentation

Two of you will be asked on Wednesday, November 4 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 Taxi.java, each function with its // line and checks.

A flag from a list of bands

On Wednesday a flag came from a list of colors, every stripe the same height. Real flags have bands of different heights: Colombia’s is gold for half its height, then blue and red for a quarter each. In units, that is 2, 1 and 1.

record Band(String color, int height) {}

Lst<Band> colombia = Lst.of(new Band("gold", 2), new Band("blue", 1), new Band("red", 1));
  1. band(Band b, int width, int unit) — one band as a picture, width wide and b.height() * unit high. Colombia’s gold band at width 120 and unit 20 is 120 by 40.
  2. flag(Lst<Band> bands, int width, int unit) — the bands one above another, the first on top: a map from bands to pictures, then a fold with Img.above from a picture of nothing, Img.rectangle(0, 0, "solid", "white"). Colombia at width 120 and unit 20 is 120 by 80: 40, then 20, then 20.
  3. units(Lst<Band> bands) — the flag’s height in units, from the bands alone, with a fold: 4 for Colombia. Then a check that the two routes agree: Img.height(flag(colombia, 120, 20)) is units(colombia) * 20.
  4. scaled(Lst<Band> bands, int k) — the same bands, each k times as high: a map that makes a new Band from each. scaled(colombia, 3) is gold 6, blue 3, red 3, and its flag at unit 10 is 120 high.
  5. Two more flags of your choice with bands of different heights, each a Lst<Band> of its own, shown with Img.show.