More On Enumeration Types

Slides:



Advertisements
Similar presentations
Arrays.
Advertisements

Based on slides from Deitel & Associates, Inc.
Chapter 7: User-Defined Simple Data Types, Namespaces, and the string Type.
Objectives In this chapter, you will:
Chapter 5 C Functions The best way to develop and maintain a large program is to divide it into several smaller program modules, each of which is more.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 17 Exceptions and.
Fall 2007CS 2251 Enum Types from Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus.
Scope Rules and Storage Types CS-2303, C-Term Scope Rules and Storage Types CS-2303, System Programming Concepts (Slides include materials from The.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Object-Oriented Programming (OOP). Implementing an OOD in Java Each class is stored in a separate file. All files must be stored in the same package.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Dale Roberts Object Oriented Programming using Java - Enumerations Dale Roberts, Lecturer Computer Science, IUPUI Department.
 2005 Pearson Education, Inc. All rights reserved Classes and Objects: A Deeper Look.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
Android How to Program, 2/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
 Pearson Education, Inc. All rights reserved Classes and Objects: A Deeper Look.
Dale Roberts Object Oriented Programming using Java - Final and Static Keywords Dale Roberts, Lecturer Computer Science, IUPUI
 2005 Pearson Education, Inc. All rights reserved Classes and Objects: A Deeper Look.
CIS 270—Application Development II Chapter 8—Classes and Objects: A Deeper Look.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
1 Exceptions. 2 Syntax Errors, Runtime Errors, and Logic Errors syntax errors, runtime errors, and logic errors You learned that there are three categories.
Jozef Goetz Credits: Copyright  Pearson Education, Inc. All rights reserved. expanded by J. Goetz, 2016.
Part III © Copyright by Pearson Education, Inc. All Rights Reserved.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
C# Fundamentals An Introduction. Before we begin How to get started writing C# – Quick tour of the dev. Environment – The current C# version is 5.0 –
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
SCJP 5, 1/7 Declarations, Initialization and Scoping
Information and Computer Sciences University of Hawaii, Manoa
Objectives In this chapter, you will:
Objectives In this chapter, you will:
Enumeration Type Data type: a set of values with a set of operations on them Enumeration type: a simple data type created by the programmer To define an.
Examples of Classes & Objects
Principles of programming languages 8: Types
Some Eclipse shortcuts
Java Primer 1: Types, Classes and Operators
Programming Fundamentals Lecture #7 Functions
Chapter 8: Control Structures
11/10/2018.
Object Based Programming
Arrays, For loop While loop Do while loop
Class Structure 16-Nov-18.
Object-Oriented Programming: Polymorphism
Interface.
Chapter 6 Methods: A Deeper Look
Class Structure 28-Nov-18.
Classes & Objects: Examples
Unit-1 Introduction to Java
Variables Numbers can be stored and retrieved while a program is running if they are given a home. The way that integers and decimal numbers are stored.
Class Structure 7-Dec-18.
Introduction To Programming Information Technology , 1’st Semester
Class Structure 2-Jan-19.
Chapter 6 Control Statements: Part 2
Scope Rules and Storage Types
Review for Final Exam.
Class Structure 25-Feb-19.
CS360 Client/Server Programming Using Java
Abstract Classes and Interfaces
Java Programming Language
Objectives In this chapter, you will:
Introduction to Programming
Standard Version of Starting Out with C++, 4th Edition
Programming Languages and Paradigms
Corresponds with Chapter 5
Classes Member Qualifiers
Methods Scope How are names handled?
Introduction to Classes and Objects
Presentation transcript:

More On Enumeration Types Lecture 10

What are enum types? enum types are reference types an enum type is declared with an enum declaration an enum declaration is a comma-separated list of enum constants an enum declaration may include other components of tradational classes such as constructors fields methods Deitel and Deitel, Java - How to Program,7th Edition, page 288 Show Craps2.java

Restrictions on enum types enum types are implicitly final, because they declare constants that should not be modified enum constants are implicitly static any attempt to create an object of an enum type with operator new results in a compilation error Deitel and Deitel, Java - How to Program,7th Edition, page 288

What can you do with them? “You can use them anywhere constants can be used such as in the case labels for switch statements and to control enhanced for statements”

For Statement Review For statement known as a “counted” loop use when you know how many times you want to loop loop control variable can be declared inside loop in which case its scope is the loop and it is not known outside the loop. It is local to the loop. for (int i = 0; i < 10; i++) loop control variable can be declared outside loop which which case its scope is the block in which it is declared. for (i = 0; i < 10; i++) Show TestInsideForLoop TestOutsideForLoop

For Statement Review, continued Java allows you to do “evil” things with a for loop. However, you, the programmer, should not do any of them. Here are the rules to follow: programmer should not assign values to the loop control variable within the loop programmer should not alter the value of the terminating value of the loop Show TestEvilForLoop

Enhanced For Statement Syntax For (parameter : arrayName) statement; The type of the parameter must be consistent with the type of the elements in the array (i.e. The type specified in the parameter is whatever type of value is stored in the array). The loop selects one value from the array during each iteration. The enhanced for statement iterates through successive values in the array, one by one. Deitel and Deitel, Java - How to Program,7th Edition, page 312 Show TestEnhancedFor and TestBadEnhancedFor

EnumSet range public static <E extends Enum<E>> EnumSet<E> range(E from, E to) Creates an enum set initially containing all of the elements in the range defined by the two specified endpoints. The returned set will contain the endpoints themselves, which may be identical but must not be out of order. Parameters: from - the first element in the range to - the last element in the range Returns: an enum set initially containing all of the elements in the range defined by the two specified endpoints Throws: NullPointerException - if first or last are null IllegalArgumentException - if first.compareTo(last) > 0 from API Show DeitelBook and DeitelBookDriver