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:
Part A — Wednesday
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.ticket(2)is0. Checks on both sides of every edge: 3 and 4, 11 and 12, 64 and 65.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.allowed(Rating.R, 16, true)istrue;allowed(Rating.NC17, 17, true)isfalse. Aswitchonr. A case does not have to betrueorfalseoutright: it can be any question with abooleananswer, and it may ask aboutageandwithAdult. Checks at 16 and 17 for R, and at 17 and 18 for NC-17.discount— how much less a ticket costs at that show: 3 at a matinee, 2 at a late show, nothing in the evening.discount(Show.MATINEE)is3.price— the price of a ticket for that age at that show:ticket’s price less the show’sdiscount, but never below 0.price(30, Show.MATINEE)is9, andprice(2, Show.MATINEE)is0, not-3. Give the price before that last rule a name inside the function.isPeak— whether a show is at the cinema’s busiest time: an evening show on a Friday or a Saturday.isPeak(Day.FRI, Show.EVENING)istrue, andisPeak(Day.FRI, Show.MATINEE)isfalse. Two values of one enum are compared with==: there is only oneDay.FRI, so “is this the same one?” is the right question. Mind the grouping, as in A6’spasses.
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:
new Visit(16, Rating.R, true) makes a visit, and v.age() reads its age back.
admitted— whether the visit gets in.admitted(new Visit(16, Rating.R, true))istrue. One line:allowed, on the visit’s three parts.cost— what a ticket costs.cost(new Ticket(8, Show.MATINEE))is3.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".label(new Ticket(8, Show.MATINEE))is"child, matinee". Two names inside the function, one for each half — the show’s from aswitch— and then thereturnjoins 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 thepricethat comes out0. - A
switchwith a case for every value, and nodefault, inallowed,discountandlabel. - 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.
beats—boolean beats(Move a, Move b), withenum Move { ROCK, PAPER, SCISSORS }beside the other enums: whetherabeatsb. Rock crushes scissors, scissors cut paper, paper covers rock. Aswitchona, and in each case one question aboutb; the first iscase ROCK -> b == Move.SCISSORS;. The checks:beats(Move.ROCK, Move.SCISSORS)istrue,beats(Move.SCISSORS, Move.ROCK)isfalse, andbeats(Move.ROCK, Move.ROCK)isfalse— a move does not beat itself, which is this function’s edge.result—String result(Move mine, Move theirs):"win","lose"or"draw". A chain of three that asksbeatstwice, 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.points— withrecord 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; andint theirPoints(Round r), the same for them. Each is one line:beatson the round’s two parts, the right way round, inside? :.match—String match(Round r1, Round r2, Round r3), the best of three:"you","them"or"tie". Two names first,mineandtheirs, each one side’s points over the three rounds; then a chain of three, as inresult. Worked by hand:round mine theirs pointstheirPoints1 ROCK SCISSORS 1 0 2 PAPER SCISSORS 0 1 3 ROCK ROCK 0 0 mineis 1theirsis 1So that match is a
"tie", and it is your first check: a draw gives nobody a point.Lizard and Spock. Add
LIZARDandSPOCKtoMove, and run. Java stops atbeats: itsswitchno 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 aboutSCISSORS. Java found the cases that were missing; a case that is there and wrong takes a check. WriteCheck.is(beats(Move.ROCK, Move.LIZARD), true)before you fixROCK’s case, and watch it fail first. Then: which ofresult,pointsandmatchhad to change? Say so in a//line abovematch.
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
Give Wednesday’s
isWeekend(Day d)a last casedefault -> false;, then takeSUNout of its case. Does Java still object? What isisWeekend(Day.SUN)now? That answer is why an enum’sswitchgoes without adefault.IO.println(new Ticket(8, Show.MATINEE));— what does a record print? AndIO.println(Show.LATE);?new Ticket(8, Show.MATINEE) == new Ticket(8, Show.MATINEE)—trueorfalse? Then the same two with.equals. Twonews make two records;.equalscompares their parts. Class 10 had the drawing.