== isn t always equal?

Similar documents
Another interface: Comparable

Last time s big ideas

Programming Abstractions

CPSC 221: Algorithms and Data Structures Lecture #1: Stacks and Queues

CPSC 221: Algorithms and Data Structures ADTs, Stacks, and Queues

Basic Data Structures

Discussion 2C Notes (Week 3, January 21) TA: Brian Choi Section Webpage:

CSE 373 SEPTEMBER 29 STACKS AND QUEUES

final int a = 10; for(int i=0;i<a;i+=2) { for(int j=i;j<a;j++) { System.out.print("*"); } System.out.println(); }

Apply to be a Meiklejohn! tinyurl.com/meikapply

Abstract Data Types. Abstract Data Types

CMP Points Total Midterm Spring Version (16 Points) Multiple Choice:

Stacks and Queues. Introduction to abstract data types (ADTs) Stack ADT. Queue ADT. Time permitting: additional Comparator example

Basic Data Structures 1 / 24

CS 2230 CS II: Data structures. Meeting 26: the Set ADT Brandon Myers University of Iowa

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. Stacks and Queues. Lecture 11.

CS 2230 CS II: Data structures. Meeting 21: trees Brandon Myers University of Iowa

CS 2230 CS II: Data structures. Meeting 31: Priority queue Brandon Myers University of Iowa

Interfaces & Generics

CS24 Week 4 Lecture 2

Day 6. COMP1006/1406 Summer M. Jason Hinek Carleton University

The Stack ADT. Stacks and Queues. Stack: Illustration. What is a Stack? Accessors top size isempty

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

CMP Points Total Midterm Spring Version (16 Points) Multiple Choice:

Data Abstraction and Specification of ADTs

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

No Aids Allowed. Do not turn this page until you have received the signal to start.

What Can You Use a Queue For?

Stacks and Queues. EECS2030: Advanced Object Oriented Programming Fall 2017 CHEN-WEI WANG

CS171 Midterm Exam. October 29, Name:

CS/ENGRD 2110 SPRING Lecture 7: Interfaces and Abstract Classes

More Group HW. #ifndef Stackh #define Stackh. #include <cstdlib> using namespace std;

Queues. Virtuelle Fachhochschule. Prof. Dr. Debora Weber-Wulff

Name: After line 1: After line 3: After line 5: After line 8: After line 11:

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Announcements. CS18000: Problem Solving And Object-Oriented Programming

CT 229 Object-Oriented Programming Continued

CS 2230 CS II: Data structures. Meeting 20: generic types, exceptions, higher order functions Brandon Myers University of Iowa

CSE 332: Data Structures. Spring 2016 Richard Anderson Lecture 1

Stacks and Queues

Lecture 6. COMP1006/1406 (the OOP course) Summer M. Jason Hinek Carleton University

csci 210: Data Structures Stacks and Queues

CS 2150 Exam 1, Spring 2018 Page 1 of 6 UVa userid:

COMP250: Stacks. Jérôme Waldispühl School of Computer Science McGill University. Based on slides from (Goodrich & Tamassia, 2004)

Insertions and removals follow the Fist-In First-Out rule: Insertions: at the rear of the queue Removals: at the front of the queue

Communications of the ACM Learning to Learn Denning & Flores learning-to-learn/fulltext

CMSC 132: Object-Oriented Programming II. Stack and Queue

Name: 1 stack<int> s; 2 s.push(9) 3 s.push(5) 4 s.push(10) 5 s.push(1) 6 s.pop() 7 s.pop() 8 s.push(0) 9 s.pop() 10 s.pop() 11 s.

COL106: Data Structures and Algorithms. Ragesh Jaiswal, IIT Delhi

16. Dynamic Data Structures

CSE 143. Lecture 4: Stacks and Queues

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures

Standard ADTs. Lecture 19 CS2110 Summer 2009

! A data type for which: ! An ADT may be implemented using various. ! Examples:

Computer Science Foundation Exam. Dec. 19, 2003 COMPUTER SCIENCE I. Section I A. No Calculators! KEY

Introduction to the Stack. Stacks and Queues. Stack Operations. Stack illustrated. elements of the same type. Week 9. Gaddis: Chapter 18

Summer Final Exam Review Session August 5, 2009

Priority Queue ADT. Revised based on textbook author s notes.

COMP 250 Midterm #2 March 11 th 2013

Stacks and Queues. Chapter Stacks

CMSC Introduction to Algorithms Spring 2012 Lecture 7

CMPT 125: Practice Midterm Answer Key

CSE 326 Team. Today s Outline. Course Information. Instructor: Steve Seitz. Winter Lecture 1. Introductions. Web page:

CS200 Spring 2004 Midterm Exam II April 14, Value Points a. 5 3b. 16 3c. 8 3d Total 100

Queues Fall 2018 Margaret Reid-Miller

