If anybody can help me with this assignment I would really appreciate it. Im so
ID: 3760387 • Letter: I
Question
If anybody can help me with this assignment I would really appreciate it. Im so stuck I can not understand the buckets, the String counter or anything in part 1. part 2 i kind of figure out but I really need coding help on part 1 It is not my biggest strong point for me. I have the three hash functions but i do not know how to put them in code for the program. I really do not know where to start and its due today at midnight and i been working on this for a week now. its hard doing this with 2 jobs and 2 boys so If anybody can help me out with code, advise, both, anything I would be real happy. Thanks!!
HW4: Crime and Punishment
Due 11:59PM 11/6/2015
For this assignment you will create a class for counting occurrences of
Strings. Effectively, this will be a hashmap from String to int .
Do not use java.util.HashMap.
Code must compile to be eligible for points.
10 point bonus: write a generic class that maps arbitrary keys to arbitrary values. Late work is not eligible for bonus points.
Part 1 requires you to do some analysis. This should appear in a plain text or PDF file – no Word documents or Excel files allowed.
The StringCounter (20 points)
Use the following class as a starting point for yours. Note that you are using the separate chaining collision resolution strategy, with a fixed table size – no rehashing is required. When you construct a new StringCounter you specify the table size and an object to use for hashing the strings.
Update : Note the chicanery required to construct an array of generic classes.
public class StringCounter
{
public interface HashFunction
{
int hash(String key);
}
private class Pair
{
public String key;
public int count;
}
private LinkedList<Pair>[] buckets;
private HashFunction hasher;
@SuppressWarnings("unchecked")
public StringCounter(int tableSize, HashFunction hasher)
{
buckets = (LinkedList<Pair>[]) new LinkedList<?>[tableSize];
this.hasher = hasher;
}
public void increment(String word)
{
}
public int getCount(String word)
{
}
public void printStatistics()
{
}
public String mostCommonWord()
{
}
}
Part 1 (40 points)
Write a program that uses this StringCounter to process all words in the provided wordsfile (this file is a spell checking dictionary for unix systems), and calls the printStatistics() method to print out the following pieces of information:
1. total number of buckets
2. number of used and unused buckets
3. average size of non-empty buckets
4. maximum bucket size
Using a table size of 235886, run this program 3 times, with the 3 different hash functions as listed
here. For each, collect the output of printStatistics():
1. Sum of ascii values of each Stringcharacter
2. The horner’s rule-based hash function for English words (the one with the
37 in it).
3. A hash function of your own design.
Describe your results. Which is best? How did your hash function work?
Part 2 (40 points)
Use your
StringCounter
to count the words in the novel “Crime and Punishment”.
Your program should print out the most common word in the novel, along with the number of times
it appears. It should then prompt the user to enter words, one per line, and print out the number
of times that word appeared in the novel.
Hints
Your keys should be case insensitive. Use the String method toLowerCase(). Use a java.util.Scanner to process the text file. The best way to break this file into individual words is to configure the Scanner with useDelimiter. I suggest using the delimiter “ [^a-zA-Z]+ ”.
This will cause theScanner to discard all non-alphabetic characters.
Explanation / Answer
Part 2
import java.awt.List;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class CandPHashSet
{
public static void main(String[] args) throws FileNotFoundException
{
Scanner file = new Scanner(new File("crime_and_punishment.txt"));
Set<String> crime = new HashSet<>();
while (file.hasNext())
{
crime.add(file.next().trim().toLowerCase());
}
System.out.println("Enter a word to search for: ");
Scanner input = new Scanner(System.in);
String search = input.next().toLowerCase();
if (crime.contains(search))
{
System.out.println("Yes");
}
else
{
System.out.println("No");
}