And with Tie-breaking
This is the full example, including:
/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 LibraryBookHashTable myHashTable; 10 11 public HashGUI() { 12 initComponents(); 13 myHashTable = new LibraryBookHashTable(5000); 14 } 15 247 248 private void addButtonMouseReleased(java.awt.event.MouseEvent evt) { 249 250 LibraryBook book = new LibraryBook(bookTitleTF.getText(), Double.parseDouble(DDNumberTF.getText())); 251 if(myHashTable.addToHashTable(book)){//Note here a boolean is returned if the hashtable is not full. 252 bookTitleTF.setText(""); 253 DDNumberTF.setText(""); 254 }else{ 255 bookTitleTF.setText("Sorry, hash table full."); 256 DDNumberTF.setText("Sorry, hash table full."); 257 } 254 255 } 256 257 private void searchButtonMouseReleased(java.awt.event.MouseEvent evt) { 258 259 LibraryBook bookResult = new LibraryBook(); 260 bookResult = myHashTable.findName(bookSearchTF.getText()); 261 262 if(bookResult == null){ 263 DDNumberResultTF.setText("No book by that name."); 264 }else{ 265 DDNumberResultTF.setText(bookResult.getDDNumber()+""); 266 } 267 268 }
/Users/adelaide/Public/Netbeans - All JSR Projects/Hashing 2012/src/hashing2012package/LibraryBookHashTable.java |
1 package hashing2012package; 2 3 /** 4 * 5 * @author John Rayworth 6 */ 7 public class LibraryBookHashTable { 8 9 private LibraryBook[] hashArray; 10 private int hashTableLength; 11 private boolean isFull = false; 12 13 public LibraryBookHashTable(int hashTableLength) { 14 this.hashTableLength = hashTableLength; 15 hashArray = new LibraryBook[hashTableLength]; 16 for (int i = 0; i < hashTableLength; i++) { 17 hashArray[i] = new LibraryBook(); 18 } 19 } 20 21 private boolean isFull(){ 22 return isFull; 23 } 24 25 public boolean addToHashTable(LibraryBook bookToHash){ 26 27 int total = 0; 28 String nameToHash = bookToHash.getBookTitle(); 29 for (int i = 0; i < nameToHash.length(); i++) { 30 total += nameToHash.charAt(i); 31 } 32 int hashValue = total % hashTableLength; 33 int startingHashValue = hashValue; 34 while (!hashArray[hashValue].getBookTitle().equals("not set yet") && !isFull()) { 35 hashValue++; 36 if (hashValue == hashArray.length) { 37 hashValue = 0; 38 } 39 if(hashValue + 1 == startingHashValue){//The way to know if the hash table is full. 40 isFull = true; 41 } 42 } 43 System.out.println(hashValue); 44 if(!isFull()){ 45 hashArray[hashValue] = bookToHash; 46 return true; 47 } 48 else{ 49 return false;//not added because full 50 } 51 } 37 38 public LibraryBook findName(String stringToFind){ 39 int total = 0; 40 for (int i = 0; i < stringToFind.length(); i++) { 41 total += stringToFind.charAt(i); 42 } 43 int hashValue = total % hashTableLength; 44 45 if (!hashArray[hashValue].getBookTitle().equals(stringToFind)) { 46 int startingHashValue = hashValue; 47 while (!hashArray[hashValue].getBookTitle().equals(stringToFind)) { 48 hashValue++; 49 if (hashValue == hashArray.length) { 50 hashValue = 0; 51 } 52 if(hashValue == startingHashValue){ 53 return null; 54 } 55 } 56 } 57 return hashArray[hashValue]; 58 } 59 } 60 61
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