CS 2430 Day 26. Announcements Exam #2: Wednesday, April 3 –Review in lab on Tuesday, April 2 –Sample problems sent via email.

Slides:



Advertisements
Similar presentations
Queues Printer queues Several jobs submitted to printer Jobs form a queue Jobs processed in same order as they were received.
Advertisements

Why not just use Arrays? Java ArrayLists.
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Java Review Interface, Casting, Generics, Iterator.
1 ADT and Data Structure Example Generics / Parameterized Classes Using a Set Implementing a Set with an Array Example: SetADT interface Example: ArraySet.
1 Queues – Chapter 3 A queue is a data structure in which all additions are made at one end called the rear of the queue and all deletions are made from.
What is a Queue? n Logical (or ADT) level: A queue is an ordered group of homogeneous items (elements), in which new elements are added at one end (the.
Queues CS 3358 – Data Structures. What is a queue? It is an ordered group of homogeneous items of elements. Queues have two ends: – Elements are added.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
Lab 1: 1. Download all my programs in your computer under the same folder. 2. The tree shown in the following figure represents an expression: (((( 3 +
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
Elementary Data Structures CS 110: Data Structures and Algorithms First Semester,
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Queue Overview Queue ADT Basic operations of queue
5-May-15 ArrayLists. 2 ArrayList s and arrays A ArrayList is like an array of Object s Differences between arrays and ArrayList s: Arrays have special.
CS 2430 Day 14. Agenda for today The Bag class No mixing! What if we have a bunch of String s that we want to store in our program? We can’t use the.
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
Tirgul 3 Subjects of this Tirgul: Linked Lists Doubly-Linked Lists Sparse Matrices Stack Queue.
Lecture 27 Exam outline Boxing of primitive types in Java 1.5 Generic types in Java 1.5.
1 Generics, Type Safety, and Dynamic Data Structures.
CPSC150 Inheritance Details Chapter 9. CPSC150 Print in Entertainment ver 2 (with inheritance): public void print() { System.out.print("title: " + title.
Unit 11 1 Unit 11: Data Structures H We explore some simple techniques for organizing and managing information H This unit focuses on: Abstract Data Types.
1 Lecture 24 Abstract Data Types (ADT) –I Overview  What is an Abstract Data type?  What is Stack ADT?  Stack ADT Specifications  Array Implementation.
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 13: Queues and Vectors.
Building Java Programs Inner classes, generics, abstract classes reading: 9.6, 15.4,
1 Lecture 26 Abstract Data Types – IV Overview  The List ADT  Implementing Stacks as Linked List  Linked List Implementation of Queues .  Preview:
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation – FIFO As items are added they are chronologically ordered, items are removed.
15-Jul-15 Generics. ArrayList s and arrays A ArrayList is like an array of Object s, but... Arrays use [ ] syntax; ArrayList s use object syntax An ArrayList.
P Object type and wrapper classes p Object methods p Generic classes p Interfaces and iterators Generic Programming Data Structures and Other Objects Using.
Effective Java: Generics Last Updated: Spring 2009.
ITI Introduction to Computing II Lab-8 Dewan Tanvir Ahmed University of Ottawa.
Lists Ellen Walker CPSC 201 Data Structures Hiram College.
CSC 212 Stacks & Queues. Announcement Many programs not compiled before submission  Several could not be compiled  Several others not tested with javadoc.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
Lecture 101 CS110 Lecture 10 Thursday, February Announcements –hw4 due tonight –Exam next Tuesday (sample posted) Agenda –questions –what’s on.
CSE 143 Lecture 24 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, , slides.
Stacks And Queues Chapter 18.
1 COMP313A Programming Languages Object Oriented Progamming Languages (2)
Lecture 121 CS110 Lecture 12 Tuesday, March 9, 2004 Announcements –hw5 due Thursday –Spring break next week Agenda –questions –ArrayList –TreeMap.
Lecture 71 CS110 Lecture 8 February 19, 2004 Announcements –hw3 due tonight –hw4 available, due Thursday February 26 –exam Tuesday March 2 Agenda –questions.
Classes and Objects CS177 Rec 10. Announcements Project 4 is posted ◦ Milestone due on Nov. 12. ◦ Final submission due on Nov. 19. Exam 2 on Nov. 4 ◦
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 14: Overloading and Templates Overloading will not be covered.
CS 2430 Day 12. Agenda for today Container class example: DateList Growable container classes.
1 CSE 331 Generics (Parametric Polymorphism) slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
CS 2430 Day 24. Announcements Quiz this Friday Program 5 posted on Monday Program 4 due date: Friday at 10pm Program 4 grace date: Wednesday at 10pm (don’t.
CS 367 Introduction to Data Structures Lecture 5.
Access Modifiers Control which classes use a feature Only class-level variables may be controlled by access modifiers Modifiers 1. public 2. protected.
M180: Data Structures & Algorithms in Java Queues Arab Open University 1.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Interfaces, Classes, Collections School of Engineering and Computer Science, Victoria University of Wellington COMP T2 Lecture 3 Marcus Frean.
Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
CS 367 Introduction to Data Structures Charles N. Fischer Fall s367.html.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 15 1.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
1 Queues (Continued) Queue ADT Linked queue implementation Array queue implementation Circular array queue implementation Deque Reading L&C , 9.3.
19-Mar-16 Collections and ArrayLists.. 2 Collections Why use Collections. Collections and Object-Orientation. ArrayLists. Special Features. Creating ArrayLists.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 18: Stacks and Queues.
Queues By Jimmy M. Lu. Overview Definition Standard Java Queue Operations Implementation Queue at Work References.
Chapter 18: Stacks and Queues.
HW-6 Deadline Extended to April 27th
Chapter 19: Stacks and Queues.
COMPUTER 2430 Object Oriented Programming and Data Structures I
COMPUTER 2430 Object Oriented Programming and Data Structures I
Stacks and Queues 1.
ArrayLists 22-Feb-19.
slides created by Alyssa Harding
Presentation transcript:

CS 2430 Day 26

Announcements Exam #2: Wednesday, April 3 –Review in lab on Tuesday, April 2 –Sample problems sent via

Agenda Generic Queue

Generic types

How to make a generic container class in Java?

First try Make a different class to hold each type of Object We would have BagOfDate, BagOfString, etc. Problem? Too much similarity! Why write all that code?

Second try Make containers backed by array of Object Can hold any type of Object Problem? Casting required to “get” items Example: Rational rat = (Rational)operands.pop(); Awkward: Container could hold Rational s, String s, Date s, etc.

Java generics Can make a generic container class that is instantiated with a particular type (of Object ) The “type” is a parameter to the class The “type” parameter MUST be a class, NOT a primitive type

public class Queue { private static final int DEFAULT_MAX = 10; private Object[] items; private int front, rear, count; public Queue() {... } public Queue(int inSize) {... } public boolean isEmpty() {... } public boolean isFull() {... } public void enqueue(Object obj) {... } public Object dequeue() {... } } Not generic! Not generic Queue

public class Queue // E must be a class, not a primitive { private static final int DEFAULT_MAX = 10; private E[] items; private int front, rear, count; public Queue() {... } public Queue(int inSize) {... } public boolean isEmpty() {... } public boolean isFull() {... } public void enqueue(E obj) {... } public E dequeue() {... } } Generic Queue

public class Queue // E must be a class, not a primitive { private static final int DEFAULT_MAX = 10; private E[] items; private int front, rear, count; public Queue() {... } public Queue(int inSize) {... } public boolean isEmpty() {... } public boolean isFull() {... } public void enqueue(E obj) {... } public E dequeue() {... } } Same as before, but with Object replaced by E Generic Queue

Implementation

Constructor public Queue() { items = ??? }

Does this work? public Queue() { items = new E[DEFAULT_MAX]; } Java doesn’t allow this! We need to create an array of Object and cast it to ( E[] ).

Constructor fixed! public Queue() { items = (E[]) new Object[DEFAULT_MAX]; } We might get a “warning” from the compiler. We’ll just live with it.

Constructors public Queue() { items = (E[]) new Object[DEFAULT_MAX]; } public Queue(int inSize) { if (inSize < 0) items = (E[]) new Object[DEFAULT_MAX]; else items = (E[]) new Object[inSize]; }

The rest is the same, but just replace Object with E

Client code Queue dq = new Queue (365); Queue rq = new Queue (40); Date date = dq.dequeue(); // no cast Rational rat = rq.dequeue(); rq.enqueue(new Rational(1, 2)); // same as before

Can also do this Queue dq = new Queue(365); Queue rq = new Queue(40); Date date = dq.dequeue(); // no cast Rational rat = rq.dequeue(); rq.enqueue(new Rational(1, 2)); // same as before

Can’t do this! Queue dq = new Queue(365); Queue rq = new Queue(40); Date date = dq.dequeue(); Rational rat = rq.dequeue(); rq.enqueue(new Rational(1, 2)); dq.enqueue(rat); // this is a syntax error!

Or this! Queue dq = new Queue(365); Queue rq = new Queue(40); Date date = dq.dequeue(); Rational rat = rq.dequeue(); rq.enqueue(new Rational(1, 2)); rat = dq.dequeue(); // this is a syntax error!