KaiquanMah commited on
Commit
4142885
·
verified ·
1 Parent(s): 6bd73f4

enum Topic1 {VALUE1, ...}; "someStr" == Topic1.VALUE1;

Browse files
Week 7: Enum, Generic Type, Streams, write to file, class diagram/01A. Enum Classes+++ ADDED
@@ -0,0 +1,244 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Last week we saw a few examples of storing constant values as static variables in a class.
2
+ In older versions of Java, it was common to use static variables in this way:
3
+
4
+ class Suit {
5
+ public static final int SPADES = 1;
6
+ public static final int HEARTS = 2;
7
+ public static final int CLUBS = 3;
8
+ public static final int DIAMONDS = 4;
9
+ }
10
+
11
+
12
+
13
+
14
+
15
+ Now, the playing card class could look something like this:
16
+
17
+ class PlayingCard {
18
+ private int suit;
19
+ private int number;
20
+
21
+ public PlayingCard(int suit, int number) {
22
+ this.suit = suit;
23
+ this.number = number;
24
+ }
25
+
26
+ public int getSuit() {
27
+ return suit;
28
+ }
29
+
30
+ public int getNumber() {
31
+ return number;
32
+ }
33
+ }
34
+
35
+
36
+
37
+
38
+ ...and a new playing card could be created like this:
39
+
40
+ public static void main(String[] args) {
41
+ PlayingCard sevenOfSpades = new PlayingCard(Suit.SPADES, 7);
42
+ }
43
+
44
+
45
+
46
+
47
+
48
+ ===============================================================
49
+
50
+
51
+
52
+ However, the method has a clear shortcoming:
53
+ the constant numerical values do not automatically associate with their meanings.
54
+
55
+ Even if we agreed that the numerical value 1 corresponds to the suit of spades in a deck of cards,
56
+ requesting the suit will nevertheless return an integer:
57
+
58
+ public static void main(String[] args) {
59
+ PlayingCard sevenOfSpades = new PlayingCard(Suit.SPADES, 7);
60
+ System.out.println("Card's suit: " + sevenOfSpades.getSuit());
61
+ System.out.println("Card's number: " + sevenOfSpades.getNumber());
62
+ }
63
+
64
+ The program prints:
65
+ Card's suit: 1
66
+ Card's number: 7
67
+
68
+
69
+ A better solution is to use the enum class introduced in Java 5.
70
+
71
+
72
+
73
+
74
+
75
+
76
+
77
+
78
+
79
+ ==============================================
80
+
81
+
82
+
83
+
84
+ What is an enum class?
85
+
86
+ The purpose of an enum class is to define a SET of VALUES.
87
+ Typical examples could be
88
+ the suits in a card game,
89
+ compass directions or
90
+ the houses at Hogwarts.
91
+
92
+ The class is defined with the keyword 'enum'.
93
+ In its simplest form, we just write the values that belong to the set
94
+ inside the class definition, separated by commas:
95
+
96
+ enum Suit {
97
+ SPADES, HEARTS, CLUBS, DIAMONDS
98
+ }
99
+
100
+
101
+
102
+
103
+
104
+
105
+ Now we can change the definition of the card game class so that the suit is of type 'Suit', not an integer.
106
+ This also limits the possible values: the suit of the card cannot be anything other than one of the four defined in the enum class Suit.
107
+
108
+ class PlayingCard {
109
+ private Suit suit;
110
+ private int number;
111
+
112
+ public PlayingCard(Suit suit, int number) {
113
+ this.suit = suit;
114
+ this.number = number;
115
+ }
116
+
117
+ public Suit getSuit() {
118
+ return suit;
119
+ }
120
+
121
+ public int getNumber() {
122
+ return number;
123
+ }
124
+ }
125
+
126
+
127
+
128
+
129
+
130
+
131
+
132
+
133
+
134
+
135
+
136
+
137
+
138
+ This is now seen not only when creating a card, but also when printing the card's suit:
139
+
140
+ public static void main(String[] args) {
141
+ PlayingCard sevenOfClubs = new PlayingCard(Suit.CLUBS, 7);
142
+ System.out.println("Card's suit: " + sevenOfClubs.getSuit());
143
+ System.out.println("Card's number: " + sevenOfClubs.getNumber());
144
+ }
145
+
146
+ Now the program prints:
147
+ Card's suit: CLUBS
148
+ Card's number: 7
149
+
150
+
151
+
152
+
153
+
154
+
155
+
156
+
157
+
158
+ ==============================================
159
+
160
+
161
+
162
+
163
+
164
+
165
+
166
+
167
+ Using Enum Values
168
+
169
+ Enum type values can be directly compared with Java's equality and inequality operators.
170
+ // ==
171
+ // !=
172
+
173
+ If we want to check whether a card is a diamond, we can write:
174
+
175
+ public static void main(String[] args) {
176
+ PlayingCard card = new PlayingCard(Suit.DIAMONDS, 9);
177
+ if (card.getSuit() == Suit.DIAMONDS) {
178
+ System.out.println("The card is a diamond!");
179
+ }
180
+ }
181
+
182
+
183
+
184
+
185
+
186
+
187
+ ==============================================
188
+
189
+
190
+
191
+ As another example, let's define another enum class, which includes subjects taught:
192
+
193
+ enum Subject {
194
+ MATHEMATICS, PHYSICS, CHEMISTRY, BIOLOGY, SWEDISH
195
+ }
196
+
197
+
198
+
199
+
200
+
201
+
202
+ Now we can use the value as the type of the 'mainSubject' field in the 'Student' class, for example:
203
+
204
+ class Student extends Person {
205
+ private int credits;
206
+ private Subject mainSubject;
207
+
208
+ public Student(String name, String email, int credits, Subject mainSubject) {
209
+ super(name, email);
210
+ this.credits = credits;
211
+ this.mainSubject = mainSubject;
212
+ }
213
+
214
+ public int getCredits() {
215
+ return credits;
216
+ }
217
+
218
+ public Subject getMainSubject() {
219
+ return mainSubject;
220
+ }
221
+ }
222
+
223
+
224
+
225
+
226
+
227
+
228
+ Now it's easy to write a method, for example, that picks out the mathematics students from the student list:
229
+
230
+ public static ArrayList<Student> mathematicians(ArrayList<Student> students) {
231
+ ArrayList<Student> al = new ArrayList<>();
232
+ for (Student student : students) {
233
+ if (student.getMainSubject() == Subject.MATHEMATICS) {
234
+ al.add(student);
235
+ }
236
+ }
237
+ return al;
238
+ }
239
+
240
+
241
+ Enum classes also support more versatile (and complex) features, which are not covered in this course.
242
+ You can find more information at
243
+ https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html
244
+