/* * EnumTest.java * * Created on June 2, 2004, 3:03 PM */ //need this for the card example below import java.awt.Color; import java.util.ArrayList; /** * * @author cpresser */ public class EnumTest { //Create an enumerated type // NOTE: The C style SUNDAY=1 does not fly here // In some builds of 1.5 enum can not be local in a method, in // others it can. enum Day {SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY} /** * @param args the command line arguments */ public static void main(String[] args) { // Test for the enum type //simple use of an enum (CS1) //create a variable of type day. //NOTE Day d = SUNDAY won't compile, the cases of the switch // statement, don't seem to have this scope issue. Day d = Day.SUNDAY; System.out.println("today is probably not " + d); boolean done = false; //loop through the days of the week assigning a value to the variable while(!done){ //the enumerated type has a toString method for such eventualities // and an ordinal method to get a 0 based value System.out.println("It is now: " + d + ", day: " + d.ordinal()); switch(d){ //cases work w/out the type specified. I can't explain why. case SUNDAY: d = Day.MONDAY; break; case MONDAY: d = Day.TUESDAY; break; case TUESDAY: d = Day.WEDNESDAY; break; case WEDNESDAY: d = Day.THURSDAY; break; case THURSDAY: d = Day.FRIDAY; break; case FRIDAY: d = Day.SATURDAY; break; case SATURDAY: d = Day.SUNDAY; done = true; break; } } //use the new "for each" looping structure (and an iterator //see the other examples) for(Day day : Day.values()){ System.out.println(day); } } }