1 of 14

Lecture 9 - CS 159 enumerated types

Prof. Alvin Chao

Due this week:

  • Thu 11pm Quiz Loops
  • Fri Exam 1(in class)
  • Next Week�Wed Lab 4B�Fri Lab 5�Fri Reading quiz Testing

2 of 14

Enumerated Types

  • Definition:
    • A definition of a set that is written by listing the elements (i.e., an extensive definition of a set)
    • The set of elements so defined
  • Examples:
    • Months of the Year (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec)
    • Marital Statuses (Never Been Married, Engaged, Married, Divorced, Widowed)
    • Suits in a Deck of Cards (Clubs Diamonds, Hearts, Spades)
    • Cards in a Deck of Cards (Ace, 2, ..., 10, Jack, Queen, King)

3 of 14

Enumerated Types in Java

  • Value Type or Reference Type?
    • Enumerated types are reference types�
  • Ordered or Unordered?
    • Enumerated types are ordered (though the order may not be meaningful/useful)

4 of 14

Design + Implementation

  • Identify the Elements:
    • List all of the elements in the set
  • Identify the Properties of the Elements:
    • Declare the names and types of attributes
  • Identify the Behaviors of the Elements:
    • Declare the signatures of methods
  • Refine the Design:
    • Think about ways the design can be improved before your start writing code

5 of 14

Properties of enum vs. instance

  • Properties of the Instances:
    • Attributes and behaviors of individual instances (in Java, the non-static variables and methods)
  • Properties of the enum:
    • Attributes and behaviors of the set of instances (in Java, the static variables and methods)

6 of 14

Methods available to enum in Java

Though it is not immediately obvious from the documentation for the Java API, all enumerated types have the following methods "implemented for them".�Static Methods:� - values() returns an array containing all of the objects in the set� - valueOf(String) returns the instance with the specified identifier (or throws IllegalArgumentException)�Non-Static Methods:� - int compareTo(E other) which uses the order of the elements� - boolean equals(E other)� - ordinal() returns the ordinal value of the instance (0-based)� - toString() returns a String representation of the identifier

7 of 14

Example

  • Suppose:
    • We are designing and implementing a product that needs a calendar
  • An Obvious enum:
    • The months of the year

8 of 14

Example - Initial Encapsulation

  • public enum Month
  • {
  • JANUARY, FEBRUARY, MARCH, � APRIL, MAY, JUNE, JULY, � AUGUST, SEPTEMBER, OCTOBER, � NOVEMBER, DECEMBER;
  • }

9 of 14

Using Initial Encapsulation

  • public class MonthDriver
  • {
  • public static void main(String[] Args)
  • {
  • Month[] all, fun;
  • fun = new Month[3];
  • fun[0] = Month.JUNE;
  • fun[1] = Month.JULY;
  • fun[2] = Month.AUGUST;
  • System.out.println("\nThe fun months:");
  • for (int i=0; i<fun.length; i++)
  • {
  • System.out.println(fun[i].toString());
  • }
  • System.out.println("\nAll months after May:");
  • all = Month.values();
  • for (int i=0; i<all.length; i++)
  • {
  • if (all[i].compareTo(Month.MAY) > 0)
  • {
  • System.out.println(all[i].toString());
  • }
  • }
  • }
  • }

10 of 14

Adding Attributes

11 of 14

Adding Methods

12 of 14

Final Design

  • public enum Month
  • {
  • JANUARY ("January", 31),
  • FEBRUARY ("February", 28),
  • MARCH ("March", 31),
  • APRIL ("April", 30),
  • MAY ("May", 31),
  • JUNE ("June", 30),
  • JULY ("July", 31),
  • AUGUST ("August", 31),
  • SEPTEMBER ("September",30),
  • OCTOBER ("October", 31),
  • NOVEMBER ("November", 30),
  • DECEMBER ("December", 31);
  • private final int days;
  • private final String name;
  • private Month(String name, int days)
  • {
  • this.name = name;
  • this.days = days;
  • }

  • public String getAbbreviation()
  • {
  • String result;
  • if (name.length() > 3) result = name.substring(0,3)+".";
  • else result = name;
  • return result;
  • }
  • public int getNumber()
  • {
  • return ordinal()+1; // ordinal() returns the position in the declaration
  • }
  • public int getLength()
  • {
  • return days;
  • }
  • public static Month parseMonth(String s)
  • {
  • Month[] all;
  • all = Month.values();
  • for (int i=0; i<all.length; i++)
  • {
  • if (s.equalsIgnoreCase(all[i].name)
  • || s.equalsIgnoreCase(all[i].getAbbreviation()))
  • {
  • return all[i];
  • }
  • }
  • return null;
  • }
  • public String toString()
  • {
  • return name;
  • }
  • }

13 of 14

Using the Month enum

  • public class MonthDriver
  • {
  • public static void main(String[] Args)
  • {
  • int total;
  • Month[] fun;
  • fun = new Month[3];
  • fun[0] = Month.JUNE;
  • fun[1] = Month.JULY;
  • fun[2] = Month.AUGUST;
  • total = 0;
  • System.out.println("\nThe fun months:");
  • for (int i=0; i<fun.length; i++)
  • {
  • System.out.printf("%5s %3d\n",
  • fun[i].getAbbreviation(), fun[i].getLength());
  • total += fun[i].getLength();
  • }
  • System.out.printf("\n%5s %3d", " ", total);
  • }
  • }

14 of 14

Alternatives to enums

  • A "Set" of public static final String Objects:
    • They can only be validated at run-time
    • It is impossible to iterate over the group
  • An Array of public static final String Objects:
    • They can only be validated at run-time
    • They need to be referred to by index number
  • A "Set" of public static final int Values:
    • They can only be validated at run-time
    • They aren't useful for user interaction (e.g., they are meaningless when printed)
  • A Mixed Approach:
    • They can only be validated at run-time
    • Inconsistencies can arise