CSC153, Class 50: Dictionaries Overview: * Notes on Exam 3 * Questions from Friday's class. * The Dictionary ADT: Basic Design Issues * Applying Dictionaries. * The Dictionary ADT: Central Methods * Dictionaries vs. Databases Notes: * Today is "Feed Arjun" day. Four types of Salsa! * Exam 3 returned. * Exam 4 questions? * Questions on heaps? ---------------------------------------- Notes on Exam 3 Problem 1: Defining Polymorphism * Citations * What is a definition? * What about interfaces? Problem 2: Weird Comparable Thing * Order an interface public interface Order { public boolean mayPrecede(Object alpha, Object beta) throws Exception; } * Student: a Class (data associated with it) public class Student { double gpa; } * OrderStudentsByGPA: Has a clear procedure implementation Class or Abstract Class public class OrderStudentsByGPA implements Order // -2 { public boolean mayPrecede(Object alpha, Object beta) throws Exception { if (!(alpha instanceof Student)) throw new Exception(alpha + " is not a student"); if (!(beta instanceof Student)) throw new Exception(beta + " is not a student"); return ((Student) alpha).gpa > ((Student) beta).gpa; } // boolean mayPrecede(Object, Object) public boolean mayPrecede(Object alpha, Object beta) throws Exception { return ((Student) alpha).gpa > ((Student) beta).gpa; } } // OrderStudentsByGPA ---------------------------------------- What's wrong with the following code? We'll draw pictures to help. public class BriansQueue implements Queue { // Fields Node first; Node last; public Object get() throws EmptyStructureExcepction { if (this.first == null) throw new EmptyStructureException; Object tmp = this.first.value; this.first = this.first.next; return tmp; } public void add(Object value) { this.last.next = new Node(value); this.last = this.last.next; } // add(Object) } // class BriansQueue public class BriansQueue implements Queue { // Fields Node first; Node last; // Side note: If the queue has one element, do // first and last point to the same node? YES // Problem: If we delete the only element in a // one-element queue, we need to set both first // and last to null. public Object get() throws EmptyStructureExcepction { if (this.first == null) throw new EmptyStructureException; Object tmp = this.first.value; this.first = this.first.next; if (this.first == null) this.last = null; return tmp; } // Question: // What does it mean that this.first and this.last are // the same? // this.first == this.last // Both point to the same object // this.first.equals(this.last) // It depends on how Node.equals is implemented // If the queue is empty, this.last is null. // this.last.next is some nasty exception. public void add(Object value) { if (this.last == null) { this.first = this.last = new Node(value); } else { this.last.next = new Node(value); this.last = this.last.next; } } // add(Object) } // class BriansQueue ---------------------------------------- What is a vector or an array? A collection that you can index by number Main operations: * get ith element * set ith element * get length Expectations that those are O(1) operations Are there collections you'd like to index, but by things other than numbers? * A traditional dictionary is a collection of definitions indexed by words. * We might index file folders by keyword * We might index Brian's software by color iSoftwareOrganizer iSockDrawer The computer science community has no standard term for collections indexed by values other than numbers. I call them Dictionaries. Some people call them "Keyed Tables". Java used to call them Dictionaries. Java now calls them Maps.