University of Waterloo Department of Electrical and Computer Engineering ECE250 Algorithms and Data Structures Fall 2014

Why do we have to write a type for every variable in Java?

SOLUTIONS (INCOMPLETE, UNCHECKED)

No Aids Allowed. Do not turn this page until you have received the signal to start.

Stacks. Stacks. Main stack operations. The ADT Stack stores arbitrary objects. Insertions and deletions follow the last-in first-out (LIFO) principle.


CMSC 132, Object-Oriented Programming II Summer Lecture 9:

CSc 120. Introduction to Computer Programming II. 14: Stacks and Queues. Adapted from slides by Dr. Saumya Debray

CS 106B Lecture 5: Stacks and Queues

Sorting race.

Queues. Stacks and Queues

Introduction to the Stack. Stacks and Queues. Stack Operations. Stack illustrated. Week 9. elements of the same type.

Keeping Order:! Stacks, Queues, & Deques. Travis W. Peters Dartmouth College - CS 10

The Bucharest University of Economic Studies. Data Structures. ADTs-Abstract Data Types Stacks and Queues

Where we are going (today)

More Data Structures (Part 1) Stacks

CS200: Queues. Prichard Ch. 8. CS200 - Queues 1

Data Structures. BSc in Computer Science University of New York, Tirana. Assoc. Prof. Marenglen Biba 1-1

COMP 213 Advanced Object-oriented Programming Lecture 8 The Queue ADT (cont.)

CS S-04 Stacks and Queues 1. An Abstract Data Type is a definition of a type based on the operations that can be performed on it.

CSCE 2014 Final Exam Spring Version A

Extra Credit: write mystrlen1 as a single function without the second parameter int mystrlen2(char* str)

CIS Fall Data Structures Midterm exam 10/16/2012

CS2113 Lab: Collections 10/29/2018

CSE143 Exam with answers Problem numbering may differ from the test as given. Midterm #2 February 16, 2001

double d0, d1, d2, d3; double * dp = new double[4]; double da[4];

Name CPTR246 Spring '17 (100 total points) Exam 3

! A data type for which: ! In fact, an ADT may be implemented by various. ! Examples:

Data Structures and Algorithms

Midterm Exam (REGULAR SECTION)

CSCI 136 Data Structures & Advanced Programming. Lecture 13 Fall 2018 Instructors: Bill 2

Section 05: Solutions

CS 2230 CS II: Data structures. Limits of comparison sorting, beyond comparison sorting Brandon Myers University of Iowa

Your Topic Goes Here Queue

16-Dec-10. Collections & Data Structures ABSTRACTION VS. IMPLEMENTATION. Abstraction vs. Implementation

Transcription:

== isn t always equal? In Java, == does the expected for primitives. int a = 26; int b = 26; // a == b is true int a = 13; int b = 26; // a == b is false Comparing two references checks if they are pointing to the same object Patient p1 = new Patient("Marion", 100); Patient p2 = new Patient("Marion", 100); Patient p3 = p1; // p1 == p2 is false // p1 == p3 is true Not pointing to the same object? not ==

The equals() method We decide that two Patients are equal() when they have the same name and height the code that does that... public boolean equals(object o) { if (o instanceof Patient){ Patient op = (Patient) o; return this.height==op.height && this.name.equals(op.name); } } else { return false; } Every Java class already has an invisible equals method defined. But you have to override it with your own if you want to do something smarter like compare the fields. String.equals compares the content of the String instead of references Secondary new things in this snippet of code instanceof to check if o is a Patient casting o from Object to Patient

Peer instruction boolean equals(object o) { if (o instanceof Cat) { Cat c = (Cat) o; return this.breed.equals(o.breed); } return false; } Object o1 = new Object(); Object o2 = new Cat( Siamese ); Cat o3 = new Cat( Tabby ); Cat o4 = new Cat( Siamese ); Cat o5 = o2; Which are true statements? A. o1 == o2 B. o2 == o4 C. o2 == o5 D. o4==o5 E. o2.equals(o4) F. o2.equals(o5) G. o4.equals(o5) H. o2.equals(o3) https://b.socrative.com/login/student/ room CS2230X ids 1000-2999 room CS2230Y ids 3000+

Midterm 1 Next Tuesday 9/19 in class, 75 minutes What is on it? Anything you ve practiced including HW3 (incl. quizzes, peer instructions, HWs, labs) Is there any practice/review? Pre-lab is midterm1_sp17 Section on 9/21 is a review session Notes allowed on exam? 1 double-sided 8.5 x11 sheet of notes

CS 2230 CS II: Data structures Meeting 9: More ADTs: queues Brandon Myers University of Iowa

Today s big ideas Examine and implement two more Abstract Data Types: Stack and Queue

Queue ADT Queues use FIFO order First In First Out https://upload.wikimedia.org/wikipedia/commons/thumb/5/52/data_queu e.svg/2000px-data_queue.svg.png

