CSC223, Class 19: Swing (1) Admin: * Homework questions? * What methods should we test for Vectors? * Nullary constructor * Both versions of add * addElement * capacity * clear * contains * get * indexOf (both versions) * insertElementAt * remove (all versions) * Can we assume the other methods work correctly, because that helps us test? * Yes! * Did anyone do the reading? (Barely) * People named Sam rule! * Sorry, few details in today's outline. * Fun with Prospies! * What is your name? * Where are you from? * What is your favorite cliche' and why? * Ryan; New Jersey; "I have no idea" (because he rarely has ideas) * Vanessa; Texas; "I can't think of any cliche's right now" (c'mon, it's 9 a.m.) * Sarah; St. Louis; * Antoine; Shampoo-Banana; Every cloud has a silver lining. (Because it's false) * Haven't rearranged outlines Overview: * Hash tables * Swing basics * Sample programs * Some design issues * Exercise (if time) /Hash Tables/ * A hash table is two-dimensional array in which each spot is occupied by a value. (Gene Concurs.) * SamR hates this definition. * A collection of values in which values are stored by "key". * Similar to a one-dimensional array, except that the keys are objects rather than integers. * Sarah: An array is a way to store stuff. * Store values store("this value",at this numerical index) * Get values get(from this numerical index) store("Omondi", 4); students[4] = "Omondi"; store("Evan", 2); students[2] = "Evan"; * A hash table is also a way to store stuff, except that you use an index of some other type * When you search for a value, you search by that other kind of key firstNames.store("Martin", "Sam"); firstNames.store("Tang", "Sam"); firstNames.store("Gray", "Katherine"); ... firstName.get("Gray"); Problem in the design of hash tables in Java: * How do you implement these things? * Use an array (indexed by number) * We write a hash function that converts keys to numbers. * We reduce the number by the size of the array (using modulus) * We put the value in that position in the array * Handle conflicts using stuff that you should know * How do you decide if two keys are the same? * You use some equality test Morals for you as Java programmers * Whenever you write an object that serves as a key * write a hash method (Standard: hashCode()) * If you don't write a hash method, the hashCode method gives the address of the object (different objects will have different hashCodes) * Your hash table probably won't work. * The memory location is a bad thing to use as a key. * If you construct two "identical" objects, they will have different hashCodes. If you insert using the first, you won't find it using the second. firstNames.insert("Martin", "Sam"); ... lname = keyboard.readLine(); pen.println(lname + "'s first name is : " + firstNames.get(lname); * write an equals method (Standard: equals(Object other)) * If you don't write an equals method, you get "do they occupy the same area of memory?" * What happens when you see multiple things with the same hash code? * Two identical things won't be noted as identical unless you provdie equals * What happens to equals and hashCode if my object changes? * If you get different hashCodes when objects change, * Don't use them as keys or * Don't let your objects change (make them immutable) /Swing/ * Swing is Java's current Graphical User Interface toolkit * Two preceding GUI kits * Java 1.0 * Java 1.1: AWT (better event model) * Java 1.2 (Java 2): Swing (better something) * Java 1.3-Java 1.4 (Java 2): Still Swing * Java 1.5 (Java 5): Still Swing * Swing is based closely on AWT * Important issues in Swing * GUI model: You put components in containers * E.g., create a button, add it to a container * The typical containers are JFrames and JPanels * The typical components are JButton, JScrollBar, JLabel, JMenu, and lots of other stuff * Event handling system * Objects listen for events (implement eventListener or mouseListener) * Listeners must attach themselves to objects with addListener * Start with some stupid method name (scheduleLater) * Parameter is a Runnable * A Runnable is something that can be run within a thread * When the Runnable is ready to execute, the run method gets called