Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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?

2 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

3 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()); }

4 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()); } Output cone1 value: rockyRoad cone1 ordinal: 5 cone1 name: rockyRoad cone2 value: chocolate cone2 ordinal: 1 cone2 name: chocolate cone3 value: rockyRoad cone3 ordinal: 5 cone3 name: rockyRoad

5 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.

6 Another enumerated type public 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; }.

7 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.

8 //******************************************************************** // 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; }

9 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()); }

10 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.


Download ppt "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."

Similar presentations


Ads by Google