enumerated types define a new data type and list all possible values: enum season {winter, spring,...
TRANSCRIPT
Enumerated Types• Define a new data type and list all possible values:
enum Season {winter, spring, summer, fall}
• Then the new type can be used to declare variables
Season time;
• And then the variable can be assigned a value:
time = Season.fall;
• The only values this variable can be assigned are the ones from the enum definition
Copyright © 2012 Pearson Education, Inc.
Why would we want to use enums?
Copyright © 2012 Pearson Education, Inc.
//********************************************************************// IceCream.java Author: Lewis/Loftus//// Demonstrates the use of enumerated types.//********************************************************************
public class IceCream{ enum Flavor {vanilla, chocolate, strawberry, fudgeRipple, coffee, rockyRoad, mintChocolateChip, cookieDough}
//----------------------------------------------------------------- // Creates and uses variables of the Flavor type. //----------------------------------------------------------------- public static void main (String[] args) { Flavor cone1, cone2, cone3;
cone1 = Flavor.rockyRoad; cone2 = Flavor.chocolate;
System.out.println ("cone1 value: " + cone1); System.out.println ("cone1 ordinal: " + cone1.ordinal()); System.out.println ("cone1 name: " + cone1.name());
continued
Copyright © 2012 Pearson Education, Inc.
continued
System.out.println (); System.out.println ("cone2 value: " + cone2); System.out.println ("cone2 ordinal: " + cone2.ordinal()); System.out.println ("cone2 name: " + cone2.name());
cone3 = cone1;
System.out.println (); System.out.println ("cone3 value: " + cone3); System.out.println ("cone3 ordinal: " + cone3.ordinal()); System.out.println ("cone3 name: " + cone3.name()); }}
Copyright © 2012 Pearson Education, Inc.
continued
System.out.println (); System.out.println ("cone2 value: " + cone2); System.out.println ("cone2 ordinal: " + cone2.ordinal()); System.out.println ("cone2 name: " + cone2.name());
cone3 = cone1;
System.out.println (); System.out.println ("cone3 value: " + cone3); System.out.println ("cone3 ordinal: " + cone3.ordinal()); System.out.println ("cone3 name: " + cone3.name()); }}
Outputcone1 value: rockyRoadcone1 ordinal: 5cone1 name: rockyRoadcone2 value: chocolatecone2 ordinal: 1cone2 name: chocolatecone3 value: rockyRoadcone3 ordinal: 5cone3 name: rockyRoad
A basic enumerated type
public enum CommandWord{ // A value for each command word, // plus one for unrecognised commands. GO, QUIT, HELP, UNKNOWN;}
• Each name represents an object of the enumerated type, e.g. CommandWord.HELP. • Enumerated objects are not created directly by the programmer.
Another enumerated typepublic enum CommandWord{ // A value for each command word, // plus one for unrecognised commands. GO(“go”), QUIT(“quit”), HELP(“help”), UNKNOWN(“unknown); private String commandWord;
// Initialize the command word CommandWord (String commandword) { this. commandWord = commandWord; } public String toString() { return commandWord; }}
.
Enumerated Types• An enumerated type definition can be more interesting
than a simple list of values
• Because they are like classes, we can add additional instance data and methods
• We can define an enum constructor as well
• Each value listed for the enumerated type calls the constructor
• See Season.java • See SeasonTester.java
Copyright © 2012 Pearson Education, Inc.
Copyright © 2012 Pearson Education, Inc.
//********************************************************************// Season.java Author: Lewis/Loftus//// Enumerates the values for Season.//********************************************************************
public enum Season{ winter ("December through February"), spring ("March through May"), summer ("June through August"), fall ("September through November");
private String span;
//----------------------------------------------------------------- // Constructor: Sets up each value with an associated string. //----------------------------------------------------------------- Season (String months) { span = months; }
//----------------------------------------------------------------- // Returns the span message for this value. //----------------------------------------------------------------- public String getSpan() { return span; }
Copyright © 2012 Pearson Education, Inc.
//********************************************************************// SeasonTester.java Author: Lewis/Loftus//// Demonstrates the use of a full enumerated type.//********************************************************************
public class SeasonTester{ //----------------------------------------------------------------- // Iterates through the values of the Season enumerated type. //----------------------------------------------------------------- public static void main (String[] args) { for (Season time : Season.values()) System.out.println (time + "\t" + time.getSpan()); }}
Enumerated Types• Every enumerated type contains a static method called values that returns a list of all possible values for that type
• The list returned from values can be processed using a for-each loop
• An enumerated type cannot be instantiated outside of its own definition
• A carefully designed enumerated type provides a versatile and type-safe mechanism for managing data
Copyright © 2012 Pearson Education, Inc.