enum replacing class
public class Day { static public final Day MONDAY = new Day("Monday"), ... SUNDAY = new Day("Sunday"); public final String name; private Day(final String name) { this.name = name; } public String toString() { return name; } } |
public enum Day { MONDAY("Monday"), ... SUNDAY("Sunday"); final String name; Day(final String name) { this.name = name; } public String toString() { return name;} } |
public enum Day { ... public static String getItalianDayName(final Day day) { switch (day) { case MONDAY: return "Lunedì"; case TUESDAY: return "Martedì"; ... case SUNDAY: return "Domenica"; } return null; // Actually unreachable, but static compiler code analysis is limited } }
public enum Day {
...
private Day(final String name)
{ ... |
Compile time warning: Modifier 'private' is redundant for |
public enum Day {
...
public Day(final String name)
{ ... |
Compile time error: Modifier 'public' not allowed here |
Prohibits enum external instance creation.
| Code | |
|
|---|---|---|
| Result | State: CLOSED Use handle to open |
|
| Code | |
|---|---|
| Result | class java.lang.Enum |
See Enum for
details.
enum values | Code | for (final Day d: Day.values()) { IO.println(d + ", ordinal value: " + d.ordinal()); } |
|---|---|
| Result | Monday, ordinal value: 0 Tuesday, ordinal value: 1 Wednesday, ordinal value: 2 Thursday, ordinal value: 3 Friday, ordinal value: 4 Saturday, ordinal value: 5 Sunday, ordinal value: 6 |
| Code | |
for (final Day d: Day.values()) { IO.println(d + ", ordinal value: " + d.ordinal()); } |
|---|---|---|
| Result | Sunday, ordinal value: 0 Saturday, ordinal value: 1 Friday, ordinal value: 2 Thursday, ordinal value: 3 Wednesday, ordinal value: 4 Tuesday, ordinal value: 5 Monday, ordinal value: 6 |
|
enum instance by its
ordinal value | Code | final int ordinalOfWednesday = 2; // 0: MONDAY 1:TUESDAY 2:WEDNESDAY ... final Day d = Day.values()[ordinalOfWednesday]; IO.println(d); |
|---|---|
| Result | Wednesday |
enum instance by its
constant's textual name | Code | final DoorState state = DoorState.valueOf("LOCKED");
IO.println(state); |
|---|---|
| Result | LOCKED |
No. 165
Getting a day's predecessor
|
Q: |
Extend Expected execution:
|
||||
|
A: |
|
No. 166
Compass directions
|
Q: |
We consider an eight direction compass rose: Provide an
TipProvide an appropriate constructor among with suitable
«internal» instance attributes and a corresponding |
||||||
|
A: |
The desired output contains both a given direction's oral description and a 0 - 360° degree value. We thus start by: For creating a public enum Direction {
N(0, "north"); ❶
Direction(final int degree, final String fullName) { ❷
this.degree = degree;
this.fullName = fullName;
}
public final int degree; ❸
public final String fullName;
}
For creating output texts like e.g.
|
No. 167
Compass direction neighbours
|
Q: |
We would like to «invert» a given direction
e.g. turning
Tip
|
||
|
A: |
Using a Following the provided hint we may instead calculate the opposing direction: |
No. 168
Former zodiac examination task
|
Q: |
Implement the zodiac task 2 from the 2024 winter examination implementation project. |
