Queue Implemented with 1 2 numitems = enqueue (); 1 Queue Implemented with 2 numitems = 2 enqueue (); enqueue (); 2 Queue Implemented with 2 numitems = 2 enqueue (); enqueue (); enqueue (5); enqueue (6); enqueue (7); enqueue (8);
Queue Implemented with 5 6 7 8 numitems = 6 enqueue (); enqueue (); enqueue (5); enqueue (6); enqueue (7); enqueue (8); Queue Implemented with 5 6 7 8 numitems = 6 enqueue (); enqueue (); enqueue (5); enqueue (6); enqueue (7); enqueue (8); enqueue (9); 5 Queue Implemented with 9 5 6 7 8 numitems = 7 enqueue (); enqueue (); enqueue (5); enqueue (6); enqueue (7); enqueue (8); enqueue (9); 6
Data Members for Circular Array int ; // index of front of queue int ; // index of element of queue int numitems; // how many in queue int capacity; // how many queue can hold T[] values; // array for data 7 Implementing a Circular Array boolean isempty() { return numitems == 0; int size() { return numitems; 8 Implementing a Circular Array T getfirst() { return values[]; T removefirst() { if (numitems > 0) { T returnvalue = values[]; = ( + 1) % capacity; numitems--; return returnvalue return null; 9
Implementing a Circular Array void addlast (T item) { if (numitems == capacity) { reallocate(); // Like ArrayLists, grow if necessary = ( + 1) % capacity; values[] = item; numitems++; 10 Growing a Circular Array 9 10 5 6 7 8 Where should the data go? 9 10 5 6 7 8 11 Growing a Circular Array 9 10 5 6 7 8 Where should the data go? 5 6 7 8 9 10 12
Growing a Circular Array void reallocate() { int newcapacity = 2 * capacity; T[] newdata = new T[newCapacity]; int j = ; for (int i = 0; i < numitems; i++) { newdata[i] = values[j]; j = (j+1) % capacity; = 0; = numitems - 1; capacity = newcapacity; values = newdata; 1 Queue Abstraction Data structure that removes items in the same order they are added public interface Queue<T> { public void enqueue (T item); // Add an item to end public T front(); // Get item waiting the longest public T dequeue(); // Remove item waiting the longest public boolean isempty(); // Are any items waiting? public int size(); // How many items are waiting? Circular Array?? Linked List?? Abstraction! You can use a queue without knowing how it is implemented! 1 Queue in Collection Hierarchy <<interface>> Iterable <<interface>> Collection <<interface>> Queue <<interface>> List <<abstract>> AbstractCollection <<abstract>> AbstractList LinkedList ArrayList Stack 15
What was this course about anyway??? Assuring Correctness Algorithm Performance Algorithmic Problem Solving Fundamentals!! Program Decomposition Programming Concepts Java/Eclipse/JUnit Basic Data Structures Magic! NOT! 16 Java Topics Classes: methods, constructors, instance variables Contracts: method signatures, assert statements Interfaces, polymorphism, inheritance Swing - basics of painting, using components, attaching listeners Scope: instance variables, parameters, local variables Exception handling Basics of File I/O Iterators 17 Software Development Topics Contracts: preconditions and postconditions Recursion 18
Data Structure Topics O() analysis ArrayList Linked lists Stacks Queues Should know: How each represents data Methods supported by each class O() costs of those methods 19 Skills Knowing Java terminology: constructor, exception, object, class,... Reading Java code to determine what it does Recognizing good / bad style in a Java program Recognizing whether Java code follows the Java s subtyping rules Understanding a stack trace Writing very small amounts of Java code Showing how you would call a method given its signature or contract Identifying good test cases for a method Doing a O() analysis Designing a recursive algorithm 20 Summer Ideas Keep practicing! Still uneasy about Java? Head First Java Want to challenge yourself? Program an Android or iphone! Learn a new language, like Python! Read a book! Final Jeopardy - about Watson Effective Java Blown to Bits Linked: The New Science Of Networks Ted Talks, for example... What is the Internet, Really? The rise of human-computer cooperation 21
NullPointerException http://xkcd.com/71/ Object variables 22