CPSC1301 Computer Science 1 - PowerPoint PPT Presentation

1 / 9
About This Presentation
Title:

CPSC1301 Computer Science 1

Description:

The ordinal method returns the ordinal value of the object ... case WEST : System.out.println ('Move west'); break; default: ... – PowerPoint PPT presentation

Number of Views:43
Avg rating:3.0/5.0
Slides: 10
Provided by: columbusst
Category:

less

Transcript and Presenter's Notes

Title: CPSC1301 Computer Science 1


1
CPSC1301Computer Science 1
  • Enumerated Data Types

2
Enumerated Types
  • Java allows you to define an enumerated type,
    which can then be used to declare variables
  • An enumerated type establishes all possible
    values for a variable of that type
  • The values are identifiers of your own choosing
  • The following declaration creates an enumerated
    type called Season
  • enum Season WINTER, SPRING, SUMMER, FALL
  • Any number of values can be listed

3
Enumerated Types
  • Once a type is defined, a variable of that type
    can be declared
  • Season time
  • and it can be assigned a value
  • time Season.FALL
  • The values are specified through the name of the
    type
  • Enumerated types are type-safe you cannot
    assign any value other than those listed

4
Ordinal Values
  • Internally, each value of an enumerated type is
    stored as an integer, called its ordinal value
  • The first value in an enumerated type has an
    ordinal value of zero, the second one, and so on
  • However, you cannot assign a numeric value to an
    enumerated type, even if it corresponds to a
    valid ordinal value

5
Enumerated Types
  • The declaration of an enumerated type is a
    special type of class, and each variable of that
    type is an object
  • The ordinal method returns the ordinal value of
    the object
  • The name method returns the name of the
    identifier corresponding to the object's value
  • See IceCream.java, Compass.java, Breeds.java in
    source programs 3.5 (WebCT)

6
// Compass.java class Compass public enum
Direction NORTH, SOUTH, EAST, WEST public
static void main (String args) for
(Direction dir Direction.values ())
System.out.println (dir) switch (dir)
case NORTH System.out.println ("Move north")
break case SOUTH System.out.println
("Move south") break case EAST
System.out.println ("Move east") break
case WEST System.out.println ("Move west")
break default throw new
AssertionError ("Unknown direction")
7
Enumerated Types
  • In Chapter 3 we introduced enumerated types,
    which define a new data type and list all
    possible values of that type

enum Season WINTER, SPRING, SUMMER, FALL
  • Once established, the new type can be used to
    declare variables

Season time
  • The only values this variable can be assigned are
    the ones established in the enum definition

8
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, SeasonTester.java in source
    programs 6.6 (WebCT)

9
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 is an iterator, so
    a for loop can be used to process them easily
  • 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
Write a Comment
User Comments (0)
About PowerShow.com