Enumerations

This page is under construction. Please come back later.
import java.util.HashMap;
import java.util.Map;
import org.pureprogrammer.Utils;
public class Enums1 {
enum CompassPoint {
NORTH,EAST,SOUTH,WEST
}
static String compassPointToString(final CompassPoint c) {
String result = "";
switch (c) {
case NORTH:
result = "North";
break;
case EAST:
result = "East";
break;
case SOUTH:
result = "South";
break;
case WEST:
result = "West";
break;
}
return result;
}
static CompassPoint turnRight(final CompassPoint c) {
if (c == CompassPoint.NORTH) {
return CompassPoint.EAST;
} else if (c == CompassPoint.EAST) {
return CompassPoint.SOUTH;
} else if (c == CompassPoint.SOUTH) {
return CompassPoint.WEST;
} else {
return CompassPoint.NORTH;
}
}
public static void main(String[] args) {
CompassPoint cp1 = CompassPoint.NORTH;
System.out.println(Utils.join("", "cp1: ", cp1));
System.out.println(Utils.join("", "SOUTH: ", compassPointToString(CompassPoint.SOUTH)));
System.out.println(Utils.join("", "turnRight(cp1): ", compassPointToString(turnRight(cp1))));
final CompassPoint CP2 = CompassPoint.EAST;
if (cp1 == CP2) {
System.out.println("cp1 == cp2");
} else {
System.out.println("cp1 != cp2");
}
cp1 = CP2;
if (cp1 == CP2) {
System.out.println("cp1 == cp2");
} else {
System.out.println("cp1 != cp2");
}
}
}
Output
$ javac -Xlint Enums1.java
$ java -ea Enums1
cp1: NORTH
SOUTH: South
turnRight(cp1): East
cp1 != cp2
cp1 == cp2
import java.util.HashMap;
import java.util.Map;
import org.pureprogrammer.Utils;
public class Enums2 {
enum Color {
RED(1),
GREEN(2),
YELLOW(3),
BLUE(4),
MAGENTA(5),
CYAN(6);
private static Map<Integer, Color> byValue = new HashMap<>();
static {
for (Color e: values()) {
byValue.put(e.value, e);
}
}
public final int value;
private Color(int v) { value = v; }
public int value() { return value; }
public static Color valueOf(int i) {
Color result = byValue.get(i);
return result;
}
}
static Color combineColors(final Color c1, final Color c2) {
final int i1 = c1.value();
final int i2 = c2.value();
final int RESULT = i1 | i2;
final Color RETURN_COLOR = Color.valueOf(RESULT);
return RETURN_COLOR;
}
public static void main(String[] args) {
Color c1 = Color.BLUE;
System.out.println(Utils.join("", "c1: ", c1));
System.out.println(Utils.join("", "RED: ", Color.RED));
System.out.println(Utils.join("", "combineColors(c1, RED): ", combineColors(c1, Color.RED)));
final Color c2 = Color.GREEN;
System.out.println(Utils.join("", "combineColors(c1, c2): ", combineColors(c1, c2)));
if (c1 == c2) {
System.out.println("c1 == c2");
} else {
System.out.println("c1 != c2");
}
c1 = c2;
if (c1 == c2) {
System.out.println("c1 == c2");
} else {
System.out.println("c1 != c2");
}
final Color BAD = Color.valueOf(7);
System.out.println(Utils.join("", "bad: ", BAD));
}
}
Output
$ javac -Xlint Enums2.java
$ java -ea Enums2
c1: BLUE
RED: RED
combineColors(c1, RED): MAGENTA
combineColors(c1, c2): CYAN
c1 != c2
c1 == c2
bad: null
Questions
- {{Who's on first?}}
- {{Who's on second?}}
- {{Who's on third?}}
Projects
More ★'s indicate higher difficulty level.
References
- [[Java Language Specification]], Java SE 21 Edition, Gosling, et. al., 2023.
- [[Java Tutorials]]
- [[Java at TutorialsPoint]]
- Download Java at [[Amazon Corretto]], [[Azul Zulu]], [[Eclipse Temurin]] or [[Oracle JDK]]
Pure Programmer


