Dear all,
I have a problem with my code. There are some bugs which I don't know how to fix!
The code is supposed to use the division method of hashing to store the data values into hash tables with different table sizes, and be able to search for a specific number. The linear probing is used to minimize the collisions. I used the methods provided in my course textbook (hash, add and get). So the methods are correct.
The compiler cannot compile my code and gives for the line:
tablesize7.add((Hashable)start);
the following error:
inconvertible types.
found: int; required: Hashtable.hashable
but I cannot define variable start as a Hashable. Right? Because when I do that, then the compiler cannot calculate the following:
return(element%max_elements);
Here's the code:
package Hashtable; public interface Hashable { int hash(); }
package Hashtable; public class HashDataset implements Hashable { static int element; static Hashable list[]; static int max_elements; protected int numElements = 0; static int tableSize; static int start; public int hash() { return(element%max_elements); } public void add (Hashable element) { int location = element.hash(); while(list[location]!=null) { location = (location + 1) % list.length; list[location]=element; numElements++; } } public Hashable get(Hashable element) { int location = element.hash(); while(list[location]!=element) location = (location + 1) % list.length; return (Hashable)list[location]; } public HashDataset(int start, Hashable list[], int tableSize) { element=start; this.list = list; max_elements = tableSize; } public static void main(String[] args) { HashDataset tablesize7 = new HashDataset(1500, new Hashable[150], 7); for(int i=0; i<100; i++) { tablesize7.hash(); tablesize7.add((Hashable)start); System.out.println(start); } } }