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:
Part A — which rides, and where to
longer— the rides longer thannkilometers, in order. One line.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.nights— the rides at night. One line.places— where each ride went, in order. One line.places(day)is[list: Danville, Berwick, Millville, Bloomsburg], andCheck.iscompares it withLst.of("Danville", ...).
Part B — the takings, and the route
fares— each ride’s fare, in order, fromfare. One line.fares(day)is[list: 29, 5, 32, 5].takings— the day’s takings: every fare, added up. One line, afold.takings(day)is71. What is the takings of a day with no rides?nightTakings— the takings of the night rides only. One line, which calls two of the functions above.route— the places in order, joined by a comma and a space. Afoldwhose value so far is a text.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.
rideCount— how many rides there are.rideCount(day)is4.longest— the most kilometers of any ride, and0for no rides.longest(day)is12. The larger of two numbers isMath.max(a, b). Check it once on a list whose longest ride is not the first.anyNight— whether some ride is at night.anyNight(day)istrue; a day with no night rides givesfalse, and so does no rides at all.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’stotalFromcarries the sum:takingsFrom(int soFar, Lst<Ride> rides), the running takings ofrideswhensoFarwas taken before them. ItsEmptycase is the empty list; itsLinkcase 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 asoFarthat is not0.longestCarried—longesta second way: a helperlongestFrom(int best, Lst<Ride> rides)carries the most kilometers seen so far, and theEmptycase hands it back.longestCarried(day)is12, aslongest(day)is. Check the helper once with abestthe rides never reach,longestFrom(100, day).kmBeforeNight— the kilometers driven before the first night ride:kmBeforeNight(day)is12, a day with no night rides gives all its kilometers, and no rides gives0. The helper carries the kilometers so far and stops at the first night ride, handing them back there instead of going on.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: aboolean, 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.
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:
Lstand an arrow, no loop and noif. 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 nodefault, each function, or its helper, calling itself onrest.
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));band(Band b, int width, int unit)— one band as a picture,widthwide andb.height() * unithigh. Colombia’s gold band at width 120 and unit 20 is 120 by 40.flag(Lst<Band> bands, int width, int unit)— the bands one above another, the first on top: amapfrom bands to pictures, then afoldwithImg.abovefrom 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.units(Lst<Band> bands)— the flag’s height in units, from the bands alone, with afold: 4 for Colombia. Then a check that the two routes agree:Img.height(flag(colombia, 120, 20))isunits(colombia) * 20.scaled(Lst<Band> bands, int k)— the same bands, eachktimes as high: amapthat makes anew Bandfrom each.scaled(colombia, 3)is gold 6, blue 3, red 3, and its flag at unit 10 is 120 high.- Two more flags of your choice with bands of different heights, each a
Lst<Band>of its own, shown withImg.show.