Enumerated DATA Types

Download Report

Transcript Enumerated DATA Types

Enum Types
ENUMERATED DATA TYPES
Enumerated Data Types
July 21, 2015
1
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
July 21, 2015
2
Enumerated Types
 Known as an enum, it requires a declaration
and definition similar to a class
 Syntax:
List of all possible values
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; // entity of type Day named workDay
 Assignment of a value:
Day workDay = Day.WEDNESDAY;
Enumerated Data Types
July 21, 2015
3
Enumerated Types
 Best practice is to define enumerated data
types in their own .java files
 For example:
Enumerated Data Types
July 21, 2015
4
Enumerated Types
 An enum is a specialized class
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
address
Day.SUNDAY
Day.MONDAY
Day.TUESDAY
Day.WEDNESDAY
Day.THURSDAY
Day.FRIDAY
Day.SATURDAY
Enumerated Data Types
July 21, 2015
5
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
July 21, 2015
6
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
July 21, 2015
7
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
Example
Day.java
Program output
EnumDemo.java
Enumerated Data Types
July 21, 2015
9
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
July 21, 2015
10
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
July 21, 2015
11
Enum Example
This represents the entire
contents of the file named
Coin.java
Enumerated Data Types
July 21, 2015
12
Enum example, continued
Enumerated Data Types
July 21, 2015
13
Example
Enumerated Data Types
July 21, 2015
14
Example, part 2
Enumerated Data Types
July 21, 2015
15