Lecture 3: Queues, Testing, and Working with Code

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Stack & Queues COP 3502.
0 of 37 Stacks and Queues Lecture of 37 Abstract Data Types To use a method, need to know its essentials: signature and return type o additionally,
CHAPTER 7 Queues.
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
Queues Cmput Lecture 19 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based on code from.
CSE 143 Lecture 7 Stacks and Queues reading: Stuart Reges notes on website slides created by Marty Stepp
Building Java Programs
CSE 373 Data Structures and Algorithms Lecture 2: Queues.
CSE 143 Lecture 7 Stacks and Queues reading: "Appendix Q" (see course website) slides created by Marty Stepp and Hélène Martin
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
CSCA48 Course Summary.
CSE 143 Lecture 5 Stacks and Queues slides created by Marty Stepp
CSE 373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
Stacks and Queues. 2 3 Runtime Efficiency efficiency: measure of computing resources used by code. can be relative to speed (time), memory (space), etc.
Stacks And Queues Chapter 18.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Week 2 - Friday.  What did we talk about last time?  Computing Big Oh.
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
Stacks and Queues CMSC 201. Stacks and Queues Sometimes, when we use a data-structure in a very specific way, we have a special name for it. This is to.
CSE 373 Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
Stacks and Queues. 2 Abstract Data Types (ADTs) abstract data type (ADT): A specification of a collection of data and the operations that can be performed.
Review Array Array Elements Accessing array elements
CSE373: Data Structures & Algorithms
Queues 5/11/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
CSE 373 Implementing a Stack/Queue as a Linked List
COSC160: Data Structures: Lists and Queues
Stacks and Queues.
September 29 – Stacks and queues
Queues Rem Collier Room A1.02
March 27 – Course introductions; Adts; Stacks and Queues
March 29 – Testing and Priority QUeues
Week 3 - Friday CS221.
CSE 373: Data Structures and Algorithms
Stacks and Queues.
Algorithm Analysis and Modeling
Data Structures and Algorithms
Lecture 3: Working with Data Structures
Stacks and Queues.
Building Java Programs
i206: Lecture 11: Stacks, Queues
Lecture 1: Welcome to CSE 373
Lecture 1: Welcome to CSE 373
CMSC 341 Lecture 5 Stacks, Queues
structures and their relationships." - Linus Torvalds
Lecture 2: Implementing ADTs
Stacks, Queues, and Deques
Lecture 2: Implementing ADTs
Data Structures and Algorithms
Stacks and Queues.
Data Structures – Stacks and Queus
ITEC 2620M Introduction to Data Structures
Data Structures and Algorithms
Lecture 5: Master Theorem, Maps, and Iterators
Building Java Programs
Building Java Programs
Implementing Hash and AVL
CSE 214 – Computer Science II Stacks
Stacks and Queues CLRS, Section 10.1.
slides created by Marty Stepp
Based on slides by Alyssa Harding & Marty Stepp
Lecture 2: Stacks and Queues
Lecture 3: Maps and Iterators
Lecture 4: Introduction to Code Analysis
Stacks, Queues, and Deques
Lecture 3: Maps and Iterators
Lecture 2: Stacks and Queues
structures and their relationships." - Linus Torvalds
Stacks and Queues.
CMPT 225 Lecture 8 – Queue.
Presentation transcript:

Lecture 3: Queues, Testing, and Working with Code Data Structures and Algorithms CSE 373 Su 18 – ben jones

Clearing up Stacks CSE 373 SU 18 – Ben Jones

Clearing up Stacks CSE 373 SU 18 – Ben Jones

Clearing up Stacks CSE 373 SU 18 – Ben Jones

Clearing up Stacks (push them over) CSE 373 SU 18 – Ben Jones

Clearing up Stacks (push them over) size = 8 top 1 2 3 4 v 5 6 8 7 6 5 4 3 7 8 2 1 CSE 373 SU 18 – Ben Jones

Review: What is a Queue? queue: Retrieves elements in the order they were added. First-In, First-Out ("FIFO") Elements are stored in order of insertion but don't have indexes. Client can only add to the end of the queue, and can only examine/remove the front of the queue. basic queue operations: add(item): aka “enqueue” add an element to the back. remove(): aka “dequeue” Remove the front element and return. peek(): Examine the front element without removing it. size(): how many items are stored in the queue? isEmpty(): if 1 or more items in the queue returns true, false otherwise front back 1 2 3 remove, peek add queue CSE 143 SP 17 – Zora Fung

Queues as Linked Lists CSE 373 Su 18 – Ben Jones

Queues an Arrays CSE 373 Su 18 – ben jones

Queues an Arrays – Looping Back Discuss with your neighbors: 1) What data do I need to store in addition to the array itself? (variable(s) + type(s)) 2) How do you calculate the next array position to fill? (equation and/or code) 3) How do you determine if the array is full? Empty? (equation and/or code) 4) How do you resize the array if needed? (description of algorithm) CSE 373 SU 18 – Ben Jones

Queues an Arrays – Looping Back Discuss with your neighbors: 1) What data do I need to store in addition to the array itself? (variable(s) + type(s)) 2) How do you calculate the next array position to fill? (equation and/or code) 3) How do you determine if the array is full? Empty? (equation and/or code) 4) How do you resize the array if needed? (description of algorithm) CSE 373 SU 18 – Ben jones

