// Weekday class and Day enum. // class using Day enumeration public class Weekday { //Define an enum for use in this class // for wider use, put in a file of its own // with usual java naming -- Day.java in this case enum Day { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY } // enum is now a type which can take on only selected values Day day; public Weekday(Day day) { this.day = day; } // print out which classes I have today public void classes() { // enums can be used in switch statements switch (day) { case MONDAY: case WEDNESDAY: System.out.println("VB and 102"); break; case TUESDAY: case THURSDAY: System.out.println("Java and 102"); break; case FRIDAY: System.out.println("Meetings"); break; case SATURDAY: case SUNDAY: // my best subject System.out.println("sleep."); break; } } public String toString() { return day.toString(); } public static void main(String[] args) { // various variables Weekday firstDay = new Weekday(Day.MONDAY); firstDay.classes(); Weekday thirdDay = new Weekday(Day.THURSDAY); thirdDay.classes(); Weekday fifthDay = new Weekday(Day.FRIDAY); fifthDay.classes(); Weekday sixthDay = new Weekday(Day.SATURDAY); sixthDay.classes(); Weekday seventhDay = new Weekday(Day.SUNDAY); seventhDay.classes(); // enum type has .values() which returns array of enum values Weekday [] daylist = new Weekday[Day.values().length]; // foreach loop -- curr is set to each Day in the array in turn for(Day curr : Day.values()) { // .ordinal method pulls int position out of enum daylist[curr.ordinal()] = new Weekday(curr); } //Weekday q = new Weekday(Day.valueOf("TUESDAY")); Weekday q = new Weekday(Day.valueOf("Tuesday".toUpperCase())); // causes Exception //Weekday q = new Weekday(Day.valueOf("Tuesday")); Weekday q = new Weekday(Day.valueOf("Tuesday".toUpperCase())); System.out.println(q); } }