Enumerated DATA Types - East Tennessee State University

Enumerated DATA Types - East Tennessee State University

Enum Types ENUMERATED DATA TYPES Enumerated Data Types 2/27/20 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 2/27/20 2 Enumerated Types Known as an enum, it requires a declaration and definition similar to a class List of all possible values

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; Enumerated Data Types 2/27/20 3

Enumerated Types Best practice is to define enumerated data types in their own .java files For example: Enumerated Data Types 2/27/20 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 2/27/20 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 constants ordinal < than the arguments ordinal, a positive integer if the calling constants ordinal > than the arguments ordinal, and zero if the calling constants ordinal == the arguments ordinal Enumerated Data Types

2/27/20 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 types values including the correct case Enumerated Data Types

2/27/20 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 2/27/20 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 types value 2/27/20 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 2/27/20 11 Enum Example This represents the entire contents of the file named Coin.java Enumerated Data Types 2/27/20 12

Enum example, continued Enumerated Data Types 2/27/20 13

Recently Viewed Presentations

  • i Welcome to the continuation of Ernies Blog

    i Welcome to the continuation of Ernies Blog

    Welcome to the continuation of Ernie's Blog in a more condensed format for the month of October, 2017.. Table of Contents by Date. The purpose of this blog is to provide a commentary in essays, visual organizers and poems on...
  • Business Intelligence Systems Chapter Preview This chapter surveys

    Business Intelligence Systems Chapter Preview This chapter surveys

    Basic Reporting Operations Reporting tools produce information from data using five basic operations: Sorting Grouping Calculating Filtering Formatting List of Sales Data Data Sorted by Customer Name Sales Data, Sorted by Customer Name and Grouped by Orders and Purchase Amount...
  • Development Committee

    Development Committee

    Bubble Diagram . section based on . Flickr. post. ., (Aug 2009) by . Harrison Architects. Tool architects use during early design. Assists in capturing living dynamics & flow . Helps to drive suitable layout. Fall 2010. Diagrams way of...
  • IES PE Short Course

    IES PE Short Course

    For a particular angle , how are the terms "lagging" and "leading" meaningful with respect to real power? With respect to reactive power? 1.0 power factor condition in terms of Ef, Vt, and delta We have seen that gen lagging...
  • Predicting percentages Percentages are good ways of comparing

    Predicting percentages Percentages are good ways of comparing

    liked bananas: Percentage that like bananas = ???? ×100=(100 ÷??) ×?? = 40 % HINT. If you think more than half have dogs, predict higher than 50%. If you think almost everyone has a dog, predict close to 100%. If...
  • SHSM Gr.10 Information Session Friday, Dec 9, 2016

    SHSM Gr.10 Information Session Friday, Dec 9, 2016

    The College SHSM Construction program is a hands on learning environment where students will learn and understand many aspects of Construction either at Co-op or in the shop. Main focus of the program is on Carpentry, Electrical, Plumbing and Drywall...
  • Public Health and The Prisoner

    Public Health and The Prisoner

    PUBLIC HEALTH AND THE PRISONER Is there a Problem? Peter Sims UPNG Teaching and Learning Tasks Performance Objectives "Write the vision, and make it plain upon tables, that he may run that readeth it" Habbakkuk ch.2 v.2 Peter Sims Professor...
  • Access Project 1 - Brooklyn College

    Access Project 1 - Brooklyn College

    Access Project 1 Creating and Using a Database Objectives Describe databases and database management systems Start Access Describe the features of the Access desktop Create a database Create a table and add records Objectives Close a table Close a database...