Enumerated Types Define a new data type and list all possible values: enum Season {winter, spring, summer, fall} Then the new type can be used to declare.

Slides:



Advertisements
Similar presentations
1001ICT Programming 1 Semester 1, 2011 Lecture 6 Using Java Classes (Textbook, Chapter 3, Sections 3.2 to 3.7 ONLY)
Advertisements

Which season do you like best? 1106 Grade 8 Unit 9.
Copyright © 2012 Pearson Education, Inc. Chapter 14: More About Classes.
The Months and The Seasons Prepared by Claudia Doria and Terra Myers.
Seasons Table of Contents:  Winter Winter  Spring Spring  Summer Summer  Fall Fall Kelly Hazzard Grade 1.
Designing Classes How to write classes in a way that they are easily understandable, maintainable and reusable.
Fall 2007CS 2251 Enum Types from Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus.
Birthday Months By: Cynthia Tran Months in a year? January February March April May June July August September October November December.
Design: Coupling and Cohesion How to write classes in a way that they are easily understandable, maintainable and reusable.
Chapter 3 Using Classes and Objects. © 2004 Pearson Addison-Wesley. All rights reserved3-2 Outline Last Time: Creating Objects The GregorianCalendar Class.
Using Classes and Objects Chapters 3 Section 3.3 Packages Section 3.4 Random Class Section 3.5 Math Class Section 3.7 Enumerated Types Instructor: Scott.
COS 312 DAY 3 Tony Gauvin. Ch 1 -2 Agenda Questions? Assignment 1 DUE right now Assignment 2 Posted – Due Feb 5 prior to class Using Classes and Objects.
Chapter 3 Using Classes and Objects. Chapter Scope Creating objects Services of the String class The Java API class library The Random and Math classes.
 January is a winter month. It is the first month of the new year. It is after December and before February. What is the weather like in January?
© 2007 Pearson Addison-Wesley. All rights reserved3-1 Using Classes and Objects We can create more interesting programs using predefined classes and related.
Interfaces. –An interface describes a set of methods: no constructors no instance variables –The interface must be implemented by some class. 646 java.
© 2004 Pearson Addison-Wesley. All rights reserved November 7, 2007 Interfaces ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
Chapter 7 Object-Oriented Design Concepts
Copyright © 2012 Pearson Education, Inc. Chapter 3 Using Classes and Objects Java Software Solutions Foundations of Program Design Seventh Edition John.
Chapter 6 Object-Oriented Design Part 3. © 2004 Pearson Addison-Wesley. All rights reserved6-2 Outline Enumerated Types Revisited Method Design Testing.
Layouts, Types, & Switches Copyright © 2012 Pearson Education, Inc.
WORD JUMBLE. Months of the year Word in jumbled form e r r f b u y a Word in jumbled form e r r f b u y a february Click for the answer Next Question.
WEATHER BY: JENNIFER FAUTH KINDERGARTEN.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William.
Programming in Java (COP 2250) Lecture 8 Chengyong Yang Fall, 2005.
1 Enumerations and Structs Chapter 9. 2 Objectives You will be able to: Write programs that define and use enumeration variables. Write programs that.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
Week101 APCS-AB: Java Miscellaneous Topics: Snippets we missed in Chapters 1-6 of book November 11, 2005.
Chapter 6 Object-Oriented Design Part 2. © 2004 Pearson Addison-Wesley. All rights reserved2/20 The this Reference The this reference allows an object.
1 COS 260 DAY 14 Tony Gauvin. 2 Agenda Questions? 6 th Mini quiz graded  Oct 29 –Chapter 6 Assignment 4 will be posted later Today –First two problems.
Using Classes and Objects (Chapter 3) Copyright © 2012 Pearson Education, Inc.
2011 Calendar Important Dates/Events/Homework. SunSatFriThursWedTuesMon January
The months of the year January February. The months of the year March April.
 2005 Pearson Education, Inc. All rights reserved. 1 Introduction to Classes and Objects.
