Question 2: Hash Table Implementations of a Dictionary Read through all of the i
ID: 3914739 • Letter: Q
Question
Question 2: Hash Table Implementations of a Dictionary Read through all of the instructions for question 2 before beginning, and refer back to these instructions frequently to ensure you have completed all required details. In this question you will create two new implementations of a dictionary, to be used with the Scrabble word generator from Assignment 2. Both dictionary implementations will use a hash table to store the contents of the dictionary as described below. The first implementation will use open addressing, with double hashing to resolve collisons. The second implemen- tation will use separate chaining. Details specific to each hash table are listed in separate sections below. A modified version of the main class from Assignment 2 is provided. The provided file A402ProvidedCode.j contains code that will generate combinations of letters, calculate the points for each word, and determine a best It performs the search for the best word twice, once with each type of dictionary, and outputs the result of each search. Do not make any modifications to the provided code. You will only write the Dictionary classes (and any supporting classes they require) that will be used by the main method. The provided code uses the words.alpha.txt file from assignment 2 (obtained from https://github. com/duyl/english-vords). Place a copy of this file in your working directory. Begin by reviewing the provided code, noting how the dictionary classes are called in the main and supporting methods. In order to be compatible with the provided code, your dictionary classes must be named DictionaryOpen and DictionaryChain. Each dictionary class must have the following methods, which can be called from outside the dictionary class. Use additional (private) helper methods as appropriate . A constructor that accepts an integer indicating the initial size of the dictionary. That is, public DictionaryOpen (int size) for the dictionary using open addressing, and public DictionaryChain(int size) for the dictionary using separate chaining e public int getsize O: Return the number of words in the dictionary e public void insert (String nerword): Insert the given word into the dictionary. If a word already exists in the dictionary do not add itExplanation / Answer
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Map;
import java.util.Set;
/**
*
*/
/**
* @author 100
*
*/
public class Main {
/**
* @param args
*/
public static void main(String[] args) throws IOException {
DictionaryOpen dictionaryOpen= new DictionaryOpen(100);
System.out.println(dictionaryOpen.getSize());
dictionaryOpen.fill("data1.txt", 5);
dictionaryOpen.insert("john");
dictionaryOpen.search("saurabh");
dictionaryOpen.print();
/*****************************************/
DictionaryChain chain= new DictionaryChain(100);
System.out.println(chain.getSize());
chain.fill("data1.txt", 5);
chain.insert("john");
chain.search("saurabh");
chain.print();
}
}
***************************************
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.Hashtable;
import java.util.Map;
import java.util.Set;
/**
*
*/
/**
* @author 100
*
*/
public class DictionaryOpen {
private int size;
String[] wordArrStringsDictionary=null;
private Map dictionary = new Hashtable();
/**
* @return the wordArrStringsDictionary
*/
public String[] getWordArrStringsDictionary() {
return wordArrStringsDictionary;
}
/**
* @param wordArrStringsDictionary the wordArrStringsDictionary to set
*/
public void setWordArrStringsDictionary(String[] wordArrStringsDictionary) {
this.wordArrStringsDictionary = wordArrStringsDictionary;
}
/**
* @return the dictionary
*/
public Map getDictionary() {
return dictionary;
}
/**
* @param dictionary the dictionary to set
*/
public void setDictionary(Map dictionary) {
this.dictionary = dictionary;
}
/**
* @return the size
* @throws IOException
*/
public int getSize() throws IOException {
String[] wordArrStrings = getWords();
this.wordArrStringsDictionary=getWords();
return this.wordArrStringsDictionary.length;
}
/**
* @param size the size to set
*/
public void setSize(int size) {
this.size = size;
}
public DictionaryOpen(int size) {
super();
this.size = size;
}
public void insert(String word) throws IOException
{
String[] wordArrStrings = getWords();
Map dictionary = new Hashtable();
for(int i=0;i<wordArrStrings.length;i++)
{
dictionary.put(wordArrStrings[i], i);
}
if(!dictionary.containsKey(word)) {
//Integer val = (Integer) dictionary.get(word);
dictionary.put(word,0);
}
Set<String> keys = dictionary.keySet();
wordArrStrings=null;
int k=0;
for(String key: keys)
{
wordArrStrings[k++]=key;
}
this.wordArrStringsDictionary=wordArrStrings;
}
public void fill(String fileName,int num) throws IOException
{
BufferedReader reader = new BufferedReader(new FileReader(new File(fileName)));
String[] wordArrStrings = getWords();
Map dictionary = new Hashtable();
for(int i=0;i<wordArrStrings.length;i++)
{
dictionary.put(wordArrStrings[i], i);
}
String inputLine = null;
int i=0;
while((inputLine = reader.readLine()) != null) {
// Split the input line.
String[] words = inputLine.split("\s+");
// Ignore empty lines.
if(inputLine.equals(""))
continue;
for(String word: words) {
// Remove any commas and dots.
word = word.replace(".", "");
word = word.replace(",", "");
if(!dictionary.containsKey(word)) {
//Integer val = (Integer) dictionary.get(word);
dictionary.put(word,i);
}
}
}
Set<String> keys = dictionary.keySet();
wordArrStrings=null;
int k=0;
for(String key: keys)
{
wordArrStrings[k++]=key;
}
this.wordArrStringsDictionary=wordArrStrings;
}
public boolean search(String wordToFind) throws IOException
{
boolean flag=false;
String[] wordArrStrings = getWords();
Map dictionary = new Hashtable();
for(int i=0;i<wordArrStrings.length;i++)
{
dictionary.put(wordArrStrings[i], i);
}
if(dictionary.containsKey(wordToFind)) {
//Integer val = (Integer) dictionary.get(word);
flag=true;
}
return flag;
}
public void print() throws IOException
{
String[] wordArrStrings = getWords();
for(String word:wordArrStrings)
{
System.out.println(word);
}
}
public String[] getWords() throws IOException
{
BufferedReader reader = new BufferedReader(new FileReader(new File("data.txt")));
String inputLine = null;
int i=0;
Map dictionary = new Hashtable();
String[] wordArrStrings = null;
while((inputLine = reader.readLine()) != null) {
if(inputLine.equals(""))
continue;
wordArrStrings[i++]=inputLine;
}
this.wordArrStringsDictionary=wordArrStrings;
return this.wordArrStringsDictionary;
}
/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((dictionary == null) ? 0 : dictionary.hashCode());
result = prime * result + size;
result = prime * result + Arrays.hashCode(wordArrStringsDictionary);
return result;
}
/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
DictionaryOpen other = (DictionaryOpen) obj;
if (dictionary == null) {
if (other.dictionary != null)
return false;
} else if (!dictionary.equals(other.dictionary))
return false;
if (size != other.size)
return false;
if (!Arrays.equals(wordArrStringsDictionary,
other.wordArrStringsDictionary))
return false;
return true;
}
}
************************************************
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.Hashtable;
import java.util.Map;
import java.util.Set;
/**
*
*/
/**
* @author 100
*
*/
public class DictionaryChain {
private int size;
String[] wordArrStringsDictionary=null;
private Map dictionary = new Hashtable();
/**
* @return the wordArrStringsDictionary
*/
public String[] getWordArrStringsDictionary() {
return wordArrStringsDictionary;
}
/**
* @param wordArrStringsDictionary the wordArrStringsDictionary to set
*/
public void setWordArrStringsDictionary(String[] wordArrStringsDictionary) {
this.wordArrStringsDictionary = wordArrStringsDictionary;
}
/**
* @return the dictionary
*/
public Map getDictionary() {
return dictionary;
}
/**
* @param dictionary the dictionary to set
*/
public void setDictionary(Map dictionary) {
this.dictionary = dictionary;
}
/**
* @return the size
*/
public int getSize() {
return size;
}
/**
* @param size the size to set
*/
public void setSize(int size) {
this.size = size;
}
public DictionaryChain(int size) {
super();
this.size = size;
}
public void insert(String word) throws IOException
{
String[] wordArrStrings = getWords();
Map dictionary = new Hashtable();
for(int i=0;i<wordArrStrings.length;i++)
{
dictionary.put(wordArrStrings[i], i);
}
if(!dictionary.containsKey(word)) {
//Integer val = (Integer) dictionary.get(word);
dictionary.put(word,0);
}
Set<String> keys = dictionary.keySet();
wordArrStrings=null;
int k=0;
for(String key: keys)
{
wordArrStrings[k++]=key;
}
this.wordArrStringsDictionary=wordArrStrings;
}
public void fill(String fileName,int num) throws IOException
{
BufferedReader reader = new BufferedReader(new FileReader(new File(fileName)));
String[] wordArrStrings = getWords();
Map dictionary = new Hashtable();
for(int i=0;i<wordArrStrings.length;i++)
{
dictionary.put(wordArrStrings[i], i);
}
String inputLine = null;
int i=0;
while((inputLine = reader.readLine()) != null) {
// Split the input line.
String[] words = inputLine.split("\s+");
// Ignore empty lines.
if(inputLine.equals(""))
continue;
for(String word: words) {
// Remove any commas and dots.
word = word.replace(".", "");
word = word.replace(",", "");
if(!dictionary.containsKey(word)) {
//Integer val = (Integer) dictionary.get(word);
dictionary.put(word,i);
}
}
}
Set<String> keys = dictionary.keySet();
wordArrStrings=null;
int k=0;
for(String key: keys)
{
wordArrStrings[k++]=key;
}
this.wordArrStringsDictionary=wordArrStrings;
}
public boolean search(String wordToFind) throws IOException
{
boolean flag=false;
String[] wordArrStrings = getWords();
Map dictionary = new Hashtable();
for(int i=0;i<wordArrStrings.length;i++)
{
dictionary.put(wordArrStrings[i], i);
}
if(dictionary.containsKey(wordToFind)) {
//Integer val = (Integer) dictionary.get(word);
flag=true;
}
return flag;
}
public void print() throws IOException
{
String[] wordArrStrings = getWords();
for(String word:wordArrStrings)
{
System.out.println(word);
}
}
public String[] getWords() throws IOException
{
BufferedReader reader = new BufferedReader(new FileReader(new File("data.txt")));
String inputLine = null;
int i=0;
Map dictionary = new Hashtable();
String[] wordArrStrings = null;
while((inputLine = reader.readLine()) != null) {
String[] words = inputLine.split("\s+");
if(inputLine.equals(""))
continue;
for(int j=0;j<words.length;j++) {
wordArrStrings[j]=words[i];
}
}
this.wordArrStringsDictionary=wordArrStrings;
return this.wordArrStringsDictionary;
}
/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((dictionary == null) ? 0 : dictionary.hashCode());
result = prime * result + size;
result = prime * result + Arrays.hashCode(wordArrStringsDictionary);
return result;
}
/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
DictionaryChain other = (DictionaryChain) obj;
if (dictionary == null) {
if (other.dictionary != null)
return false;
} else if (!dictionary.equals(other.dictionary))
return false;
if (size != other.size)
return false;
if (!Arrays.equals(wordArrStringsDictionary,
other.wordArrStringsDictionary))
return false;
return true;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.