Enumerations. Fundamentals of Computer Science

Similar documents
Avoiding magic numbers

Mul$- state systems CSCI 255: Introduc/on to Embedded Systems Keith Vertanen Copyright 2011

Other conditional and loop constructs. Fundamentals of Computer Science Keith Vertanen

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

Michele Van Dyne Museum 204B CSCI 136: Fundamentals of Computer Science II, Spring

Lecture 12. Data Types and Strings

Enumerations. Fundamentals of Computer Science

Java Review. Fundamentals of Computer Science

Following is the general form of a typical decision making structure found in most of the programming languages:

switch case Logic Syntax Basics Functionality Rules Nested switch switch case Comp Sci 1570 Introduction to C++

CONDITIONAL EXECUTION

Cellular Automata Language (CAL) Language Reference Manual

Recursion. Fundamentals of Computer Science

CONDITIONAL EXECUTION: PART 2

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable?

Flow of Control: Loops. Chapter 4

Programming with Java

Abstract Classes Interfaces CSCI 201 Principles of Software Development

Abstract Classes Interfaces CSCI 201 Principles of Software Development

BASICS.

Chapter 4 Defining Classes I

ASSIGNMENT 3 Classes, Objects and the Robot World

Introduction to Computer Science Unit 2. Exercises

CS106X Handout 06 Autumn 2012 September 26 th, 2012 Grids and Queen Safety

Data Types primitive, arrays, objects Java overview Primitive data types in Java

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 8: SEP. 29TH INSTRUCTOR: JIAYIN WANG

Dynamic Arrays. Fundamentals of Computer Science

Computer Science 136 Assignment 2 Mazes Due: Monday, at noon.

2.8. Decision Making: Equality and Relational Operators

Introduction to C++ Introduction. Structure of a C++ Program. Structure of a C++ Program. C++ widely-used general-purpose programming language

EXCEPTIONS. Fundamentals of Computer Science I

Lab # 2. For today s lab:

5/3/2006. Today! HelloWorld in BlueJ. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont.

Introduction to C++ with content from

