Enumerated DATA Types Enum Types Enumerated Data Types

Slides:



Advertisements
Similar presentations
9-Jun-14 Enum s (and a review of switch statements)
Advertisements

1 CSE 331 Enumerated types ( enum ) slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
More methods and classes, 4 of 4 Math 130 Introduction to Programming Lecture # 18 Monday, October 8, 2007.
Fall 2007CS 2251 Enum Types from Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus.
Comp 248 Introduction to Programming Chapter 6 Arrays Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
Name the United States Coins Count the Pennies 10 ¢
Programming in Java (COP 2250) Lecture 8 Chengyong Yang Fall, 2005.
ENUMERATED DATATYPES. USER DEFINED DATA TYPES  Data Type Defined By Programmer  Allows Use Of More Complex Data  Typically Defined Globally So Variables.
CONTENTS Wrapper Class Enumeration Garbage Collection Import static.
1 Enums (Chapter 4) To enumerate is: to name things one after another in a list Java has a type, called an enum, where a programmer specifies a finite.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Enumeration Types Managing Ordered Sets. Enumeration types are designed to increase the readability of programs Used to define a set of named constants.
Chapter 9 A Second Look at Classes and Objects - 4.
SCJP 5, 1/7 Declarations, Initialization and Scoping
More About Objects and Methods
11 Chapter Structured Data
Type Checking Generalizes the concept of operands and operators to include subprograms and assignments Type checking is the activity of ensuring that the.
Introduction to programming in java
Topics Static Class Members Overloaded Methods Overloaded Constructors
Days of the week NEXT.
Data Types & Operations
Java Review: Reference Types
Days of the Week Les docs d’Estelle –
Enumerated DATA Types Enum Types Enumerated Data Types
DAYS OF THE WEEK.
Chapter 9: A Second Look at Classes and Objects
Time management School of Rock.
Namespaces, Typedefs & Enums
A Class Calendar in PowerPoint
JANUARY 2018 SUNDAY MONDAY TUESDAY WEDNESDAY THURSDAY FRIDAY SATURDAY
Sunday Monday Tuesday Wednesday Sunday Monday Tuesday Wednesday
(and a review of switch statements)
(and a review of switch statements)
Enumerations CS Fall 1999 Enumerations.
Chapter 11: Structured Data.
Computer Science Review
We’re slowly working our way towards classes and objects
Pointers C#, pointers can only be declared to hold the memory addresses of value types int i = 5; int *p; p = &i; *p = 10; // changes the value of i to.
A Different Kind of Variable
Name the United States Coins
More About Objects and Methods
MY FUNDAY.
Section 2.1 Basic Set Concepts
CMSC 202 Interfaces.
CHAPTER 2 Set Theory.
Days of the Week Monday Tuesday Wednesday Friday Thursday Saturday
Unit 1: Quick Quizzes After 5,13
CS 240 – Lecture 7 Boolean Operations, Increment and Decrement Operators, Constant Types, enum Types, Precedence.
SEPTEMBER 2014 Unit 1 Unit 1 Unit 1 Unit 1 Quick Quiz 9,16, 21 Unit 1
CHAPTER 2 Set Theory.
SEPTEMBER ½ Day Unit PLC
JANUARY 2018 SUNDAY MONDAY TUESDAY WEDNESDAY THURSDAY FRIDAY SATURDAY
C++ Introduction - 3.
January 2015 Sunday Monday Tuesday Wednesday Thursday Friday Saturday
CHAPTER 2 Set Theory.
| January Sunday Monday Tuesday Wednesday Thursday Friday
Arrays.
Standard Version of Starting Out with C++, 4th Edition
Time 1.
2.1 Basic Set Concepts.
Contact
2011年 5月 2011年 6月 2011年 7月 2011年 8月 Sunday Monday Tuesday Wednesday
§2.1 Basic Set Concepts.
Arrays, Casting & User Defined Variables

CHAPTER 2 Set Theory.
January Monday Tuesday Wednesday Thursday Friday Saturday Sunday 30 31
Lecture 7: Types (Revised based on the Tucker’s slides) 10/4/2019
Interfaces, Enumerations, Boxing, and Unboxing
Presentation transcript:

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

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

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

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

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

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

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

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 February 18, 2019

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

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

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

Enum example, continued Enumerated Data Types February 18, 2019