JAVA hi this is my code can you help me fix the bug here and change arraylist to
ID: 3817401 • Letter: J
Question
JAVA
hi this is my code
can you help me fix the bug here and change arraylist to basic array plz
---------------------------------------------------------------------------------------------------------
Do not change anything in Entry and SimpleList Class
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 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);
}
}
SimpleList.java-----------------------------------------------------------------------------------------
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;
}
}
HashTable.java-------------------------------------------------------------------------------------
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
ArrayList<String> numbers = new ArrayList<> ();
//constructor
public HashTable(ArrayList<String> numbers){
this.numbers = numbers;
}
public SimpleList generateSimpleList(){
SimpleList obj = new SimpleList();
for(int i = 0; i < numbers.size(); i++){
// add number
obj.add(new Entry(numbers.get(i)));
}
return obj;
}
private SimpleList generateHashTable(){
SimpleList obj = new SimpleList();
for(int i =0; i< numbers.size(); i++){
// add number
Entry entry = new Entry(numbers.get(i));
// get hash code
int hash = entry.hashCode(numbers.get(i), 1);
if(obj.getEntry(hash)!= null){ // get hash code success
obj.entries[hash] = entry;
}
else{
hash = entry.hashCode(numbers.get(i),2);
obj.entries[hash] = entry;
}
}
return obj;
}
// main function
public static void main(String args[]) throws IOException{
// read file
JFrame frame = new JFrame();
JFileChooser jfchooser = new JFileChooser();
ArrayList<String> list = new ArrayList<>();
// jfchooser.showOpenDialog(null);
jfchooser.showOpenDialog(frame);
File selFile = jfchooser.getSelectedFile();
FileReader reader= new FileReader(selFile);
int num = 1;
while (num != -1){
num = reader.read();
list.add(String.valueOf(num));
//System.out.println(list);
}
// creating object
HashTable obj = new HashTable(list);
obj.generateSimpleList();
obj.generateHashTable();
}
}
Explanation / Answer
Hi,
I have fixed following bugs in your code.
1. You were trying to access a private variable "entries" outside its class. You cannot access a private variable outside its class. Hence i have created its setter and getter methods in the SimpleList.java. I have not modified any logic.
2. I have also converted the Arraylist to Array along with its Constructor.
P.S : I have mentioned all my remarks in the code below....
CODE :
1. Entry.java :
package com;
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);
}
}
----------------------------------------------------------------------------------------------------------------------------------------------
2. SimpleList.java :
package com;
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.setEntries(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.getEntries().length) {
this.resize();
}
this.getEntries()[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.getEntries()[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.getEntries()[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.getEntries().length * 2];
for (int i = 0 ; i < this.size ; i++) {
newList[i] = this.getEntries()[i];
}
this.setEntries(newList);
}
public String toString() {
String result = "";
String formatter = "%-20s%-1d";
for (int i = 0 ; i < this.size ; i++) {
Entry e = this.getEntries()[i];
result += String.format(formatter, e.getWord(), e.getCount()) + " ";
}
return result;
}
// Change : You need to generate Setter and Getter for the " entries " variable as it is private.
// Inorder to access the variable outside the class we need its setter getter methods...
/**
* @return the entries
*/
public Entry[] getEntries() {
return entries;
}
/**
* @param entries the entries to set
*/
public void setEntries(Entry[] entries) {
this.entries = entries;
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------
3. Hashtable.java :
package com;
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(ArrayList<String> numbers){
this.numbers = numbers;
}
*/
// Change : Changed the constructor to accept the Array as a parameter..
public HashTable(String[] listArr) {
this.numbers = listArr;
}
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();
// Change : Checking array length instead of arraylist size
for(int i =0; i< numbers.length; i++){
// add number
// Change : Retrieving index based array element..
Entry entry = new Entry(numbers[i]);
// get hash code
int hash = entry.hashCode(numbers[i], 1);
if(obj.getEntry(hash)!= null){ // get hash code success
//Change : Setter and getter method created in SimpleList.java class for "entries" variable..
// The variable is declared as private and hence inorder to access it outside of its class, we have to use the getter method here.
obj.getEntries()[hash] = entry;
}
else{
hash = entry.hashCode(numbers[i],2);
obj.getEntries()[hash] = entry;
}
}
return obj;
}
// main function
public static void main(String args[]) throws IOException{
// read file
JFrame frame = new JFrame();
JFileChooser jfchooser = new JFileChooser();
ArrayList<String> list = new ArrayList<>();
// jfchooser.showOpenDialog(null);
jfchooser.showOpenDialog(frame);
File selFile = jfchooser.getSelectedFile();
FileReader reader= new FileReader(selFile);
int num = 1;
while (num != -1){
num = reader.read();
list.add(String.valueOf(num));
//System.out.println(list);
}
// Converted the Arraylist to Array...
String[] listArr = new String[list.size()];
listArr = list.toArray(listArr);
// creating object
HashTable obj = new HashTable(listArr);
obj.generateSimpleList();
obj.generateHashTable();
}
}
**********************************end**********************************************
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.