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.

Slides:



Advertisements
Similar presentations
Why not just use Arrays? Java ArrayLists.
Advertisements

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.
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.
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 +
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
COP3538 – Data Structures Using OOP Chapter 4 – Stacks and Queues.
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.
1 Generics, Type Safety, and Dynamic Data Structures.
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.
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:
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
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.
CSE 373 Data Structures and Algorithms Lecture 2: Queues.
1 Genericity Parameterizing by Type. 2 Generic Class One that is parameterized by type  Works when feature semantics is common to a set of types On object.
13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.
11 Values and References Chapter Objectives You will be able to: Describe and compare value types and reference types. Write programs that use variables.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Chapter 21 Generics 1. Generics - Overview Generic Methods specify a set of related methods Generic classes specify a set of related types Software reuse.
CS 2430 Day 26. Announcements Exam #2: Wednesday, April 3 –Review in lab on Tuesday, April 2 –Sample problems sent via .
Effective Java: Generics Last Updated: Spring 2009.
ITI Introduction to Computing II Lab-8 Dewan Tanvir Ahmed University of Ottawa.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
CSC 212 Stacks & Queues. Announcement Many programs not compiled before submission  Several could not be compiled  Several others not tested with javadoc.
Generics CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
09-1 Queues and List-Based ADT Implementations Problem Set: PS3 due Wednesday, March 7 Wellesley College CS230 Lecture 09 Monday, February 26 Handout #18.
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.
Arrays Construct array: new double[10] Store in variable of type double[] double[] data = new double[10];
OBJECTS FOR ORGANIZING DATA -- As our programs get more sophisticated, we need assistance organizing large amounts of data. : array declaration and use.
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
CSE 143 Lecture 24 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, , slides.
Week 4 - Monday.  What did we talk about last time?  Queues  Implementing queues with circular arrays.
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 ◦
1 CSE 331 Generics (Parametric Polymorphism) slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
1 CSC 2053 New from AutoBoxing 3 Before J2SE 5.0, working with primitive types required the repetitive work of converting between the primitive.
M180: Data Structures & Algorithms in Java Queues Arab Open University 1.
Introduction to Collections. Collections Collections provide a way of organizing related data in a model Different types of collections have different.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Announcements Final Exam: TBD. Static Variables and Methods static means “in class” methods and variables static variable: one per class (not one per.
Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
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];
CS 367 Introduction to Data Structures Charles N. Fischer Fall s367.html.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
Queues CS 367 – Introduction to Data Structures. Queue A queue is a data structure that stores data in such a way that the last piece of data stored,
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.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Linked Data Structures
Queues 11/9/2018 6:28 PM Queues 11/9/2018 6:28 PM Queues.
Queues 11/16/2018 4:19 AM Queues 11/16/2018 4:19 AM Queues.
Priority Queues.
COMPUTER 2430 Object Oriented Programming and Data Structures I
COMPUTER 2430 Object Oriented Programming and Data Structures I
COMPUTER 2430 Object Oriented Programming and Data Structures I
COMPUTER 2430 Object Oriented Programming and Data Structures I
Queues 12/30/2018 9:24 PM Queues 12/30/2018 9:24 PM Queues.
slides created by Alyssa Harding
ArrayLists 22-Feb-19.
Java Programming Language
Presentation transcript:

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 worry about what the WWW says)

Agenda Finish Queue examples Generics A generic Queue

Go to THE THING:

The Queue class

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

Implementation of Queue methods…

…you do it as part of Program 5

Example How many items are currently stored in the Queue ?

As implementer public int size() { return count; }

As user public static int size(Queue inQueue, int qSize) { int num = 0; while (!inQueue.isEmpty()) { inQueue.enqueue(inQueue.dequeue()); num++; } return num; } What’s wrong? Infinite loop!

As user public static int size(Queue inQueue, int qSize) { int num = 0; Queue tq = new Queue(qSize); while (!inQueue.isEmpty()) { tq.enqueue(inQueue.dequeue()); num++; } while (!tq.isEmpty()) inQueue.enqueue(tq.dequeue()); return num; } All good!

Example How do you delete all occurrences of a specified item from a Queue and maintain relative order?

As user public static void removeAll(Queue q, Object x, int qSize) { Queue tq = new Queue(qSize); while (!q.isEmpty()) { Object o = q.poll(); if (!o.equals(x)) tq.offer(o); } q = tq; } This does not work!

Java: no such thing as “pass by reference”

main() public static void main(String args[]) { Queue q = new Queue(5); Object x;... removeAll(q, x, 5); } public static void removeAll(Queue q, Object x, int qSize) { Queue tq = new Queue(qSize); while (!q.isEmpty()) { Object o = q.poll(); if (!o.equals(x)) tq.offer(o); } q = tq; } q removeAll() q tq x ► ► ► ► ► ► ► ► ► ► uoiea x e uoia

UOIE public static void removeAll(Queue q, Object x, int qSize) { Queue tq = new Queue(qSize); while (!q.isEmpty()) { Object o = q.poll(); if (!o.equals(x)) tq.offer(o); } while (!tq.isEmpty()) q.offer(tq.poll()); } A How about this? qtq EIOUA E ► ► ► ► ► ► AAIIOOUU x

UOIAUOIE public static void removeAll(Queue q, Object x, int qSize) { Queue tq = new Queue(qSize); while (!q.isEmpty()) { Object o = q.poll(); if (!o.equals(x)) tq.offer(o); } while (!tq.isEmpty()) q.offer(tq.poll()); } A How about this? qtq ► ► ► AIOU AA A II I OO O UU U

Example How to reverse a given Queue ? Use a Stack !

Another Example How do you swap the back two elements of a Queue ? A B C D  A B D C

Any questions?

Go to THE THING:

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! The $5 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!

Next: multiple inheritance