Object Oriented Programming and Design in Java. Session 19 Instructor: Bert Huang

Similar documents
Object Oriented Programming and Design in Java. Session 18 Instructor: Bert Huang

PA3 Design Specification

27/04/2012. Objectives. Collection. Collections Framework. "Collection" Interface. Collection algorithm. Legacy collection

Tutorial #11 SFWR ENG / COMP SCI 2S03. Interfaces and Java Collections. Week of November 17, 2014

Java Collections Framework. 24 April 2013 OSU CSE 1

Collections. Powered by Pentalog. by Vlad Costel Ungureanu for Learn Stuff

Standard ADTs. Lecture 19 CS2110 Summer 2009

CMSC 341. Linked Lists, Stacks and Queues. CMSC 341 Lists, Stacks &Queues 1

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

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

Big Java Late Objects

Collections Questions

1 P age DS & OOPS / UNIT II

STANDARD ADTS Lecture 17 CS2110 Spring 2013

Summer Final Exam Review Session August 5, 2009

Abstract Data Types (ADTs) Queues & Priority Queues. Sets. Dictionaries. Stacks 6/15/2011

Stacks and Queues. David Greenstein Monta Vista

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

Stacks and Queues

Linked lists. Yet another Abstract Data Type Provides another method for providing space-efficient storage of data

Object Oriented Programming and Design in Java. Session 21 Instructor: Bert Huang

Java HashMap Interview Questions

Abstract Data Types Data Structure Grand Tour Java Collections.

Collection Framework Collection, Set, Queue, List, Map 3

CSE 373: Data Structures and Algorithms

COMP2402: Mid-term Review Questions

Collections Framework: Part 2

cs Java: lecture #6

Abstract Data Types Data Structure Grand Tour Java Collections.

+ Abstract Data Types

Collections, Maps and Generics

COMP 103 : Test. 2018, Sept 12//(Corrected)

Introduction to Programming System Design CSCI 455x (4 Units)

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

Computational Applications in Nuclear Astrophysics using Java Java course Lecture 6

Hash tables. hashing -- idea collision resolution. hash function Java hashcode() for HashMap and HashSet big-o time bounds applications

Linked lists (6.5, 16)

Lecture 4. The Java Collections Framework

Data Structures. COMS W1007 Introduction to Computer Science. Christopher Conway 1 July 2003

CMPT 126: Lecture 11 Collections: Abstract Data Types and Dynamic Representation (Chapt. 12)

COMS W1007 Homework 4

Hashing. Reading: L&C 17.1, 17.3 Eck Programming Course CL I

CS61BL Summer 2013 Midterm 2

CS 314 Midterm 2 Fall 2012

Lists. CSC212 Lecture 8 D. Thiebaut, Fall 2014

Casting -Allows a narrowing assignment by asking the Java compiler to "trust us"

CISC 3115 TY3. C28a: Set. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 11/29/2018 CUNY Brooklyn College

CMSC132, Practice Questions

SUMMARY INTRODUCTION COLLECTIONS FRAMEWORK. Introduction Collections and iterators Linked list Array list Hash set Tree set Maps Collections framework

Java Collections Framework reloaded

CE204 Data Structures and Algorithms Part 2

Abstract Data Types Data Structure Grand Tour Java Collections.

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

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Winter 2016 COMP-250: Introduction to Computer Science. Lecture 6, January 28, 2016

CS61BL: Data Structures & Programming Methodology Summer 2014

csci 210: Data Structures Stacks and Queues

Hash Table. Ric Glassey

Data Structure. Recitation VII

8. Fundamental Data Structures

AP Computer Science 4325

COMP 103 : Test. 2018, Sept 12//(Corrected) ** WITH SOLUTIONS

Announcements. Submit Prelim 2 conflicts by Thursday night A6 is due Nov 7 (tomorrow!)

Information Science 2

Problem 1: Building the BookCollection application

Stacks (5.1) Abstract Data Types (ADTs) CSE 2011 Winter 2011

V.S.B ENGINEERING COLLEGE DEPARTMENT OF INFORMATION TECHNOLOGY I IT-II Semester. Sl.No Subject Name Page No. 1 Programming & Data Structures-I 2

III Data Structures. Dynamic sets

CS350 - Exam 1 (100 Points)

Computer Science 62. Bruce/Mawhorter Fall 16. Midterm Examination. October 5, Question Points Score TOTAL 52 SOLUTIONS. Your name (Please print)

DM550 Introduction to Programming part 2. Jan Baumbach.

Linked Structures. See Section 3.2 of the text.

CS PROGRAMMING & ATA STRUCTURES I. UNIT I Part - A

Sequence Abstract Data Type

11/27/12. CS202 Fall 2012 Lecture 11/15. Hashing. What: WiCS CS Courses: Inside Scoop When: Monday, Nov 19th from 5-7pm Where: SEO 1000

1/22/13. Queue? CS 200 Algorithms and Data Structures. Implementing Queue Comparison implementations. Photo by David Jump 9. Colorado State University

Hash table basics mod 83 ate. ate. hashcode()

1/22/13. Queue. Create an empty queue Determine whether a queue is empty Add a new item to the queue

Building Java Programs

Queue? Part 4. Queues. Photo by David Jump. Create an empty queue Items leave a queue from its front.

Lecture 3: Queues, Testing, and Working with Code

More Data Structures (Part 1) Stacks

