Skip to content

Commit 09ce102

Browse files
committed
Enumarated Types πŸŽ„
1 parent dfe7a30 commit 09ce102

File tree

4 files changed

+70
-0
lines changed

4 files changed

+70
-0
lines changed

β€ŽEnumaratedTypes/EnumClass.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
enum Shrubbery {
2+
GROUND,
3+
CRAWLING,
4+
HANGING
5+
}
6+
7+
public class EnumClass {
8+
public static void main(String[] args) {
9+
for (Shrubbery s : Shrubbery.values()) {
10+
System.out.println(s + " ordinal: " + s.ordinal());
11+
System.out.println(s.compareTo(Shrubbery.CRAWLING) + " ");
12+
System.out.println(s.equals(Shrubbery.CRAWLING) + " ");
13+
System.out.println(s == Shrubbery.CRAWLING);
14+
System.out.println(s.getDeclaringClass());
15+
System.out.println(s.name());
16+
System.out.println("----------------------");
17+
}
18+
for (String s: "HANGING CRAWLING GROUND".split(" ")){
19+
Shrubbery shrub = Enum.valueOf(Shrubbery.class, s);
20+
System.out.println(shrub);
21+
}
22+
}
23+
}

β€ŽEnumaratedTypes/OzWitch.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
public enum OzWitch {
2+
// Instances must be defined first, before methods:
3+
WEST("Miss Gulch, aka the Wicked Witch of the West"),
4+
NORTH("Glinda, the Good Witch of the North"),
5+
EAST("Wicked Witch of the East, wearer of the Ruby " +
6+
"Slippers, crushed by Dorothy’s house"),
7+
SOUTH("Good by inference, but missing");
8+
private String description;
9+
// Constructor must be package or private access:
10+
private OzWitch(String description) {
11+
this.description = description;
12+
}
13+
public String getDescription() { return description; }
14+
public static void main(String[] args) {
15+
for(OzWitch witch : OzWitch.values())
16+
System.out.println(witch + ": " + witch.getDescription());
17+
}
18+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package viktorvoltz;
2+
import static viktorvoltz.Signal.*;
3+
4+
public class Exercise1 {
5+
6+
Signal color = RED;
7+
public void change(){
8+
switch(color){
9+
case RED: color = GREEN; break;
10+
case GREEN: color = YELLOW; break;
11+
case YELLOW: color = RED; break;
12+
}
13+
}
14+
@Override
15+
public String toString() {
16+
return "Traffic Light is: " + color;
17+
}
18+
public static void main(String[] args) {
19+
Exercise1 exercise1 = new Exercise1();
20+
for (int i = 0; i < 7; i++){
21+
System.out.println(exercise1);
22+
exercise1.change();
23+
}
24+
}
25+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package viktorvoltz;
2+
public enum Signal {
3+
GREEN, YELLOW, RED,
4+
}

0 commit comments

Comments
 (0)