-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathEnumDemo4.java
More file actions
50 lines (40 loc) · 1.28 KB
/
EnumDemo4.java
File metadata and controls
50 lines (40 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package chapter12;
//Demonstrate ordinal(),compareTo() and equals()
//An enumeration of apple varieties
enum Apple4 {
Jonathan, GoldenDel, RedDel, Winesap, Cortland
}
public class EnumDemo4 {
public static void main(String[] args) {
Apple4 ap, ap2, ap3;
//Obtain all ordinal values using ordinal()
System.out.println("Here are all apple constants and their ordinal values");
for (Apple4 a : Apple4.values()) {
System.out.println(a + " " + a.ordinal());
}
ap = Apple4.RedDel;
ap2 = Apple4.GoldenDel;
ap3 = Apple4.RedDel;
System.out.println();
//Demonstrate compareTo() and equals()
if (ap.compareTo(ap2) < 0) {
System.out.println(ap + " comes before" + ap2);
}
if (ap.compareTo(ap2) > 0) {
System.out.println(ap2 + " comes before" + ap);
}
if (ap.compareTo(ap3) == 0) {
System.out.println(ap + " equals " + ap3);
}
System.out.println();
if (ap.equals(ap2)) {
System.out.println("Error!");
}
if (ap.equals(ap3)) {
System.out.println(ap + " equals " + ap3);
}
if (ap == ap3) {
System.out.println(ap + " equals " + ap3);
}
}
}