A Different Kind of Variable

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

08/2012Tanya Mishra1 EASYC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817.
Elementary Data Types Prof. Alamdeep Singh. Scalar Data Types Scalar data types represent a single object, i.e. only one value can be derived. In general,
Chapter 2 Review Questions
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
Declaring Variables You must first declare a variable before you can use it! Declaring involves: – Establishing the variable’s spot in memory – Specifying.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Elementary Data Types Scalar Data Types Numerical Data Types Other
Chapter 2: Introduction to C++.
String Escape Sequences
Section 2 - More Basics. The char Data Type Data type of a single character Example char letter; letter = 'C';
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
CSC204 – Programming I Lecture 4 August 28, 2002.
COMP 110: Introduction to Programming Tyler Johnson Feb 2, 2009 MWF 11:00AM-12:15PM Sitterson 014.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Variables and Functions. Open your Encoder program Let’s begin by opening the “Labyrinth Auto Straight” code. Save this file as Labyrinth with variables.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 4 Making Decisions.
Introduction to Java Java Translation Program Structure
Copyright Curt Hill Variables What are they? Why do we need them?
Copyright Curt Hill The C/C++ switch Statement A multi-path decision statement.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Copyright © Curt Hill The Compound Statement C-Family Languages and Scope.
Copyright © Curt Hill The C++ IF Statement The most important decision statement Part 1.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Copyright © Curt Hill The C++ IF Statement More important details More fun Part 3.
Chapter 9 A Second Look at Classes and Objects - 4.
The for Statement A most versatile loop
Copyright © Texas Education Agency, Computer Programming Variables and Data Types.
C++ LANGUAGE MULTIPLE CHOICE QUESTION
Java Language Basics.
Flow of Control An Overview
Data Types and Expressions
Computing and Statistical Data Analysis Lecture 2
Side Effect Operators Changing the value of variables
Programming Fundamentals
More important details More fun Part 3
EGR 2261 Unit 4 Control Structures I: Selection
Data Types, Identifiers, and Expressions
Variables and Primative Types
Enumerated DATA Types Enum Types Enumerated Data Types
Primitive Types Vs. Reference Types, Strings, Enumerations
2.1 Parts of a C++ Program.
Constants in Java Why and How Copyright © Curt Hill.
Selection CSCE 121 J. Michael Moore.
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
Unit-1 Introduction to Java
The most important decision statement
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.
Java Classes and Objects 3rd Lecture
CMSC 202 Java Primer 2.
Compound Statements A Quick Overview
Topics 4.1 Relational Operators 4.2 The if Statement
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
Enumerated DATA Types Enum Types Enumerated Data Types
Chapter 2: Introduction to C++.
The Java switch Statement
CS 240 – Lecture 7 Boolean Operations, Increment and Decrement Operators, Constant Types, enum Types, Precedence.
The Data Element.
Primitive Types and Expressions
Classes, Objects and Methods
The Data Element.
CprE 185: Intro to Problem Solving (using C)
Variables in C Topics Naming Variables Declaring Variables
Announcements Program 1 due noon Lab 1 due noon
Variables and Constants
Arrays.
Methods Coding in Java Copyright © Curt Hill.
Presentation transcript:

A Different Kind of Variable Enumerations A Different Kind of Variable Enumerations appear in Java first in Java 5 They are unlike C++ because they are a class. Copyright © 2005-2016 Curt Hill

Conventions There are many times when you use a code that represents a value Many times there are existing conventions for a code and its meaning For example months When we say that today is 3/20 you know that the 3 represents March An enumeration allows us to name these in a way that makes the program more readable Copyright © 2005-2016 Curt Hill

The Problem More often there are no existing conventions Programmer makes up the convention What kind of convention? Choices: Strings Chars Ints Copyright © 2005-2016 Curt Hill

Example Consider classifying students such as freshmen, sophomores, juniors, seniors, graduate, prospects, extension We could create a code to represent these in a file that described our students We would not want to type in the entire classification We could misspell it and cause our extraction of juniors not to work Takes too much space Copyright © 2005-2016 Curt Hill

Example continued We will usually assign a letter, unless there are too many or they start with same letter Sophomore and senior Then we use a number So 1=freshman, 2=sophomore ... The problem with that is we will see in our code statements like: s = 6 The reader then wonders what 6 represents Copyright © 2005-2016 Curt Hill

One Solution One approach to solve this is to use named constants: final int freshman=1,sophomore=2…; Another solution to this problem is to use an enumeration type Copyright © 2005-2016 Curt Hill

Enumeration Type Enumerations are a shortened way to make a series of named constants In this type we make up a series of names that we will call our items as a new type enum student_class {freshman, sophomore, junior, senior, grad, prospect, extension}; What we are saying is that we want a new type that has precisely 7 values We have not yet defined a variable Copyright © 2005-2016 Curt Hill

Code Now we can use these in sample code enum Directions {LEFT, FORWARD, RIGHT,BACKWARD}; … Directions dir = Directions.LEFT; Directions is now a type It is usually placed with constants Copyright © 2005-2016 Curt Hill

C++ and Java In C++ an enumeration is a series of named constants In Java it is actually a special form of a class This class does have some methods defined These methods need: import java.lang.Enum; Copyright © 2005-2016 Curt Hill

Enumeration Methods The method ordinal gives the ordinal position Starts at zero dir.ordinal() == 0 indicates that this is LEFT The toString method gives the string representation valueOf takes a string and gives the enumeration value It must be an exact match otherwise IllegalArgumentException is thrown Copyright © 2005-2016 Curt Hill

Comparisons equals gives an equality comparison However the == is preferable compareTo gives -1 if less, 0 if equal and +1 if greater The other comparison operators are not allowed Copyright © 2005-2016 Curt Hill

Representation of value Although technically it is supposed to be hidden from us Practically we do know it Each value in the list is stored as an integer First item is zero Count up from there Copyright © 2005-2016 Curt Hill

Booleans Booleans are a predefined enumeration with special privileges Two values are {false, true} The special privileges include the being result type of all comparisons The enum reserved word is not needed Copyright © 2005-2016 Curt Hill

Declaration note Enumerations are always declared enclosed in braces The Java convention is that like constants they are usually all in upper case This is just a convention Note that bools use lower case for their constants Copyright © 2005-2016 Curt Hill

Constants When using an enumeration constant, it must be prefixed by the enumeration type Directions dir = Directions.LEFT; The exception to this rule is the case constant in a switch: switch(dir){ case LEFT: Copyright © 2005-2016 Curt Hill

Finally Enumerations are very handy for naming constants that are related They tend to make use of such constants more readable They are relatively new feature From Java 5.0 enum was a reserved word from Java 1.0 but not used Copyright © 2005-2016 Curt Hill