JAVA This is my code..... and it is a word count generator but I havent finished
ID: 3821508 • Letter: J
Question
JAVA
This is my code..... and it is a word count generator
but I havent finished yet... Can you add Measure RunTime by using
java.time.Duration;
java.time.Instant
for both SimPleList and HashTable result
and make it wring on 2 .txt file for HashTable and 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.)
and adjust my 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.
but Do not alter SimpleList & Entry class
-----------------------------------------------------------------------
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 result = "";
result += "Word: " + this.word + " " +
"Count: " + this.count;
return result;
}
public int hashCode(String str, int type) {
//You must implement this method!!!!
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.
private 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.FileReader;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
public class HashTable {
//variable
String[] numbers = null;
//constructor
public HashTable(String[] numbers){
this.numbers = numbers;
}
public SimpleList generateSimpleList(){
SimpleList obj = new SimpleList();
for(int i = 0; i < numbers.length; i++){
// add number
obj.add(new Entry(numbers[i]));
}
return obj;
}
private SimpleList generateHashTable(){
SimpleList obj = new SimpleList();
Entry[]elist = new Entry[numbers.length];
for(int i =0; i< numbers.length; i++){
// add number
Entry entry = new Entry(numbers[i]);
// get hash code
int hash = entry.hashCode(numbers[i], 1);
if(elist[hash]!= null){ // get hash code success
elist[hash] = entry;
}
else{
hash = entry.hashCode(numbers[i], 2);
elist[hash] = entry;
}
}
for(int i =0; i < numbers.length; i ++){
obj.add(elist[i]);
}
return obj;
}
// main function
public static void main(String args[]) throws IOException{
// read file
JFrame frame = new JFrame();
JFileChooser jfchooser = new JFileChooser();
//ArrayList list = new ArrayList<>();
// jfchooser.showOpenDialog(null);
String[] list = new String[10];
int count = 0;
jfchooser.showOpenDialog(frame);
File selFile = jfchooser.getSelectedFile();
FileReader reader= new FileReader(selFile);
Integer num = 1;
num = reader.read();
while (num != -1){
//num = reader.read();
//list.add(String.valueOf(num));
//System.out.println(list);
if(count >= list.length){
String[] newList = new String[count *2];
for(int i = 0; i< count; i++){
newList[i] = list[i];
}
list = newList;
}
list[count] = num.toString();
num = reader.read();
count++;
}
// creating object
HashTable obj = new HashTable(list);
obj.generateSimpleList();
obj.generateHashTable();
}
}
Explanation / Answer
java.time.Duration;
java.time.Instant
public class ElapsedTime extends SimpleList,HashTable{
public static void main(String[] args) throws InterruptedException,ArithmeticException {
/ read file
JFrame frame = new JFrame();
JFileChooser jfchooser = new JFileChooser();
jfchooser.showOpenDialog(null);
jfchooser.showOpenDialog(frame);
File selFile = jfchooser.getSelectedFile();//selected file is word.txt containing word data
String[] list = new String[10];
int count = 0;
SimpleList sw=new SimpleList();
SimpleList sc=new SimpleList();
Scanner sc2 = null;
ElapsedTime et1=new ElapsedTime();
long startTime1 = System.nanoTime();
HashTable obj1 = new HashTable(sw);
obj1.generateSimpleList();
obj1.generateHashTable();
Thread.sleep(1000 * 100);
long difference = System.nanoTime() - startTime1;
System.out.println("Total execution time: " + String.format("%d min, %d sec", TimeUnit.NANOSECONDS.toHours(difference), TimeUnit.NANOSECONDS.toSeconds(difference) - TimeUnit.MINUTES.toSeconds(TimeUnit.NANOSECONDS.toMinutes(difference))));
String tmp,cmp;
for (int i = 0;i <sw.size() ;i++)
{
tmp = sw.getEntry[i];
for (int j = 0;j < sw.size();j++)
{
if (i == j) continue;
int x = tmp.compareTo(sw.getEntry[j]);
if (x < 0)
{
tmp = sw.getEntry[j];
sw.getEntry[j] = sw.getEntry[i];
sw.getEntry[i] = tmp;
}
}
}
for (int i = 0;i < sw.size();i++){
System.out.println("word entry"+sw.getEntry[i]);}
//sw.resize();
//writing sorted entries to output file
outStream.write(sc.getEntry(k));
}
ElapsedTime et2=new ElapsedTime();
long startTime2 = System.nanoTime();
while (num != -1)
{
sc.add(String.valueOf(num));
System.out.println(sc);
if(count >= sc.length){
String[] newList = new String[count *2];
for(int i = 0; i< count; i++){
newList[i] = sc[i];
}
sc = newList;
}
sc[count] = num.toString();
count++;
}
HashTable obj2 = new HashTable(sc);
obj2.generateSimpleList();
obj2.generateHashTable();
Thread.sleep(100 * 100);
long difference = System.nanoTime() - startTime2;
System.out.println("Total execution time: " + String.format("%d min, %d sec", TimeUnit.NANOSECONDS.toHours(difference), TimeUnit.NANOSECONDS.toSeconds(difference) - TimeUnit.MINUTES.toSeconds(TimeUnit.NANOSECONDS.toMinutes(difference))));
for (int i = 0;i <sc.size() ;i++)
{
cmp = sc.getEntry[i];
for (int j = 0;j < sc.size();j++)
{
if (i == j) continue;
int x = cmp.compareTo(sc.getEntry[j]);
if (x < 0)
{
cmp = sc.getEntry[j];
sc.getEntry[j] = sc.getEntry[i];
sc.getEntry[i] = cmp;
}
}
}
for (int i = 0;i < sc.size();i++){
System.out.println("count entry"+sc.getEntry[i]);}
//sw.resize();
}
}
}
Now, let us look at the Load Factor and threshold parameters of the hash table.
Load factor of a hash table=number of elements/hash table length.
Rehashing involves allocating a new hashtable twice the size of the original one.We then have to renter each element of the old table into the new one using our hash function.
Our hash table code is,
public int hashCode(String str, int type) {
//You must implement this method!!!!
int hashValue;
if(type ==1){ // primary hash
hashValue = str.hashCode();
return hashValue %7;
}
// secondary hash function
hashValue = str.hashCode();
return 7 - (hashValue % 7);
}
}
{
float loadfactor;
int initialCapacity;//intial capacity of hash table
table.generateHashTAble
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.