Enum Types ENUMERATED DATA TYPES Enumerated Data Types

  • Slides: 13
Download presentation
Enum Types ENUMERATED DATA TYPES Enumerated Data Types June 15, 2021 1

Enum Types ENUMERATED DATA TYPES Enumerated Data Types June 15, 2021 1

What is an enumerated type? Sometimes, a special data type is needed that has

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 June 15, 2021 2

Enumerated Types Known as an enum, it requires a declaration and definition similar to

Enumerated Types Known as an enum, it requires a declaration and definition similar to a class List of all possible values Syntax: enum type. Name { 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 work. Day; // ref to entity of type Day named work. Day Assignment of a value: Day work. Day = Day. WEDNESDAY; Enumerated Data Types June 15, 2021 3

Enumerated Types Best practice is to define enumerated data types in their own. java

Enumerated Types Best practice is to define enumerated data types in their own. java files For example: Enumerated Data Types June 15, 2021 4

Enumerated Types An enum is a specialized class Each value is an object of

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

Enumerated Types - Methods to. String – returns name of the constant ordinal –

Enumerated Types - Methods to. String – 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 compare. To - 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 June 15, 2021 6

Enumerated Types: Methods, continued values – requires no arguments but returns an array of

Enumerated Types: Methods, continued values – requires no arguments but returns an array of all of the values the enumerated type has value. Of – 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 June 15, 2021 7

Converting Between String and Enum Type Enum type String: use Photo. Type. JPG. to.

Converting Between String and Enum Type Enum type String: use Photo. Type. JPG. to. String ( ) If you have an enum value and need a String, use the to. String method of the enum type String Enum: use Photo. Type. value. Of (my. Str) If you have a String and need the equivalent enum value, use the value. Of method of the enum type These methods are intended for conversions between valid enum values and the corresponding strings only

Example Day. java Program output Enum. Demo. java Enumerated Data Types June 15, 2021

Example Day. java Program output Enum. Demo. java Enumerated Data Types June 15, 2021 9

Examples – values ( ) method Face. java For every Face f in the

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

Enumerated Types - Switching One may use an enum constant with an if, while,

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 June 15, 2021 11

Enum Example This represents the entire contents of the file named Coin. java Enumerated

Enum Example This represents the entire contents of the file named Coin. java Enumerated Data Types June 15, 2021 12

Enum example, continued Enumerated Data Types June 15, 2021 13

Enum example, continued Enumerated Data Types June 15, 2021 13