COS 312 DAY 3 Tony Gauvin. Ch 1 -2 Agenda Questions? Assignment 1 posted – Due Feb 2PM Assignment 2 Posted – Due Feb 2 PM Finish Data and Expressions.
1 CMPT 126 Java Basics Using Classes and Objects.
Outline Anatomy of a Class Encapsulation Anatomy of a Method Graphical Objects Graphical User Interfaces Buttons and Text Fields Copyright © 2012 Pearson.
Seasons. What do we know about seasons? What is a season? How many season do we have? What season are we in right now?
Designing classes How to write classes in a way that they are easily understandable, maintainable and reusable 6.0.
Common Mistakes with Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
© 2004 Pearson Addison-Wesley. All rights reserved January 27, 2006 Formatting Output & Enumerated Types & Wrapper Classes ComS 207: Programming I (in.
New Java Features Advanced Programming Techniques.
& by HERBER.
OOP Tirgul 12.
Interfaces November 6, 2006 ComS 207: Programming I (in Java)
Outline Creating Objects The String Class Packages Formatting Output
Going further Enumerated types Recursion Collections.
Using Classes and Objects (Chapter 3)
Chapter 3: Using Classes and Objects
CMSC 202 Static Methods.
Common Mistakes with Functions
Year 2 Autumn Term Week 12 Lesson 1
Classes & Objects: Examples
The this Reference The this reference allows an object to refer to itself That is, the this reference, used inside a method, refers to the object through.
Classes and Objects 5th Lecture
Chapter 3 Using Classes and Objects
CMSC 202 Interfaces.
SEASONS Khalatyan Nane Artschool The 4th grade.
Year 2 Autumn Term Week 12 Lesson 1
Classes and Objects Static Methods
COP 3330 Notes 4/6.
Calendar.
Take a walk down the canal and see the changes that happen as each month comes and goes. SAMPLE SLIDE Random Slides From This PowerPoint Show
Object-Oriented Design Part 2
What to expect this week
Chapter 3 Using Classes and Objects
Arrivalist Seasonal Maps of Actual Visits
2015 January February March April May June July August September
& by HERBER.
Presentation transcript:

Enumerated Types Define a new data type and list all possible values: enum Season {winter, spring, summer, fall} Then the new type can be used to declare variables Season time; And then the variable can be assigned a value: time = Season.fall; The only values this variable can be assigned are the ones from the enum definition Copyright © 2012 Pearson Education, Inc. Why would we want to use enums?

Copyright © 2012 Pearson Education, Inc. //******************************************************************** // IceCream.java Author: Lewis/Loftus // // Demonstrates the use of enumerated types. //******************************************************************** public class IceCream { enum Flavor {vanilla, chocolate, strawberry, fudgeRipple, coffee, rockyRoad, mintChocolateChip, cookieDough} // // Creates and uses variables of the Flavor type. // public static void main (String[] args) { Flavor cone1, cone2, cone3; cone1 = Flavor.rockyRoad; cone2 = Flavor.chocolate; System.out.println ("cone1 value: " + cone1); System.out.println ("cone1 ordinal: " + cone1.ordinal()); System.out.println ("cone1 name: " + cone1.name()); continued

Copyright © 2012 Pearson Education, Inc. continued System.out.println (); System.out.println ("cone2 value: " + cone2); System.out.println ("cone2 ordinal: " + cone2.ordinal()); System.out.println ("cone2 name: " + cone2.name()); cone3 = cone1; System.out.println (); System.out.println ("cone3 value: " + cone3); System.out.println ("cone3 ordinal: " + cone3.ordinal()); System.out.println ("cone3 name: " + cone3.name()); }

Copyright © 2012 Pearson Education, Inc. continued System.out.println (); System.out.println ("cone2 value: " + cone2); System.out.println ("cone2 ordinal: " + cone2.ordinal()); System.out.println ("cone2 name: " + cone2.name()); cone3 = cone1; System.out.println (); System.out.println ("cone3 value: " + cone3); System.out.println ("cone3 ordinal: " + cone3.ordinal()); System.out.println ("cone3 name: " + cone3.name()); } Output cone1 value: rockyRoad cone1 ordinal: 5 cone1 name: rockyRoad cone2 value: chocolate cone2 ordinal: 1 cone2 name: chocolate cone3 value: rockyRoad cone3 ordinal: 5 cone3 name: rockyRoad

A basic enumerated type public enum CommandWord { // A value for each command word, // plus one for unrecognised commands. GO, QUIT, HELP, UNKNOWN; } Each name represents an object of the enumerated type, e.g. CommandWord.HELP. Enumerated objects are not created directly by the programmer.

Another enumerated type public enum CommandWord { // A value for each command word, // plus one for unrecognised commands. GO(“go”), QUIT(“quit”), HELP(“help”), UNKNOWN(“unknown); private String commandWord; // Initialize the command word CommandWord (String commandword) { this. commandWord = commandWord; } public String toString() { return commandWord; }.

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 See SeasonTester.java Copyright © 2012 Pearson Education, Inc.

//******************************************************************** // Season.java Author: Lewis/Loftus // // Enumerates the values for Season. //******************************************************************** public enum Season { winter ("December through February"), spring ("March through May"), summer ("June through August"), fall ("September through November"); private String span; // // Constructor: Sets up each value with an associated string. // Season (String months) { span = months; } // // Returns the span message for this value. // public String getSpan() { return span; }

Copyright © 2012 Pearson Education, Inc. //******************************************************************** // SeasonTester.java Author: Lewis/Loftus // // Demonstrates the use of a full enumerated type. //******************************************************************** public class SeasonTester { // // Iterates through the values of the Season enumerated type. // public static void main (String[] args) { for (Season time : Season.values()) System.out.println (time + "\t" + time.getSpan()); }

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 can be processed using a for-each loop 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 Copyright © 2012 Pearson Education, Inc.