-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnumeratorDemo.java
More file actions
38 lines (36 loc) · 1.11 KB
/
Copy pathEnumeratorDemo.java
File metadata and controls
38 lines (36 loc) · 1.11 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
// An enumeration of Transport varieties.
enum Transport {
CAR, TRUCK, AIRPLANE, TRAIN, BOAT
}
class EnumeratorDemo {
public static void main(String[] args)
{
Transport tp;
tp = Transport.AIRPLANE;
// Output an enum value.
System.out.println("Value of tp: " + tp);
System.out.println();
tp = Transport.TRAIN;
// Compare two enum values.
if (tp == Transport.TRAIN)
System.out.println("tp contains TRAIN.\n");
// Use an enum to control a switch statement.
switch (tp) {
case CAR:
System.out.println("A car carries people.");
break;
case TRUCK:
System.out.println("A truck carries freight.");
break;
case AIRPLANE:
System.out.println("An airplane flies.");
break;
case TRAIN:
System.out.println("A train runs on rails.");
break;
case BOAT:
System.out.println("A boat sails on water.");
break;
}
}
}