Presentation is loading. Please wait.

Presentation is loading. Please wait.

Enumerated DATA Types Enum Types Enumerated Data Types

Similar presentations


Presentation on theme: "Enumerated DATA Types Enum Types Enumerated Data Types"— Presentation transcript:

1 Enumerated DATA Types Enum Types Enumerated Data Types
February 18, 2019

2 What is an enumerated type?
Sometimes, a special data type is needed that has a small number of specific values Day of the Week: Sunday, Monday, … College Classification: Freshman, Sophomore, … Political Party: Republican, Democrat, … Sex: Female, Male Suit: Clubs, Diamonds, Hearts, Spades Coin: Penny, Nickel, Dime, Quarter, … The Enumerated Data Type (enum) is designed for this purpose in Java Enumerated Data Types February 18, 2019

3 List of all possible values
Enumerated Types Known as an enum, it requires a declaration and definition similar to a class Syntax: enum typeName { one or more enum constants } Definition example: enum Day { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY } Declaration of an instance of the Day type: Day workDay; // ref to entity of type Day named workDay Assignment of a value: Day workDay = Day.WEDNESDAY; List of all possible values Enumerated Data Types February 18, 2019

4 Enumerated Types Best practice is to define enumerated data types in their own .java files For example: Enumerated Data Types February 18, 2019

5 Enumerated Types An enum is a specialized class address Day.SUNDAY
Each value is an object of type Day, a specialized enum class Day workDay = Day.WEDNESDAY; The workDay variable refers to (holds the address) of the Day.WEDNESDAY object Day.SUNDAY Day.MONDAY Day.TUESDAY address Day.WEDNESDAY Day.THURSDAY Day.FRIDAY Day.SATURDAY Enumerated Data Types February 18, 2019

6 Enumerated Types - Methods
toString – returns name of the constant ordinal – returns the zero-based position of the constant in the enum. The values in the definition of the enum type are numbered beginning with 0. For example the ordinal for Day.THURSDAY is 4 equals – accepts an object as an argument and returns true if the argument is equal to the calling enum constant compareTo - accepts an object as an argument and returns a negative integer if the calling constant’s ordinal < than the argument’s ordinal, a positive integer if the calling constant’s ordinal > than the argument’s ordinal, and zero if the calling constant’s ordinal == the argument’s ordinal Enumerated Data Types February 18, 2019

7 Enumerated Types: Methods, continued
values – requires no arguments but returns an array of all of the values the enumerated type has valueOf – static method that returns the enumerated value corresponding to the string used as a parameter; an exception will be thrown if the string parameter does not exactly match one of the enumerated type’s values including the correct case Enumerated Data Types February 18, 2019

8 Converting Between String and Enum Type
Enum type String: use PhotoType.JPG.toString ( ) If you have an enum value and need a String, use the toString method of the enum type String Enum: use PhotoType.valueOf (myStr) If you have a String and need the equivalent enum value, use the valueOf method of the enum type These methods are intended for conversions between valid enum values and the corresponding strings only

9 Example Day.java Program output EnumDemo.java Enumerated Data Types
February 18, 2019

10 Examples – values ( ) method
Face.java For every Face f in the set of all values of Face … Called an “enhanced for” Output Results Used to convert a String to its corresponding enumerated type’s value Enumerated Data Types February 18, 2019

11 Enumerated Types - Switching
One may use an enum constant with an if, while, or switch See the example on next two slides Enumerated Data Types February 18, 2019

12 This represents the entire contents of the file named Coin.java
Enum Example This represents the entire contents of the file named Coin.java Enumerated Data Types February 18, 2019

13 Enum example, continued
Enumerated Data Types February 18, 2019


Download ppt "Enumerated DATA Types Enum Types Enumerated Data Types"

Similar presentations


Ads by Google