Usually - and particularly for your dossier, most likely - you will be making a hash table of objects, i.e. of instances of one of your "template" classes.
Also usually - and for your master aspects - you will use a separate class for the hash table.
And you'll usually use a GUI interface too.
So here's an example with the way you will usually implement a hash table.
******* But what is missing from this example is tie-breaking. ******* See the full example for that.
/Users/adelaide/Public/Netbeans - All JSR Projects/Hashing 2012/src/hashing2012package/HashGUI.java |
1 package hashing2012package; 2 3 /** 4 * 5 * @author John Rayworth 6 */ 7 public class HashGUI extends javax.swing.JFrame { 8 9 HashTable myHashTable = new HashTable(); 10 11 public HashGUI() { 12 initComponents(); 13 } 14 246 247 private void addButtonMouseReleased(java.awt.event.MouseEvent evt) { 248 249 LibraryBook book = new LibraryBook(bookTitleTF.getText(), Double.parseDouble(DDNumberTF.getText())); 250 myHashTable.addToHashTable(book); 251 bookTitleTF.setText(""); 252 DDNumberTF.setText(""); 253 254 } 255 256 private void searchButtonMouseReleased(java.awt.event.MouseEvent evt) { 257 258 LibraryBook bookResult = new LibraryBook(); 259 try { 260 bookResult = myHashTable.findName(bookSearchTF.getText()); 261 } catch (Exception e) { 262 System.out.println(e.getStackTrace()); 263 } 264 DDNumberResultTF.setText(bookResult.getDDNumber()+""); 265 266 }
/Users/adelaide/Public/Netbeans - All JSR Projects/Hashing 2012/src/hashing2012package/HashTable.java |
2 package hashing2012package; 3 4 /** 5 * 6 * @author John Rayworth 7 */ 8 public class HashTable { 9 10 11 private LibraryBook[] hashArray = new LibraryBook [5000]; 12 13 14 public void addToHashTable(LibraryBook bookToHash){ 15 int total = 0; 16 String nameToHash = bookToHash.getBookTitle(); 17 for(int i = 0; i < nameToHash.length(); i++){ 18 total += nameToHash.charAt(i); 19 } 20 int hashValue = total % 5000; 21 hashArray[hashValue] = bookToHash; 22 } 23 24 25 public LibraryBook findName(String stringToFind) throws Exception{ 26 int total = 0; 27 for(int i = 0; i < stringToFind.length(); i++){ 28 total += stringToFind.charAt(i); 29 } 30 int hashValue = total % 5000; 31 32 Exception e = new Exception(); 33 if(hashArray[hashValue] == null){ 34 throw e; 35 }else{ 36 return hashArray[hashValue]; 37 } 38 39 } 40 41 }
Scorll down if you think you need to see the LibraryBook template class.
/Users/adelaide/Public/Netbeans - All JSR Projects/Hashing 2012/src/hashing2012package/LibraryBook.java |
1 package hashing2012package; 2 3 /** 4 * 5 * @author John Rayworth 6 */ 7 public class LibraryBook { 8 9 10 private String bookTitle = "not set yet"; 11 private double DDNumber = -999.999; 12 13 public LibraryBook(){ 14 15 } 16 17 public LibraryBook(String bookTitle, double DDNumber){ 18 this.bookTitle = bookTitle; 19 this.DDNumber = DDNumber; 20 } 21 22 public String getBookTitle(){ 23 return bookTitle; 24 } 25 26 public double getDDNumber(){ 27 return DDNumber; 28 } 29 30 public void setBookTitle(String bookTitle){ 31 this.bookTitle = bookTitle; 32 } 33 34 public void setDDNumber(double DDNumber){ 35 this.DDNumber = DDNumber; 36 } 37 38 } 39 40