For each input word the program determines its home position with a hash functio
ID: 3803675 • Letter: F
Question
For each input word the program determines its home position with a hash function implemented in the program. Collision resolution should be implemented with the linear probing technique. If the home position is free, an object is put there that includes this word and a one-node linked list with the line number of the line on which the word was encountered. If the position is occupied by an object that already includes this word, add the line number to the list of line numbers in this object. If the home position is occupied by an object for another word, use linear probing to find a free cell if the word has been encountered for the first time or a cell with an object that already includes this word. During linear probing, start from the beginning of the hash table when its end has been reached. When all positions in the table are occupied, print a message and continue processing the file.
To sort the data in the hash table, count how many non-null entries in this table you have, create a new array of the size equal to the count, copy all entries from the hash table to the new array and sort this new array with
Arrays.sort(a); // see p. 19 of the textbook;
and then display it with
System.out.print(Arrays.toString(a)); // same page; and
ction 1.2 oBIECT ORIENTED PROGRAMMING IN JAVA 19 Arrays are passed to reference; that is, changes performed in a method that takes an array methods by For example, if a for doubling parameter affects the array permanently. values in integer arrays is define public void doubleArray (inti a) for (int i 0; i a. length it a [i] a [i] then the following code al 1 al [21 13 double Array(al) System out.print (Arrays. tostring (al) results in printing numbers 24, 26, and 26 In Java 1.2, the class Arrays is added with several useful methods to be applied to arrays, in particular binarysearch equals fill and sort In ava 5, method hashCode(), deepHashCode(), deepEquals tostring deepTostring have been added, and in Java 6, the Arrays class has been amplified with methods copy of and copyo For example, to sort an array, it is enough to import class java.util.Arrays and execute one of the ver sions of the method sort, for example, Arrays sort (a);Explanation / Answer
public class HashTest
{
Static BufferedReader reader(String fileName)throws Exception{ }
public static void main(String [ ] args)throws Exception
{
if(args.length!!=2)
{
System.out.println("wrong no of aarguments ");
System.exit(1);
}
BufferedReader wordReader;
int tableSize=Integer.ParseInt(args[1]);
Hashtable table =new Hashtable(tableSize);
String word;
int wordCount=0;
wordReader=reader(args[0]);
do
{
try
{
word=wordReader.readLine();
}
catch (Exception e)...
if(word==nul)
{
break;
}
else
{
wordCount++;
table.put(word,new Integer(wordCount);
}
}
while(true);
wordReader=reader(args[0]);
long startTime =System.currentTimeMillis();
do
{
try
{
word=wordReader.readerLine();
}
catch(Exception e)...
if(word==null)
{
break;
}
else
{
boolean result=table.containsKey(word);
}
}while(true);
long finishTime=system.currentTimeMillis();
System.out.println("time to hash"+wordCount+"words is"+(finishTime-startTime)+"milliseconds");
table.printStatistics();
}
}
/* program to sort an array in hashtable*/
import java.util.ArrayList;
import java.util.collection;
import java.util.Hashtable;
import java.util.List;
public class Main
{
Public static void main(String args[ ])
{
Hashtable<String , String h> h= new Hashtable<String , String>();
h.put("x","Y");
h.put("m","n");
h.put("a","b");
for(String str:h.keySet())
{
System.out.println(str);
}
List<String> v= new ArrayList<String<String>(h.keySet());
Collections Sort(v);
for(String str : v)
{
System.out.println(str+" " +(String)h.get(str);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.