- Forward


Developing Enumerated Types
With Examples in Java


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Review
Back SMYC Forward
  • What is a set?
    • A finite or infinite collection of unique elements/members (in which order may or may not have significance)
    • Expand
  • How are sets defined?
    • Extension - Listing all of the elements/members
      (e.g., \(S = \{1, 3, 5\}\))
    • Intension - Describing the properties of the elements/members
      (e.g., \(T = \{t: 0 \leq t \leq 10\}\))
    • Expand
Enumerated Types
Back SMYC Forward
  • 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)
Enumerated Types in Java
Back SMYC Forward
  • 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)
The Design and Implementation of Enumerated Types
Back SMYC Forward
  • 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
Properties of the enum vs. Properties of the Instances
Back SMYC Forward
  • 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)
Methods Available to All Enumerated Types in Java
Back SMYC Forward

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
An Example
Back SMYC Forward
  • Suppose:
    • We are designing and implementing a product that needs a calendar
  • An Obvious enum:
    • The months of the year
An Example: The Initial Encapsulation
Back SMYC Forward
images/Month_InitialEncapsulation.gif
javaexamples/oopbasics/months/v1/Month.java
 
An Example: Using the Initial Encapsulation
Back SMYC Forward
javaexamples/oopbasics/months/v1/MonthDriver.java
 
An Example: Adding Attributes
Back SMYC Forward
images/Month_Attributes.gif
An Example: Adding Methods
Back SMYC Forward
images/Month_Methods.gif
An Example: Refining the Encapsulation
Back SMYC Forward
images/Month_Final.gif
An Example: An Implementation of the Final Design
Back SMYC Forward
javaexamples/oopbasics/months/v2/Month.java
 
An Example: Using the Month enum
Back SMYC Forward
javaexamples/oopbasics/months/v2/MonthDriver.java
 
Alternatives to Enumerated Types (and their Shortcomings)
Back SMYC Forward
  • 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
There's Always More to Learn
Back -