File tree Expand file tree Collapse file tree 4 files changed +70
-0
lines changed Expand file tree Collapse file tree 4 files changed +70
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
1
+ package viktorvoltz ;
2
+ public enum Signal {
3
+ GREEN , YELLOW , RED ,
4
+ }
You canβt perform that action at this time.
0 commit comments