enum
replacing class
public enum Day {
MONDAY("Monday"),
TUESDAY("Tuesday"),
...
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
}
}
|
Compile time warning: Modifier 'private' is redundant for |
|
Compile time error: Modifier 'public' not allowed here |
Prohibits enum
external instance creation.
No. 124
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
For creating output texts like e.g.
|
No. 125
Compass direction neighbours
Q: |
We would like to «invert» a given direction
e.g. turning
Tip
|
||
A: |
Using a
Using the hint we may instead calculate the opposing direction:
|