Assignment 7 — Enums and records

The cinema’s rules: a chain of ifs in the right order, checked at its edges; enums and switch, with an answer for every value; and records, a value made of named parts.

Due Oct 20, 2026

One file, Cinema.java, next to Check.java — the file from class on Wednesday, if you started it there. Part A needs Wednesday’s class. Part B needs Monday’s, and the records half of this week’s reading.

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. In a chain, the examples that matter sit at its edges, one on each side. A switch over an enum has a case for every value and no default, so that Java can tell you when one is missing. Check.done() stays the last line of main.

The enums go at the top of the file, one line each, above the functions:

enum Day { MON, TUE, WED, THU, FRI, SAT, SUN }
enum Rating { G, PG, PG13, R, NC17 }
enum Show { MATINEE, EVENING, LATE }

Part A — Wednesday

  1. ticket — the price of a ticket for someone of the given age: under 4, free; under 12, 6; 65 and over, 8; everyone else, 12.

    int ticket(int age)

    ticket(2) is 0. Checks on both sides of every edge: 3 and 4, 11 and 12, 64 and 65.

  2. allowed — whether someone may see a film with that rating. G, PG and PG-13 admit anyone. R admits 17 and over, and anyone younger who comes with an adult. NC-17 admits 18 and over, with an adult or without.

    boolean allowed(Rating r, int age, boolean withAdult)

    allowed(Rating.R, 16, true) is true; allowed(Rating.NC17, 17, true) is false. A switch on r. A case does not have to be true or false outright: it can be any question with a boolean answer, and it may ask about age and withAdult. Checks at 16 and 17 for R, and at 17 and 18 for NC-17.

  3. discount — how much less a ticket costs at that show: 3 at a matinee, 2 at a late show, nothing in the evening.

    int discount(Show s)

    discount(Show.MATINEE) is 3.

  4. price — the price of a ticket for that age at that show: ticket’s price less the show’s discount, but never below 0.

    int price(int age, Show s)

    price(30, Show.MATINEE) is 9, and price(2, Show.MATINEE) is 0, not -3. Give the price before that last rule a name inside the function.

  5. isPeak — whether a show is at the cinema’s busiest time: an evening show on a Friday or a Saturday.

    boolean isPeak(Day d, Show s)

    isPeak(Day.FRI, Show.EVENING) is true, and isPeak(Day.FRI, Show.MATINEE) is false. Two values of one enum are compared with ==: there is only one Day.FRI, so “is this the same one?” is the right question. Mind the grouping, as in A6’s passes.

Part B — Monday

Records are Monday’s class, and the second half of the reading’s page on enums and records. Two more lines at the top of the file, under the enums:

record Visit(int age, Rating rating, boolean withAdult) {}
record Ticket(int age, Show show) {}

new Visit(16, Rating.R, true) makes a visit, and v.age() reads its age back.

  1. admitted — whether the visit gets in.

    boolean admitted(Visit v)

    admitted(new Visit(16, Rating.R, true)) is true. One line: allowed, on the visit’s three parts.

  2. cost — what a ticket costs.

    int cost(Ticket t)

    cost(new Ticket(8, Show.MATINEE)) is 3.

  3. label — who the ticket is for and when, as one text: "child" under 12, "senior" from 65, "adult" in between; then the show, as "matinee", "evening" or "late".

    String label(Ticket t)

    label(new Ticket(8, Show.MATINEE)) is "child, matinee". Two names inside the function, one for each half — the show’s from a switch — and then the return joins them. Checks at 11 and 12, at least.

Done when Check.done() prints Looks shipshape with every function checked, a check on each side of every edge in ticket, allowed and label, and a check where price comes out 0.

Hand in

Upload Cinema.java on Brightspace, under Assignments → A7. The file itself, not a paste, and not Check.java.

Where it is: in IDEA, IdeaProjects inside your user folder, then cmsc-120-stripe/src/. In the lab that is the machine you sat at, not any other. On OneCompiler, copy the file’s text into a file called Cinema.java on your own computer, and upload that.

Grading

Full credit for a file that runs and does 1 through 8. These cost points too: a file under any name but Cinema.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.

