1 CSE 331 Enumerated types ( enum ) slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Basic Java Constructs and Data Types – Nuts and Bolts
1 Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure Definitions 10.3Initializing Structures 10.4Accessing.
The ArrayList Class and the enum Keyword
9-Jun-14 Enum s (and a review of switch statements)
Sequence of characters Generalized form Expresses Pattern of strings in a Generalized notation.
Java for C++ Programmers Second Night. Overview First Night –Basics –Classes and Objects Second Night –Enumerations –Exceptions –Input/Output –Templates.
1 Greg Wilson BA 4234 CSC407: Software Architecture Fall 2006 Refactoring.
Engr 691 Special Topics in Engineering Science Software Architecture Spring Semester 2004 Lecture Notes.
Procedural Programming in C# Chapters Objectives You will be able to: Describe the most important data types available in C#. Read numeric values.
New features in JDK 1.5 Can these new and complex features simplify Java development?
1 Todays Objectives Announcements Homework #1 is due next week Return Quiz 1 – answers are posted on the Yahoo discussion page site Basic Java Programming.
Classes and Objects. What is Design? The parts of the software including – what information each part holds – what things each part can do – how the various.
Object Oriented Programming with Java
1. Define the concept of assertions. 1 Explain the use of assertions. 2 Create Java program using assertions. 3 Run Java program using assertions. 4 2.
Intro to CS – Honors I More Objects and Methods GEORGIOS PORTOKALIDIS
1.A computer game is an example of A.system software; B.a compiler; C.application software; D.hardware; E.none of the above. 2.JVM stands for: A.Java Virtual.
Public class ABC { private int information = 0; private char moreInformation = ‘ ‘; public ABC ( int newInfo, char moreNewInfo) { } public ABC () {} public.
Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
Objects, Variables & Methods Java encapsulates data and action modules that access the data in one container, called an object. Object members that.
More methods and classes, 4 of 4 Math 130 Introduction to Programming Lecture # 18 Monday, October 8, 2007.
CS 112 Intro to Computer Science II Sami Rollins Fall 2006.
Java Syntax Primitive data types Operators Control statements.
Lecture 16 CIS 208 Friday March 18 th, Last bit on structs, I swear Structs in Debugger It’s your friend.
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
1 Identifiers  Identifiers are the words a programmer uses in a program  An identifier can be made up of letters, digits, the underscore character (
Announcements  If you need more review of Java…  I have lots of good resources – talk to me  Use “Additional Help” link on webpage  Weekly assignments.
CompSci Data Types & Operations. CompSci The Plan  Overview  Data Types  Operations  Code Samples  ChangeMaker.java  PolarCoordinate.java.
Dale Roberts Object Oriented Programming using Java - Enumerations Dale Roberts, Lecturer Computer Science, IUPUI Department.
Data Types & Operations 1 Last Edited 1/10/04CPS4: Java for Video Games Data Types.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
Introduction to Computer Science 2 Slide 1 Enumerated types We already know some data types int, float, char good for problems that involve int,
Counting Coins. The Basics Quarter 25 cents Dime 10 cents.
Let’s Learn About Money!
This recitation 1 An interesting point about A3: Using previous methods to avoid work in programming and debugging. How much time did you spend writing.
1 Basic Java Constructs and Data Types – Nuts and Bolts Looking into Specific Differences and Enhancements in Java compared to C.
CIS 270—Application Development II Chapter 8—Classes and Objects: A Deeper Look.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Recitation 5 Enums and The Java Collections classes/interfaces 1.
Announcements Final Exam: TBD. Static Variables and Methods static means “in class” methods and variables static variable: one per class (not one per.
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.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
1 CSE 331 Comparing objects; Comparable, compareTo, and Comparator slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R.
Introduction to Enumerations CMSC 202. Enumerated Values Enumerated values are used to represent a set of named values. Historically in Java (and other.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Enumeration Types Managing Ordered Sets. Enumeration types are designed to increase the readability of programs Used to define a set of named constants.
More About Objects and Methods
Compound Condition Break , Continue Switch Statements in Java
Enumerated DATA Types Enum Types Enumerated Data Types
Primitive Types Vs. Reference Types, Strings, Enumerations
Object Based Programming
Starting JavaProgramming
null, true, and false are also reserved.
(and a review of switch statements)
(and a review of switch statements)
More On Enumeration Types
A Different Kind of Variable
More About Objects and Methods
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
CSE 303 Lecture 25 Loose Ends and Random Topics
Enumerated DATA Types Enum Types Enumerated Data Types
Module 2 - Part 1 Variables, Assignment, and Data Types
CSE 142 Lecture Notes Defining New Types of Objects, cont'd.
Module 2 Variables, Data Types and Arithmetic
Agenda Types and identifiers Practice Assignment Keywords in Java
slides created by Ethan Apter and Marty Stepp
CSC 205 Java Programming II
Agenda Warmup Lesson 2.4 (String concatenation, primitive types, etc)
Presentation transcript:

1 CSE 331 Enumerated types ( enum ) slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia

2 Anti-pattern: int constants public class Card { public static final int CLUBS = 0; public static final int DIAMONDS = 1; public static final int HEARTS = 2; public static final int SPADES = 3;... private int suit;... public void setSuit(int suit) { this.suit = suit; } What's wrong with using int constants to represent card suits? variation (also bad): using String s for the same purpose.

3 Enumerated types enum: A type of objects with a fixed set of constant values. public enum Name { VALUE, VALUE,..., VALUE } Usually placed into its own.java file. C has enum s that are really int s; Java's are objects. public enum Suit { CLUBS, DIAMONDS, HEARTS, SPADES } Effective Java Tip #30: Use enum s instead of int constants. "The advantages of enum types over int constants are compelling. Enums are far more readable, safer, and more powerful."

4 What is an enum? The preceding enum is roughly equal to the following short class: public final class Suit extends Enum { public static final Suit CLUBS = new Suit(); public static final Suit DIAMONDS = new Suit(); public static final Suit HEARTS = new Suit(); public static final Suit SPADES = new Suit(); private Suit() {} // no more can be made }

5 What can you do with an enum? use it as the type of a variable, field, parameter, or return public class Card { private Suit suit;... } compare them with == (why don't we need to use equals ?) if (suit == Suit.CLUBS) {... compare them with compareTo (by order of declaration) public int compareTo(Card other) { if (suit != other.suit) { return suit.compareTo(other.suit); }... }

6 The switch statement switch ( boolean test ) { case value : code ; break; case value : code ; break;... default: // if it isn't one of the above values code ; break; } an alternative to the if/else statement must be used on integral types (e.g. int, char, long, enum ) instead of a break, a case can end with a return, or if neither is present, it will "fall through" into the code for the next case

7 Enum methods methoddescription int compareTo( E ) all enum types are Comparable by order of declaration boolean equals( o ) not needed; can just use == String name() equivalent to toString int ordinal() returns an enum's 0-based number by order of declaration (first is 0, then 1, then 2,...) methoddescription static E valueOf( s ) converts a string into an enum value static E [] values() an array of all values of your enumeration

8 EnumSet class EnumSet from java.util represents a set of enum values and has useful methods for manipulating enum s: Set coins = EnumSet.range(Coin.NICKEL, Coin.QUARTER); for (coin c : coins) { System.out.println(c); // see also: EnumMap } Effective Java Tip #32: Use EnumSet instead of bit fields. Effective Java Tip #33: Use EnumMap instead of ordinal indexing. static EnumSet allOf( Type ) a set of all values of the type static EnumSet complementOf ( set ) a set of all enum values other than the ones in the given set static EnumSet noneOf( Type ) an empty set of the given type static EnumSet of(... ) a set holding the given values static EnumSet range( from, to ) set of all enum values declared between from and to

9 More complex enums An enumerated type can have fields, methods, and constructors: public enum Coin { PENNY(1), NICKEL(5), DIME(10), QUARTER(25); private int cents; private Coin(int cents) { this.cents = cents; } public int getCents() { return cents; } public int perDollar() { return 100 / cents; } public String toString() { // "NICKEL (5c)" return super.toString() + " (" + cents + "c)"; }