1.00 Tutorial 11 5/6/2002 Agenda • Final Exam • The Object class methods • PS10 – CharPair – Files – StringTokenizer – Exceptions – HashTable • Course evaluations 1 1.00 Final Exam • Will cover the entire term, but focus on more recent concepts • Wed, May 22 1:30-4:30pm in Johnson Athletic Center • Turn in your laptop either before the final or at the final (but arrive early!) The Object Class • Object class methods: public boolean equals (Object obj) Indicates whether another object is equal to this one. public String toString() Returns a string representation of the object. public int hashCode() Returns a hash code value for the object. 2 The equals() Method public class Student { int id; public Student(int id){ this.id = id; } public static void main(String args[]){ Student s1 = new Student(12); //student id is 12 Student s2 = new Student (12); //student id is 12 Student s3 = s1; s1.equals(s2); //default equals() method will return? s1.equals(s3); //default equals() method will return? } } QUESTION: how do you make equals() to return true if two students objects have the same ID? (Hint: override the default equals() method.) The equals() Method (contd) public class Student { int id; public Student(int id){ this.id = id; } public boolean equals (Object obj) { if (obj != null && obj instanceof Student) return id == ((Student) obj).id; else return false; } public static void main(String args[ ]){ Student s1 = new Student(12); //student id is 12 Student s2 = new Student (12); //student id is 12 Student s3 = s1; s1.equals(s2); //what does it return now? s1.equals(s3); //what does it return now? } } 3 The toString() Method public class Student { int id; public Student(int id){ this.id = id; } //we override the default toString() method public String toString() { return new String ("ID: " + id);} public static void main(String args[]) { Student s = new Student(12); System.out.println (s); //ID: 12 System.out.println ("Student " + s); //Student ID: 12” System.out.println (s.toString()); //ID: 12 } } PS#10 Problem 1 • CharPair class: – Constructor – toString(): returns a String suitable for printing – equals(): returns true if CharPairs’ data are equal. [Note: if two objects a and b are equal (a.equals(b) returns true), then their hashCodes must have the same value.] – hashCode(): returns reasonably uniform distribution of integers based on character data 4 PS#10 Problem 1 • Create a text file “indata.dat”. Something like: 3 50 2.718 3.141 GOOD This is the first string This is the second string • • • Declare File and BufferedReader as shown in Problem1. Read each line in using readLine(), then use StringTokenizer if there are more than one item on the line. Create CharPair objects CharPair cp1 = new CharPair(‘A’, ‘Z’); CharPair cp2 = new CharPair(‘1’, ‘9’); PS10 Problem1 • Example output to screen Read Read Read Read Integers: 3 50 Doubles: 2.718 3.141 chars: [G] [O] [O] [D] strings: "This is the first string" "This is the second string" Hashcode Hashcode Hashcode Hashcode for for for for String: Integer: Double: CharPair: -1847644 (value: "This is the first string") 3 (value: 3) -2001631442 (value: 2.718) 2293770 (value: AZ) CharPair.equals() test: AZ.equals("19") returns false double1 ^ integer1 = 20.079290231999998 integer2 ^ double2 = 7.135046699010218E24 CP AZ • Output new Doubles, one of the Strings, and new CharPairs to a file. 5 PS#10 Problem 1 • We are creating Integer and Double objects (not int and double primitives) • Use constructors that take Strings: • public Integer(String s); • public Double(String s); • Get Strings using StringTokenizer • Be sure to close file using file.close() StringTokenizer (1) import java.util.StringTokenizer; ... String s = “3.1415926 2.7182818”; StringTokenizer st = new StringTokenizer(s); String piString = st.nextToken(); // “3.14” Double d1 = new Double(piString); String eString = st.nextToken(); // “2.718” Double d2 = new Double(eString); 6 StringTokenizer (2) import java.util.StringTokenizer; ... String s = “this is a string”; StringTokenizer st = new StringTokenizer(s); while (st.hasNext()) { String oneToken = st.nextToken(); System.out.println(oneToken); } // what will this code print out? PS#10 Exceptions (prob 1) try { // open indata.dat, read data, etc. // close indata.dat // open outdata.dat, write data // close outdata.dat } catch (IOException e) { System.out.println(“Exception: “ +e.getMessage()); e.printStackTrace() } 7 PS10 Problem 2 SpellCheck dictionary.dat the of to and a in is it you that he ... dictionary2.dat miniscule minascule millenium noticable embarassment ocurr ocurred occured .... key, value identical minuscule minuscule millennium noticeable embarrassment occur occurred occurred key is wrong spelling, value is correct spelling. PS10 Problem2 testfile.dat This is the third and final file, which is to be spell-checked by your program output those to a file preserving line breaks as they ocurr in the file be sure that words ending in s or es are properly spell-checked it works it is working it worked it fails it is failing it failed Now do the questionaires without harrassing other people Teh main point i 8 PS10 Problem2 checked.dat this is the THIRD and FINAL FILE, which is to be SPELL-CHECKED by your PROGRAM the MAIN point is OUTPUT THOSE to a FILE PRESERVING line BREAKS as they occur in the FILE be SURE that words ending in S or ES are PROPERLY SPELL-CHECKED it works it is working it worked it FAILS it is FAILING it FAILED now do the questionnaires WITHOUT harassing other people • If a word is misspelled, and you know how to correct it, output correct spelling. • If a word is misspelled, you don’t know how to correct it, output it in ALL CAPS. • If the word is spelled correctly, output ‘as is’. PS#10: HashTable Bucket index is the hash code of the key bucket [0] Contents of a bucket are a collection of objects whose keys hash to the same value (0 in this case). Can be a linked list, array, Vector, binary tree, etc. Obj 1 Obj 2 null bucket [1] bucket [2] … bucket [n] Obj 2: key Obj 2: value In general, objects in the hash table have two components: a key, and a value. The hash code is computed on the key. The value is the data that is stored. In some cases, the key and value will be the same. 9 PS#10: SimpleMap public interface SimpleMap { public void clear(); public int dataSize(); public int tableSize(); public Object get( Object key ); public Object remove( Object key ); public Object put( Object key, Object value ); } PS#10 Put into HashTable HashTable t = new HashTable(); t.put(“Curtis”, “Curtis”) key value HashTable 1. “Curtis”.hashCode() returns 2029716094 2. Size of hash table (# buckets) is 5 3. Bucket index = 2029716094 % 5 = 4 (our “hash2” function uses % hashtablesize to assign bucket index.) 4. Store key and value pair in bucket[4] bucket [0] bucket [1] bucket [2] bucket [3] bucket [4] Key: “Curtis” Value: “Curtis” 10 PS#10: get( ) from HashTable • Invoke get(key) on HashTable to retrieve previously stored value. get() returns an Object, so we must cast to String String s=(String)t.get(“Curtis”); // s equals “Curtis” PS#10 SpellChecker Hints • Just create one class SpellChecker • Constructor can: – Create HashTable – Read in dictionary files & add to HashTable (how would you do this?) – Close files – Open input text file (testfile.dat) – Open output text file (checked.dat) – Read in one line of input file. For each line in the file • For each word in the line – Spellcheck each word (write a spellCheckWord method that returns the correct spelling of the argument) – Output the word • Output a newline – Close all files 11 PS#10 spellCheckWord • Use JavaDoc to investigate String methods: – String.length() – String.substring() – String.toUpperCase() • What design should you use for checking endings (“es”, “s”, “ing”, “ed”)? Evaluations • • • • J. Harward is 2nd instructor Your TA is 3rd instructor Questions 25-32 on next slide Student volunteer takes completed form to 1-281 or 1-290 12 Evaluation: Q’s 25-32 1=worst, 7=best 25. Core concepts of software development (program design, development, debugging) 26. Programming in an interactive, object oriented environment in Java 27. Use of computation for scientific and engineering problems 28. Graphical user interfaces (Java Swing, event model) 29. Data structures (stacks, queues, trees, lists) 30. Algorithms (sorting, searching, traversing) 31. Software patterns (model-view-controller, problem generator-solver-outputgenerator) 32. Use of Java packages (built-in math, utilities, data structures) 13