Enums and records
Coles 5.4 has switch, chosen by a number. This page has the two kinds of type that switch is most at home with, which Coles’ book does not cover. Both are one line at the top of the file, above the functions.
Enums
A text can be any text. When a value can only be one of a few — a day of the week, a film’s rating, a show time — an enum lists them, and those are all the values its type has:
Day is now a type, like int or String: a function can take one, as in boolean isWeekend(Day d), or give one back. A value is written with the type’s name in front, Day.SAT. Anything else — Day.sat, Day.SATURDAY — Java refuses before the program runs:
With a text, isWeekend("sat") runs, and gives false.
Two values of one enum are compared with ==. There is only ever one Day.SAT, so “are these the same one?” is the right question here — as it is not for two texts, which .equals compares.
switch over an enum
A switch has one case per value, and values that share an answer share a case:
// whether d is Saturday or Sunday
boolean isWeekend(Day d) {
return switch (d) {
case SAT, SUN -> true;
case MON, TUE, WED, THU, FRI -> false;
};
}Written after
return, aswitchis an expression, like? :: it has a value, andreturnhands it out. The;after its closing brace ends thereturn.A case says
SAT, notDay.SAT: theswitchis on aDay, so Java already knows which type every case belongs to and looks the name up there. Everywhere else nothing says so, and the type’s name comes first —d == Day.SAT; a bareSATthere is cannot find symbol.After each
->comes an expression of the type the function gives back. It may use the function’s other parameters: inboolean isOpenAt(Day d, int hour), a case can becase SAT, SUN -> hour >= 10;.List every value, and leave out
default. Then a value with no case is an error before anything runs:Every value has to reach an answer — the rule behind
missing return statement, for aswitch. Adefaultwould answer for any value not listed, so a forgotten value would quietly get thedefault’s answer.Over a number, as in Coles 5.4, no list of cases can name every value, so a
switchthat gives a value needs adefaultfor the rest:case 7, 11 -> …, thendefault -> ….
A function that takes an enum is usually one switch, with a case per value. The data has seven shapes, so the function has seven answers, or fewer when some share one.
Records
A record is a value made of named parts:
That line makes a type, Ride, whose values each hold an int called km and a boolean called night. The {} at the end stays empty in this course.
new Ride(3, true)makes one:new, as for a builder, with the parts in the order the line lists them.r.km()andr.night()read the parts back: each part’s name, then().- A record never changes — nothing sets a part after
new— so, like a text, it can be treated as a value. Two records with the same parts are.equals;==asks whether they are one object, and twonews make two. IO.println(new Ride(3, true))printsRide[km=3, night=true].
A function that takes a record usually starts by reading the parts it needs, and then decides as before. A6’s nightFare, on a ride:
Check.is(rideFare(new Ride(3, true)), 14); checks it.
In one file
The enums and records first, then the functions, then main:
enum Day { MON, TUE, WED, THU, FRI, SAT, SUN }
record Ride(int km, boolean night) {}
// whether d is Saturday or Sunday
boolean isWeekend(Day d) {
return switch (d) {
case SAT, SUN -> true;
case MON, TUE, WED, THU, FRI -> false;
};
}
// the fare: 5 up to 2 km; longer, 5 plus 2 for every km, or 3 at night
int rideFare(Ride r) {
if (r.km() <= 2) {
return 5;
} else if (r.night()) {
return 5 + r.km() * 3;
} else {
return 5 + r.km() * 2;
}
}
void main() {
Check.is(isWeekend(Day.SAT), true);
Check.is(isWeekend(Day.FRI), false);
Check.is(rideFare(new Ride(2, true)), 5);
Check.is(rideFare(new Ride(3, true)), 14);
Check.done();
}