JMU
Developing Enumerated Types
With Examples in Java


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu


Review
Enumerated Types
Enumerated Types in Java
The Design and Implementation of Enumerated Types
Properties of the enum vs. Properties of the Instances
Methods Available to All Enumerated Types 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".

An Example
An Example: The Initial Encapsulation
images/Month_InitialEncapsulation.gif
javaexamples/oopbasics/months/v1/Month.java
/**
 * A simple enumerated type for the months of the year
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public enum Month
{
   JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE, 
   JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER;
}
        
An Example: Using the Initial Encapsulation
javaexamples/oopbasics/months/v1/MonthDriver.java
/**
 * An example that uses the Month enum
 */
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());
            }
        }
    }
}
        
An Example: Adding Attributes
images/Month_Attributes.gif
An Example: Adding Methods
images/Month_Methods.gif
An Example: Refining the Encapsulation
images/Month_Final.gif
An Example: An Implementation of the Final Design
javaexamples/oopbasics/months/v2/Month.java
/**
 * An enumerated type for the months of the year
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 2.0
 */
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;

   /**
    * Explicit Value Constructor.
    *
    * @param name   The name of the Month
    * @param days   The number of days in the Month
    */
   private Month(String name, int days)
   {
      this.name = name;
      this.days = days;
   }

   /**
    * Get the 3-letter abbreviation for this Month.
    *
    * @return  The 3-letter abbreviation
    */
   public String getAbbreviation()
   {
      String       result;
      
      if (name.length() > 3) result = name.substring(0,3)+".";
      else                   result = name;

      return result;      
   }

   /**
    * Get the numeric value of this Month (in the interval [1,12]).
    *
    * @return  The numeric value of this Month
    */
   public int getNumber()
   {
      return ordinal()+1; // ordinal() returns the position in the declaration
   }

   /**
    * Get the (normal) number of days in this Month.
    *
    * @return  The normal number of days in this Month.
    */
   public int getLength()
   {
      return days;      
   }
   
   /**
    * Return the Month that corresponds to a given String.
    *
    * @param s   The String representation
    * @return    The corresponding Month (or null)
    */
   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;
   }
   
   /**
    * Get a String representation of this Month
    *
    * @return   The String representation (i.e., the name)
    */
   public String toString()
   {
      return name;      
   }
}
        
An Example: Using the Month enum
javaexamples/oopbasics/months/v2/MonthDriver.java
/**
 * An example that uses 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);
    }
}
        
Alternatives to Enumerated Types (and their Shortcomings)