Most of the weight is on three things:

  • The checks at the edges: every edge of ticket, the ages where R and NC-17 change their answer, and the price that comes out 0.
  • A switch with a case for every value, and no default, in allowed, discount and label.
  • The grouping in isPeak: the parentheses that make it say the rule.

Presentation

Two of you will be asked on Wednesday, October 21 to put your file on the projector and walk the class through it. You will not be told in advance.

One of the two will be asked why allowed has no default.

What else to do (optional part)

Ungraded. No points, and skipping it costs nothing. One problem bigger than anything above, then A6’s daysIn once more, and three things to try — in Cinema.java, each function with its // line and checks.

Rock, paper, scissors — and then lizard and Spock

Five steps that build on each other, in this order; each one is checked before the next is started, so a mistake can only be in the newest one.

  1. beats — boolean beats(Move a, Move b), with enum Move { ROCK, PAPER, SCISSORS } beside the other enums: whether a beats b. Rock crushes scissors, scissors cut paper, paper covers rock. A switch on a, and in each case one question about b; the first is case ROCK -> b == Move.SCISSORS;. The checks: beats(Move.ROCK, Move.SCISSORS) is true, beats(Move.SCISSORS, Move.ROCK) is false, and beats(Move.ROCK, Move.ROCK) is false — a move does not beat itself, which is this function’s edge.

  2. result — String result(Move mine, Move theirs): "win", "lose" or "draw". A chain of three that asks beats twice, once each way round. The draw is what is left when neither move beats the other, so it goes last. One check for each answer.

  3. points — with record Round(Move mine, Move theirs) {} beside the other records: int points(Round r), 1 when I won the round and 0 when I did not; and int theirPoints(Round r), the same for them. Each is one line: beats on the round’s two parts, the right way round, inside ? :.

  4. match — String match(Round r1, Round r2, Round r3), the best of three: "you", "them" or "tie". Two names first, mine and theirs, each one side’s points over the three rounds; then a chain of three, as in result. Worked by hand:

    round mine theirs points theirPoints
    1 ROCK SCISSORS 1 0
    2 PAPER SCISSORS 0 1
    3 ROCK ROCK 0 0
    mine is 1 theirs is 1

    So that match is a "tie", and it is your first check: a draw gives nobody a point.

  5. Lizard and Spock. Add LIZARD and SPOCK to Move, and run. Java stops at beats: its switch no longer has a case for every move. Each move now beats two:

    move beats
    ROCK SCISSORS, LIZARD
    PAPER ROCK, SPOCK
    SCISSORS PAPER, LIZARD
    LIZARD SPOCK, PAPER
    SPOCK SCISSORS, ROCK

    Add the two missing cases and run again. It compiles, and three of the old cases are now wrong: ROCK’s still asks only about SCISSORS. Java found the cases that were missing; a case that is there and wrong takes a check. Write Check.is(beats(Move.ROCK, Move.LIZARD), true) before you fix ROCK’s case, and watch it fail first. Then: which of result, points and match had to change? Say so in a // line above match.

Done when the last check is a match of three rounds with a lizard and Spock in it, worked out by hand before you run it.

A6’s daysIn, as a switch

A6’s optional part had daysIn(int month, int year) as three branches. Write it as a switch on the month, with A6’s isLeap beside it: the four months of 30 share one case, case 4, 6, 9, 11 ->, and February’s case asks isLeap. A month is a number, and no list of cases names every number, so this switch needs a default — the one kind that does. The same checks as before: daysIn(4, 2026) is 30, daysIn(2, 2024) is 29, daysIn(2, 2026) is 28.

Three things to try

  1. Give Wednesday’s isWeekend(Day d) a last case default -> false;, then take SUN out of its case. Does Java still object? What is isWeekend(Day.SUN) now? That answer is why an enum’s switch goes without a default.

  2. IO.println(new Ticket(8, Show.MATINEE)); — what does a record print? And IO.println(Show.LATE);?

  3. new Ticket(8, Show.MATINEE) == new Ticket(8, Show.MATINEE) — true or false? Then the same two with .equals. Two news make two records; .equals compares their parts. Class 10 had the drawing.