public interface Queue { /* Insert element at back of queue */ public void enqueue(object ele); /* Remove element from front of queue and return it */ public Object dequeue(); } /* Return the element at the front of queue */ public Object peek();

Peer instruction Queue x = <instantiate a queue>; x.enqueue(100); x.enqueue(22); System.out.println(x.peek()); x.enqueue(50); x.dequeue(); What is does the abstract state of the Queue x look like after the code runs? (front on Queue is left side) A. [100, 22, 50] B. [50, 22, 100] C. [22, 50, 100] D. [22, 50] E. [50, 22] F. [100, 22] G. [22, 100] https://b.socrative.com/login/student/ room CS2230X ids 1000-2999 room CS2230Y ids 3000+

public interface Queue { /* Insert element at back of queue */ public void enqueue(object ele); /* Remove element from front of queue and return it. Returns null if queue is empty */ public Object dequeue(); } /* Return the element at the front of queue. Returns null if queue is empty */ public Object peek(); Give one way you might implement the Queue ADT: how would the be data stored? how would you find the front? the back? https://b.socrative.com/login/student/ room CS2230X ids 1000-2999 room CS2230Y ids 3000+

Queue q = new LinkedListQueue(); LinkedListQueue q head LinkedListQueue tail q.enqueue(100); q.enqueue(300); q head LinkedListQueue tail 100 300 q.dequeue(); draw the LinkedListQueue now

Empty case [] Non-empty case [100, 300] head tail LinkedListQueue head tail 100 300 LinkedListQueue /* Return the element at the front of queue. Returns null if queue is empty */ @Override public Object peek() { }

Empty case [] Non-empty case [100, 300] head tail LinkedListQueue head tail 100 300 LinkedListQueue /* Remove element from front of queue and return it. Returns null if queue is empty */ public Object dequeue() { }

Queue q = new ArrayListQueue(); q elements ArrayListQueue ArrayListQueue frontindex backindex 0 0 size 0 q q.enqueue(100); q.enqueue(300); q,dequeue(); elements ArrayListQueue 300 frontindex backindex 1 1 size 1

q elements ArrayListQueue 300 frontindex backindex 1 1 size 1 q.enqueue(400) q.dequeue(); draw the ArrayListQueue now

q for your reference elements frontindex backindex ArrayListQueue 100 300 0 1 How should we check if an ArrayListQueue is empty? A. elements == null B. frontindex==backindex C. size==0 D. all items in the elements array are null E. frontindex > backindex F. frontindex < backindex size 2 https://b.socrative.com/login/student/ room CS2230X ids 1000-2999 room CS2230Y ids 3000+

q elements ArrayListQueue public interface Queue { /* Insert element at back of queue */ public void enqueue(object ele); frontindex 2 size backindex 1 400 500 100 200 300 /* Remove element from front of queue and return it. Returns null if queue is empty */ public Object dequeue(); q.enqueue(600); 5 What should we do in this case? } /* Return the element at the front of queue. Returns null if queue is empty */ public Object peek(); https://b.socrative.com/login/student/ room CS2230X ids 1000-2999 room CS2230Y ids 3000+

One solution to full array: resize and move over the front 1. enqueue(600) to this elements frontindex 2 size 5 backindex 1 0 1. 2. 3. 4. 400 500 100 200 300 3. starting at frontindex, copy element i to i+1; when you get to size, start at 0 and copy element i to i bigger 0 1. 2. 3. 4. 5 400 500 100 200 300 2. allocate array of length size+1 bigger 0 1. 2. 3. 4. 5 4. copy the new element to backindex+1 bigger 0 1. 2. 3. 4. 5 400 500 600 100 200 300 5. point elements to bigger elements 0 1. 2. 3. 4. 400 500 100 200 300 bigger 0 1. 2. 3. 4. 5 400 500 600 100 200 300

Stack ADT Stacks use LIFO order Last In First Out

Real-life example where... a specific real-life process/situation/etc behaves like a stack or, a specific application where a stack data structure would be useful https://b.socrative.com/login/student/ room CS2230X ids 1000-2999 room CS2230Y ids 3000+

public interface Stack { /* put the element on the top of the stack */ public void push(object ele); /* remove the element on top of the stack and return it; Returns null if stack is empty */ public Object pop(); } /* return the element on top of the stack; returns null if stack is empty */ public Object peek();

Stack s = new LinkedListStack(); LinkedListStack S head LinkedListStack s.push(100); s.push(300); S head LinkedListStack 300 100 s.pop(); draw the LinkedListStack now

Empty case [] Non-empty case [300, 100] head LinkedListStack head 300 100 LinkedListStack /* remove the element on top of the stack and return it; Returns null if stack is empty */ public Object pop(); }

Today s big ideas Examine and implement two more Abstract Data Types: Stack and Queue