(A) 99 (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

Direct Input Interface Library (DIIL) Version 2.0

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

Threads. Fundamentals of Computer Science

AP Computer Science Unit 1. Programs

Question: Total Points: Score:

Java Loop Control. Programming languages provide various control structures that allow for more complicated execution paths.

Part 3: GridWorld Classes and Interfaces

CS 211: Potpourri Enums, Packages, Unit Tests, Multi-dimensional Arrays Command Line Args

About this exam review

Grammars and Parsing, second week

Fall 2017 CISC124 9/16/2017

Topic 7: Algebraic Data Types

Select the one correct answer.

Java Programming Fundamentals - Day Instructor: Jason Yoon Website:

Algoritma dan Struktur Data Leon Andretti Abdillah. 08 Control Flow Statements Selection by Using Switch and Case

PROGRAMMING FUNDAMENTALS

Timing for Interfaces and Abstract Classes

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

Computational Expression

The Drunken Sailor s Challenge

CSCI 135 Exam #1 Fundamentals of Computer Science I Fall 2013

Lecture 6. Instructor: Craig Duckett

Loops. CSE 114, Computer Science 1 Stony Brook University

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

1. Find the output of following java program. class MainClass { public static void main (String arg[])

(A) 99 ** (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

CS 302 Week 9. Jim Williams

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG

Wrapper Classes double pi = new Double(3.14); 3 double pi = new Double("3.14"); 4... Zheng-Liang Lu Java Programming 290 / 321

Building Java Programs. Chapter 2: Primitive Data and Definite Loops

SUN Certified Programmer for J2SE 5.0 Upgrade. Download Full Version :

2.2 - Making Decisions

CIS133J. Working with Numbers in Java

AP Computer Science A Unit 2. Exercises

M e t h o d s a n d P a r a m e t e r s

PROGRAMMING STYLE. Fundamentals of Computer Science I

CSCI 135 Exam #1 Fundamentals of Computer Science I Fall 2014

CSCI 1103: Introduction

Lecture 14 CSE11 Fall 2013 For loops, Do While, Break, Continue

BASIC ELEMENTS OF A COMPUTER PROGRAM

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 9: OCT. 4TH INSTRUCTOR: JIAYIN WANG

COMP 110/L Lecture 6. Kyle Dewey

Adding Existing Source Code in NetBeans CS288, Autumn 2005 Lab 002

Assignment #2 Answers

Pace University. Fundamental Concepts of CS121 1

ASSIGNMENT 4 Records and Objects

Last Time. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

A foundation for programming. Classes and objects. Overview. Java primitive types. Primitive types Creating your own data types

CS 251 Intermediate Programming Coding Standards

CSCI 135 Exam #1 Fundamentals of Computer Science I Fall 2012

6.005 Elements of Software Construction

CS106A, Stanford Handout #18. Writing a Class

Concurrency. Fundamentals of Computer Science

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

Enumerated Types. CSE 114, Computer Science 1 Stony Brook University

Building Java Programs

Assignment Definition And General Feedback By Michael Panitz at Cascadia Community College (

Java: Classes. An instance of a class is an object based on the class. Creation of an instance from a class is called instantiation.

COMP 202 Java in one week

ARRAY & FUNCTIONS ARRAY. Warm Up Exercises for Intro to Computational Programming in C. Engineering For Kids!

Topic 5: Enumerated Types and Switch Statements

Exercise (Revisited)

AP Programming GridWorld Lecture page 1 of 10

Building Java Programs

INFO Object-Oriented Programming

Transcription:

Enumerations Fundamentals of Computer Science

Outline Avoiding magic numbers Variables takes on a small set of values Use descriptive names instead of literal values Java enumerations Using in a switch statement

Variables from a Set of Values Magic numbers Where did the value come from? What does it mean? What if you mistype the number? What if you want to keep value in specific range? int direction = 0;... if ((direction == 1) (direction == 3) (direction == 5) (direction == 7)) { /* TBD */ } direction = 0; direction = 8; direction = -2729;

Solution 1: Create final constants Descriptive names means everybody can read Bugs less likely, typo in name = compile error Keyword final ensures nobody can change value final int NORTH = 0; final int NORTHEAST = 1; final int EAST = 2; final int SOUTHEAST = 3; final int SOUTH = 4; final int SOUTHWEST = 5; final int WEST = 6; final int NORTHWEST = 7; int direction = NORTH;... Variables from a Set of Values if ((direction == NORTHEAST) (direction == SOUTHEAST) (direction == SOUTHWEST) (direction == NORTHWEST)) { // TBD }

Constants not Always Ideal final int NORTH = 0; final int NORTHEAST = 1; final int EAST = 2; final int SOUTHEAST = 3; final int SOUTH = 4; final int SOUTHWEST = 5; final int WEST = 6; final int NORTHWEST = 7; Problem 1: Tedious to type. Also easy to mess up, e.g. setting two constants to same value. int direction = 0;... if ((direction == NORTHEAST) (direction == SOUTHEAST) (direction == SOUTHWEST) (direction == NORTHWEST)) {/* TBD */} direction = 0; direction = 8; direction = -2729; Problem 2: Not forced to use the friendly names. Problem 3: Not forced to stay in range. What does it mean to be 8 or -2729 if you are a compass direction?

Enumerations A better solution: enumerations Specifies exact set of friendly names Compiler ensures we stay in range Easiest to declare outside class. Semicolon is optional. public enum Compass {NORTH, NORTHEAST, EAST, SOUTHEAST, SOUTH, SOUTHWEST, WEST, NORTHWEST} public class CompassTest { public static void main(string [] args) { Compass direction = Compass.NORTH; if ((direction == Compass.NORTHEAST) (direction == Compass.SOUTHEAST) (direction == Compass.SOUTHWEST) (direction == Compass.NORTHWEST)) {/* TBD */} } } direction = 0; Now a compile error. Way to watch our back compiler!

Enumerations Enumeration Tricks Actually objects with a few handy methods: tostring() values() Print out friendly name corresponding to value of variable Returns array of all the possible values type can take on public enum Compass {NORTH, NORTHEAST, EAST, SOUTHEAST, SOUTH, SOUTHWEST, WEST, NORTHWEST}... for-each loop, goes over all values of the enumeration for (Compass d : direction.values()) { if (checkmonster(hero, d)) System.out.println("You see a monster to the " + d.tostring()); }

switch Statement Compass direction = Compass.NORTH; switch (direction) Note: normally you need { "Compass.", but not in switch case NORTH: case since Java knows type hero.move(0, 1); System.out.println("Walking north"); break; case SOUTH: hero.move(0, -1); System.out.println("Walking south"); break; case EAST: hero.move(1, 0); System.out.println("Walking east"); break; case WEST: hero.move(-1, 0); System.out.println("Walking west"); break; } You can have as many statements as you want between case and break.

Summary Avoiding magic numbers Variables takes on a small set of values Use descriptive names instead of literal values Java enumerations Using in a switch statement

Hands On Exercise Open Moodle, go to CSCI 136, Section 11 Open the dropbox for today In-Class Exercise 4 Drag and drop your program files to the Moodle dropbox You get: 1 point if you turn in something, 2 points if you turn in something that is correct. Create an enumeration of possible letter grades named Grade.java Create a GradeTest.java program that creates a variable of type Grade and assigns a grade, then use a switch statement to print out an appropriate message. Your switch statement should cover any possible letter grade in case I test it with something other than what you assigned in your program.