ENUMERATED TYPES Java Basics. Enumerated Type Topics Why use them? Defining an enumerated type Using an enumerated type Advanced features.

Slides:



Advertisements
Similar presentations
The ArrayList Class and the enum Keyword
Advertisements

Picture It Very Basic Game Picture Pepper. Original Game import java.util.Scanner; public class Game { public static void main() { Scanner scan=new Scanner(System.in);
1 Chapter 11 Introducing the Class Pages ( )
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.
More methods and classes, 4 of 4 Math 130 Introduction to Programming Lecture # 18 Monday, October 8, 2007.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 9 GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology/George Koutsogiannakis 1.
CSCI S-1 Section 5. Deadlines for Problem Set 3 Part A – Friday, July 10, 17:00 EST Parts B – Tuesday, July 14, 17:00 EST Getting the code examples from.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 8: Classes and Objects.
Dialogs. Displaying Text in a Dialog Box Windows and dialog boxes –Up to this our output has been to the screen –Many Java applications use these to display.
Java: How to Program Methods Summary Yingcai Xiao.
Java Intro. Strings “This is a string.” –Enclosed in double quotes “This is “ + “another “ + “string” –+ concatenates strings “This is “ “ string”
Constants. 2 Objectives Describe ways to create constants –const –readonly –enum.
28-Jun-15 Access to Names Namespaces, Scopes, Access privileges.
COMP 110 Errors, Strings, and Review Tabitha Peck M.S. January 28, 2008 MWF 3-3:50 pm Philips
Chapter 7: User-Defined Methods
JAVA Control Statement.
COMP More About Classes Yi Hong May 22, 2015.
GETTING INPUT Simple I/O. Simple Input Scanner scan = new Scanner(System.in); System.out.println("Enter your name"); String name = scan.nextLine(); System.out.println("Enter.
CS 241 – Computer Programming II Lab Kalpa Gunaratna –
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.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Comp 248 Introduction to Programming Chapter 6 Arrays Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
ACO 101: Introduction to Computer Science Anatomy Part 2: Methods.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & Nullable type.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 11 Structured Data.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
Java Programming: Program Design Including Data Structures 1 Vectors The class Vector can be used to implement a list Unlike an array, the size of a Vector.
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
CSC1401 Strings (text). Learning Goals Working with Strings as a data type (a class) Input and output of Strings String operations.
CONTENTS Wrapper Class Enumeration Garbage Collection Import static.
CS305j Introduction to Computing Classes II 1 Topic 24 Classes Part II "Object-oriented programming as it emerged in Simula 67 allows software structure.
Announcements Quiz 1 Next Monday. int : Integer Range of Typically –2,147,483,648 to 2,147,483,647 (machine and compiler dependent) float : Real Number.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
Enum public enum Week {Sunday, Moday, Tuesday, Wednesday, Thursday, Friday, Saturday} Week.Sunday.ordinal()  0 Week.Sunday  “Sunday”
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.
Methods.
Enum,Structure and Nullable Types Ashima Wadhwa. Enumerations, Enumerations, or enums, are used to group named constants similar to how they are used.
Topics Instance variables, set and get methods Encapsulation
AOP with AspectJ Awais Rashid, Steffen Zschaler © Awais Rashid, Steffen Zschaler 2009.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
Class Everything in Java is in a class. The class has a constructor that creates the object. If you do not supply a constructor Java will create a default.
Pointers A pointer type variable holds the address of a data object or a function. A pointer can refer to an object of any one data type; it cannot refer.
Session 2 Operators, Decisions and Loops. Objectives Operators Casting data Decision marking structures Loops break, continue, return.
SUMMARY OF CHAPTER 2: JAVA FUNDAMENTS STARTING OUT WITH JAVA: OBJECTS Parts of a Java Program.
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
PART OF SPEECH DEFINITION SYNONYM SENTENCE NAME TEACHER AND DATE1 WORDS OF THE WEEK.
PART OF SPEECH DEFINITION SYNONYM SENTENCE NAME TEACHER AND DATE1 WORDS OF THE WEEK.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Methods Matthew Harrison. Overview ● There are five main aspects of methods... ● 1) Modifiers – public, private ● 2) Method Name ● 3) Parameters ● 4)
Topic: Classes and Objects
Lecture 7 D&D Chapter 7 & 8 Composite Classes and enums Date.
Sixth Lecture ArrayList Abstract Class and Interface
Java Programming: From Problem Analysis to Program Design, 3e Chapter 7 User-Defined Methods.
CSC1401 Input and Output (and we’ll do a bit more on class creation)
Examples of Classes & Objects
Interface.
Building Java Programs
Multiple if-else boolean Data
Something about Java Introduction to Problem Solving and Programming 1.
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Introduction to Classes and Methods
Classes & Objects: Examples
Multiple if-else boolean Data
References and Objects
class PrintOnetoTen { public static void main(String args[]) {
© A+ Computer Science - OOP Pieces © A+ Computer Science -
Lecture Notes - Week 2 Lecture-1. Lecture Notes - Week 2 Lecture-1.
Classes, Objects and Methods
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

ENUMERATED TYPES Java Basics

Enumerated Type Topics Why use them? Defining an enumerated type Using an enumerated type Advanced features

Before Enumerated Types public class Appointment { private int whichDay; private int scheduleLocation; public void scheduleAppointment() { System.out.println("Enter 1 for Monday, 2 for Tuesday or 3 for Thursday: "); Scanner scan = new Scanner(System.in); whichDay = scan.nextInt(); System.out.println("Enter 1 for Golden, 2 for Boulder or 3 for Lafayette: "); scheduleLocation = scan.nextInt(); } public void remindTuesdayPatients() { if (whichDay == 2) System.out.println("Remember your appointment!"); } public static void main(String[] args) { Appointment appointment = new Appointment(); appointment.scheduleAppointment(); appointment.remindTuesdayPatients(); }

Enumerated Types - defining An enum type is a type whose fields consist of a fixed set of constants. Define the type: public enum FilingStatus {SINGLE,MARRIED}; Declare a variable of this type: FilingStatus status; typically public, so can access outside this class keyword to indicate this is enumerated type name of the enum Similar to class, this is a new data type! fixed set of possible values – in ALL_CAPS because they are like constants. In braces – similar to class definition, but with special syntax for body. enum name – it’s a type! name of variable

Enumerated Types - using To specify a value, must include the name of the enumerated type if (status == FilingStatus.SINGLE)... Can use in another class, if specify class name in which enum is declared: if (status == TaxReturn.FilingStatus.SINGLE)... An enumerated type variable can be null variable of type FilingStatus without this, SINGLE would not be recognized (would look like a regular constant) must be one of the options for FilingStatus

Enumerated Type - Example public class Invoice { public enum Status { PAID, UNPAID, WRITE_OFF }; Status status; double balance; public Invoice(double balance) { this.balance = balance; status = Status.UNPAID; } public void updateStatus(Status status) { this.status = status; } public String toString(){ return "Invoice balance: " + balance + " status " + status; }

Example continued public class InvoiceTracker { ArrayList invoices; public void addInvoices() { invoices = new ArrayList (); Invoice inv = new Invoice(300); inv.updateStatus(Invoice.Status.PAID); invoices.add(inv); inv = new Invoice(650); inv.updateStatus(Invoice.Status.WRITE_OFF); invoices.add(inv); } public void displayInvoices() { for (Invoice inv : invoices) System.out.println(inv); } public static void main(String[] args) { InvoiceTracker track = new InvoiceTracker(); track.addInvoices(); track.displayInvoices(); }

Another Example Enumerated types can be defined in their own file Enumerated types can define methods, useful for converting to String, etc. public enum DayOfWeek { MONDAY ("Monday"), TUESDAY ("Tuesday"), WEDNESDAY ("Wednesday"), THURSDAY ("Thursday"), FRIDAY ("Friday"); private String value; DayOfWeek (String aValue) { value = aValue; } public String toString() { return value; }

Revised Appointment public class Appointment { //private int whichDay; private DayOfWeek whichDay; private int scheduleLocation; public void scheduleAppointment() { System.out.println("Enter day of week: "); Scanner scan = new Scanner(System.in); String day = scan.next(); whichDay = DayOfWeek.valueOf(day.toUpperCase()); } public void remindTuesdayPatients() { if (whichDay == DayOfWeek.TUESDAY) System.out.println("Remember your appointment!"); } public static void main(String[] args) { Appointment appointment = new Appointment(); appointment.scheduleAppointment(); appointment.remindTuesdayPatients(); }