This is all i got for the question for this Java ( Hash table implementation ) u
ID: 3813729 • Letter: T
Question
This is all i got for the question
for this Java (Hash table implementation) use this Entry.java and SimpleList.java to read a text file which do not have punctuation( no integer or punctuation mark)
You may not use any built-in Java data structures. This includes ArrayList, Map, Set, among other.
The only data structure that you can use in your implementation is a basic array.
you will be implementing a word count generator using a text file without punctuation( no integer or punctuation mark). You will implement one solution using a SimpleList and another solution using your own custom designed HashTable. Your program should parse the given text file and generate a histogram of the counts of each word in the text file. Be sure to deal with different letter cases in your implementation, (i.e. Java, JAVA, java should all be considered the same word). Use the Entry class given to store a word and its respective word count. You will need to implement the hashCode() function of the Entry class. This function should generate a hashCode() based on the word of the Entry. Please implement JFileChooser to select the input file.
-------------------------------------------------------------------------------------------------
HashTable Guidelines
Implement your HashTable (Please call the class HashTable) in any way you see fit.
Your table should keep track of a Load Factor. When the computed load factor is higher than the threshold you decide upon, you will need to resize and rehash your table.
Think very carefully about what the initial size of the table should be, the initial size is not allowed to be anything over 20 to start.
When you have to resize your table, think very carefully about what the new size should be.
You may NOT use separate chaining.
For open addressing you can use linear or quadratic probing, or double hashing.
-----------------------------------------------------------------------------------------
Hash Functions
For the primary hash function ( h(word) ), use the formula for hashing a string (from the lecture notes). Use 31 for the value of b in the formula.
For the secondary hash function ( h`(word) ) (if you choose to use double hashing). Take the result of the first hash function and use it in the following formula:
h(word) gives you the result of the primary hash function from the lecture notes.
h`(word) = 7 - (h(word) % 7)
Think very carefully where the primary (and optionally secondary) hash functions should go.
------------------------------------------------------------------------------------------
Test Class Requirements
Please provide a HashTableTest class with the following two static methods.
Please provide a method called generateSimpleList() which takes no parameters and returns the unsorted, populated SimpleList.
Please provide a method called generateHashTable() which takes no parameters and returns the unsorted, populated HashTable.
------------------------------------------------------------------------------------------
Results (Output)
The program should generate a text file that includes the output of your populated SimpleList. It should very cleanly show the data of each entry (word and its count). The output should be sorted alphabetically. (Do not include the sorting in the time measurement.)
The program should generate a second text file that includes the output of your populated HashTable. It should very cleanly show the data of each entry (word and its count). The output should be sorted alphabetically. (Do not include the sorting in the time measurement.)
The program should calculate the time it takes each implementation (SimpleList vs HashTable) to complete the population.
NOTE: For accurate measurements you cannot process both populations at the same time. You will need to do two separate file reads / processing to give a fair and accurate measurement.
Be sure that you do not perform any console writing or file writing in the methods which generate the SimpleList and HashTable. This will slow down your algorithm significantly.
The program should also show the number of entries in the populated SimpleList and the number of entries in the populated HashTable.
------------------------------------------------------------------------------------------
please use to measure runtime
java.time.Duration;
java.time.Instant;
example:
------------------------------------------------------------------------------------------
Entry.java
plz read carefulm only implement in last method
------------------------------------------------------------------------------
and also this one
-----------------------------------------------------------------------------
Explanation / Answer
Entry.java
public class Entry {
//The word to store in the Entry, this will be a key value for your hash table.
private String word;
//The count of how many times the word appears.
private int count;
/**
* Constructor that creates an {@code Entry} object given a word.
*
* @param word The word to set in the {@code Entry}.
*/
public Entry(String word) {
this.word = word;
this.count = 1;
}
/**
* Returns the word of this {@code Entry}.
*
* @return The word of this {@code Entry}.
*/
public String getWord() {
return this.word;
}
/**
* Returns the count of how many times this word appears in the document.
*
* @return the word count.
*/
public int getCount() {
return this.count;
}
/**
* Increases the count of the word in this {@code Entry} by one.
*
*/
public void incrementCount() {
this.count++;
}
@Override
public String toString() {
String _res = "";
_res += "Word: " + this.word + " "
+ "Count: " + this.count;
return _res;
}
public int hashCode(String str, int type) {
int hashValue;
if (type == 1) { // primary hash
hashValue = str.hashCode();
return hashValue % 7;
}
// secondary hash function
hashValue = str.hashCode();
return 7 - (hashValue % 7);
}
}
------------------------------------------------------------------------------------------------------------------------------------------------------
public class SimpleList {
//Initial size of the internal array.
private static final int INITIAL_CAPACITY = 10;
//Internal array of Entry objects.
public Entry[] entries;
//Size of the List
private int size;
/**
* Constructor creates an empty {@code SimpleList} with default initial
* capacity.
*
*/
public SimpleList() {
this.entries = new Entry[INITIAL_CAPACITY];
this.size = 0;
}
/**
* This method adds a new {@code Entry} to the end of the list. The list
* will also be resized when necessary.
*
* @param e The entry to add to the end of the list.
*/
public void add(Entry e) {
//Check to see if we need to resize the list
if (this.size == this.entries.length) {
this.resize();
}
this.entries[this.size] = e;
this.size++;
}
/**
* This function finds the {@code Entry} in the list whose word matches the
* given {@code String}. The function returns the index of where the Entry
* can be found, or -1 if the {@code Entry} was not found.
*
* @param word The word whose {@code Entry} you want to find.
*
* @return Returns the index of where the {@code Entry} was found, -1
* otherwise.
*/
public int find(String word) {
for (int i = 0; i < this.size; i++) {
Entry current = this.entries[i];
if (word.equals(current.getWord())) {
return i;
}
}
return -1;
}
/**
* This method returns the {@code Entry} at the given index.
*
* @param index The index of the {@code Entry} to return. {@code index} must
* be a positive value between 0 to size()-1 inclusive.
* @return The {@code Entry} at the given index.
*/
public Entry getEntry(int index) {
return this.entries[index];
}
/**
* This method returns the number of entries in the list.
*
* @return The number of entries in the list.
*/
public int size() {
return this.size;
}
/**
* This method will create a new list double the size of the previous, and
* copy all values from the old list to the new list.
*/
private void resize() {
Entry[] newList = new Entry[this.entries.length * 2];
for (int i = 0; i < this.size; i++) {
newList[i] = this.entries[i];
}
this.entries = newList;
}
public String toString() {
String result = "";
String formatter = "%-20s%-1d";
for (int i = 0; i < this.size; i++) {
Entry e = this.entries[i];
result += String.format(formatter, e.getWord(), e.getCount()) + " ";
}
return result;
}
}
--------------------------------------------------------------------------------------------------------------------------------------------------------
import java.util.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
public class HashTableTest {
//variables
ArrayList<String> numbers = new ArrayList<> ();
//constructor
public HashTableTest(ArrayList<String> numbers) {
this.numbers = numbers;
}
public SimpleList generateSimpleList() {
SimpleList obj = new SimpleList();
for(int i=0;i<numbers.size();i++){
// adding numbers
obj.add(new Entry(numbers.get(i)));
}
return obj;
}
private SimpleList generateHashTable() {
SimpleList obj = new SimpleList();
for(int i=0;i<numbers.size();i++){
// adding numbers
Entry entry = new Entry(numbers.get(i));
// get hash code
int hash = entry.hashCode(numbers.get(i),1);
if(obj.getEntry(hash) != null){
obj.entries[hash] = entry;
}else{
hash = entry.hashCode(numbers.get(i),2);
obj.entries[hash] = entry;
}
}
return obj;
}
public static void main(String args[]) {
// reading file
JFileChooser file_chooser = new JFileChooser();
ArrayList<String> list = new ArrayList<>();
int _res = file_chooser.showOpenDialog(this);
if (_res == JFileChooser.APPROVE_OPTION) {
try {
FileReader sc = null;
JOptionPane.showMessageDialog(this, "Select the _myFile");
File _myFile = file_chooser.getSelectedFile();
sc = new FileReader(_myFile);
int num = 1;
while (num != -1) {
num = sc.read();
list.add(String.valueOf(num));
}
} catch (FileNotFoundException ex) {
} catch (IOException ex) {
}
}
// creating object
HashTableTest obj = new HashTableTest(list);
obj.generateSimpleList();
obj.generateHashTable();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.