CS 231 Data Structures and Algorithms Fall DDL & Queue Lecture 20 October 22, Prof. Zadia Codabux

Definition of a Heap. Heaps. Priority Queues. Example. Implementation using a heap. Heap ADT

Algorithms. Produced by. Eamonn de Leastar

Queues Fall 2018 Margaret Reid-Miller

Introduction to Software Design

The Java Collections Framework. Chapters 7.5

Announcements/Follow-ups

CS 1114: Implementing Search. Last time. ! Graph traversal. ! Two types of todo lists: ! Prof. Graeme Bailey.

182 review 1. Course Goals

Problem One: Loops and ASCII Graphics

Lecture 2: Implementing ADTs

EXAMINATIONS 2015 COMP103 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS

Chapter 18: Stacks And Queues

SRM ARTS AND SCIENCE COLLEGE SRM NAGAR, KATTANKULATHUR

Topic 10: The Java Collections Framework (and Iterators)

Queues. Stacks and Queues

Queues. Queue ADT. Queue Specification. An ordered sequence A queue supports operations for inserting (enqueuing) and removing (dequeuing) elements.

CSE 373. Java Collection Framework. reading: Weiss Ch. 3, 4.8. slides created by Marty Stepp

Transcription:

Object Oriented Programming and Design in Java Session 19 Instructor: Bert Huang

Announcements Homework 4 due MONDAY. Apr. 19 No multithreading in programming part

Review Deadlocks and the Dining Philosophers Problem More on Threads in Java ReentrantLock Thread, Runnable, Object javadoc Keywords synchronized and volatile Programming by contract and threads

Todayʼs Plan Homework tips Data Structures Lists, Stacks, Queues Sets, HashSet Maps, HashMap

Homework Tips: Main Program Remember that the framework will do a huge portion of the work; make a non-animating version first Create the button that toggles the timer on and off. Test the timer and the toggle button by having actionlistener print to console Each time Timer ticks, compute the new position and velocity for each node Force = k * lengthofedge, Velocity = Velocity + Accel., Accel. = Force / Mass Position = oldposition + Velocity After moving nodes, call repaint() on the GraphFrame

Homework Tips JButton GraphFrame loaded Graph Timer Graph Serialization will disconnect animation logic (Timer etc) because framework encapsulates Graph Main can connect Graph and Timer to "Start Animation" JButton, but loaded Graph is a private reference One solution: adding accessor for Graph in GraphFrame, and having the Timer call frame.getgraph().animate()

Abstract Data Types Data structures implement abstract data types (ADT), analogous to interfaces Algorithms for efficient data manipulation can be complicated; encapsulate them! Vast library of well-studied ADTs. Don't reinvent the wheel, don't reinvent the hash table ADTs include: Lists, Sets, Maps

ADTs and Interfaces Itʼs good practice to treat all your data structures through their interfaces Only the constructor knows the actual type; changing implementation is easy Makes your code more reusable (but be careful about being too general)

Efficiency Abstract Data Types usually have limited functionality ideally optimized for the limited functionality The more limited the functionality, the faster the operations should be Design efficient programs by using the most limited ADT that will do the job

Lists An ordered series of objects Each object has a previous and next Except first has no prev., last has no next We can insert an object (at location k) We can remove an object (at location k) We can read an object (from location k)

ArrayList Essentially a wrapper for an array Store elements in array, but handles list operations by shifting elements h l l o h e l l o If array is full, copies into a new larger array O(1) get, O(N) insert/remove O(1) insert/remove at the end of list

LinkedList Stores elements in Link objects Each link has reference to next (and prev) prev links only in doubly-linked list Navigate by following next() references O(1) insert/remove with reference But need O(N) to find (get) reference 0 1 2 3 4

Stacks and Queues Stack - Last in first out push() - add element to top of stack pop() - remove element from top Queue - First in first out enqueue (offer) - add element at back of line dequeue (poll) - remove from front of line image from http://bwog.net/2006/05/03/tray-spotting

Stack Implementation Must be as fast as possible: O(1) Singly Linked List: add to beginning, remove from beginning e d c b a Array List: add to end, remove from end a b c d d c b a a b c d e

Queue Implementation Doubly-linked list: add at beginning, remove from end Array: "circular array" mark beginning and end, wrap around when either exceeds array length add at end, remove from beginning but don't shift d e a b c d e f b c

Hierarchy Should Stack or Queue implement the Collections Interface type? Should Stack or Queue implement the List Interface type? java.util.stack extends Vector, which implements both java.util.queue is subinterface of Collection, but not List

Sets An unordered collection No duplicate entries We can insert an object We can check for an object contains() We can remove an object

HashSet Uses hashcode() to index into an array Collisions occur when distinct elements hash into the same index Collisions resolved by trying empty spots in a systematic way

Maps Maps are collections of objects "indexed" by other objects key types map to value types No duplicate keys, duplicate values allowed aka "associative array"

HashMap Map<String, Double> costs = new HashMap<String, Double>(); mymap.put("big Mac", 2.99); mymap.get("big Mac"); index by the key's hashcode() but insert value instead of key

Sets, Maps, Collections Recall that Set is a subinterface of Collections that has no new methods HashMap doesn't implement Collection Has methods Set<K> keyset() Collection<V> values()

Reading Might be worth reviewing parts of previous reading: Lists: Horstmann 1.11 Queues: Horstmann p. 42 Stacks: Horstmann p. 256-257 More discussion in section on Collections Framework, Section 8.3