/* Homework 6 * * */ /* A Foo object is a pair(BinaryHeap element, element's location), * which is stored in the BInaryHeap hashtable with element value as the key * This is needed because a 'bucket' in the hashtable can contain more than * one element in the linked list. The address of a 'bucket' is the hashcode * of an element value. Now, more than one element can have the same hashcode. * Hence, more than one element location can end up in one bucket. Now, to find * out which location belongs to which element, we need to store the location * along with the element in a pair to avoid any error. The hashcode() method * calculates the hashcode of the element. The equals() method helps us to find * the right location for an element. Once a list is selected we compare the element * with the element of the (element, location) pair. When a match is found, we * return the location for that particular element. That is our answer. */ public class Foo { int location; // location in binary heap array Object element; // element from the heap // constructor to create a Foo without a location. public Foo(Object x){ element = x; location = -99; } // constructor sets the element and location with given values public Foo(Object x, int loc){ element = x; location = loc; } // uses the hashCode of the element stored in the Foo as the hash code of a Foo object public int hashCode(){ return element.hashCode(); } // equals method used to compare two Foo objects - makes sure only the element value is compared, // and not the location or the (value, location) pair public boolean equals(Object o){ if(o instanceof Foo){ if (this.element.equals(((Foo)o).element)){ return true; } } return false; } }