Queues an Arrays – Looping Back Discuss with your neighbors: 1) What data do I need to store in addition to the array itself? (variable(s) + type(s)) 2) How do you calculate the next array position to fill? (equation and/or code) 3) How do you determine if the array is full? Empty? (equation and/or code) 4) How do you resize the array if needed? (description of algorithm) CSE 373 SU 18 – Ben jones

Queues an Arrays – Looping Back Discuss with your neighbors: 1) What data do I need to store in addition to the array itself? (variable(s) + type(s)) 2) How do you calculate the next array position to fill? (equation and/or code) 3) How do you determine if the array is full? Empty? (equation and/or code) 4) How do you resize the array if needed? (description of algorithm) CSE 373 SU 18 – Ben jones

Question from last time: Aren’t these Lists? Yes, a doubly-linked list can act as both a stack and a queue! You’ll build one of these in your first partner project. Small correction from last lecture: I flipped a <= sign in the Stack implementation – I checked data.length >= size instead of size >= data.length Sometimes called a dequeue for “double ended queue” Eagle-eyed from last lecture caught that I flipped the inequality while I was typing. CSE 373 SU 18 – Ben Jones

Announcements Course Background Survey (required – due next Friday) TA Office Hours Posted - All in CSE 006 – Calendar on Website Ben’s Office Hours shifted to avoid section 2:10 – 4:10 T Th. Problems and solutions posted for yesterday’s section after-all. Feedback Form on Website Course Background Survey (required – due next Friday) Homework 1 releases (individual – due next Friday) CSE 373 SU 18 – Ben jones

Homework Demo gitlab.cs.washington.edu CSE 373 SU 18 – Ben jones

Testing Computers don’t make mistakes- people do! “I’m almost done, I just need to make sure it works” – Naive 14Xers Software Test: a separate piece of code that exercises the code you are assessing by providing input to your code and finishes with an assertion of what the result should be. 1. Isolate break your code into small modules 2. Build in increments Make a plan from simplest to most complex cases 3. Test as you go As your code grows, so should your tests We’re giving you a lot of tests – you usually will need to make your own. When you are “finished coding” – getting the thing to compile, it’s hardly done. Probably 2/3 of the time is spent going from “compiling” to “working” Tests can both guide this process and serve as a regression test (avoid breaking things that you previously had working) CSE 373 SP 18 - Kasey Champion

Types of Tests Black Box White Box Behavior only – ADT requirements From an outside point of view Does your code uphold its contracts with its users? Performance/efficiency White Box Includes an understanding of the implementation Written by the author as they develop their code Break apart requirements into smaller steps “unit tests” break implementation into single assertions HW1 gives you a bunch of black-box tests. An example of a white box test would be to test that the array resizing is working as intended in the CSE 373 SP 18 - Kasey Champion

What to test? Expected behavior Forbidden Input Empty/Null The main use case scenario Does your code do what it should given friendly conditions? Forbidden Input What are all the ways the user can mess up? Empty/Null Protect yourself! How do things get started? Boundary/Edge Cases First last Scale Is there a difference between 10, 100, 1000, 10000 items? CSE 373 SP 18 - Kasey Champion

Thought Experiment Boundary/Edge Cases Scale Discuss with your neighbors: Imagine you are writing an implementation of the List interface that stores integers in an Array. What are some ways you can assess your program’s correctness in the following cases: Expected Behavior Create a new list Add some amount of items to it Remove a couple of them Forbidden Input Add a negative number Add duplicates Add extra large numbers Empty/Null Call remove on an empty list Add to a null list Call size on an null list Boundary/Edge Cases Add 1 item to an empty list Set an item at the front of the list Set an item at the back of the list Scale Add 1000 items to the list Remove 100 items in a row Set the value of the same item 50 times CSE 373 SP 18 - Kasey Champion

JUnit JUnit: a testing framework that works with IDEs to give you a special GUI experience when testing your code @Test public void myTest() { Map<String, Integer> basicMap = new LinkedListDict<String, Integer>(); basicMap.put(“Fido”, 42); assertEquals(42, basicMap.get(“Fido”)); } Assertions: assertEquals(item1, item2) assertTrue(Boolean expression) assertFalse(bollean expression) assertNotNull(item) More: https://junit.org/junit5/docs/5.0.1/api/org/junit/jupiter/api/Assertions.html CSE 373 SP 18 - Kasey Champion

Traversing Data Iterator! Array List for (int i = 0; i < arr.length; i++) { System.out.println(arr[i]); } List for (int i = 0; i < myList.size(); i++) { System.out.println(myList.get(i)); for (T item : list) { System.out.println(item); Iterator! CSE 373 SP 18 - Kasey Champion

Implement a CircularQueue (ArrayQueue) - What order should we implement in? - Test as we go! - The debugger is our friend! CSE 373 SP 18 - Kasey Champion

Iterators iterator: a Java interface that dictates how a collection of data should be traversed. Behaviors: hasNext() – returns true if the iteration has more elements next() – returns the next element in the iteration while (iterator.hasNext()) { T item = iterator.next(); } CSE 373 SP 18 - Kasey Champion

Implementing an Iterator Implement over the map we just made CSE 373 SP 18 - Kasey Champion

TODO list Homework 1 is live! Set up your development environment. - Individual Assignment - Try logging into gitlab.cs.washington.edu ASAP so we can help you early! - Due 6/29 at 11:59pm Start looking for partners for Homework 2! - Will be assigned on 6/29 - Sign up for Piazza – there’s a “Search for Teammates” pinned post Fill out the pre-class survey - Link is on the website CSE 373 SU 18 – Ben Jones