Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

/ * * * * @author blaze * / / * * * Represents a hash table for storing dated entries. * / public class

/**
*
* @author blaze
*/
/**
* Represents a hash table for storing dated entries.
*/
public class DateHashTable {
private DatedEntry[] table;
private int numElements; // Number of elements stored in the hash table
/**
* Constructs a DateHashTable with the specified size.
*
* @param size The size of the hash table.
*/
public DateHashTable(int size){
table = new DatedEntry[size];
numElements =0;
}
// Manage entries in the table
/**
* Checks if the given entry already exists in the hash table.
*
* @param entry The entry to check.
* @return true if the entry already exists, false otherwise.
*/
public boolean entryExists(DatedEntry entry){
int index = hashKey(entry);
return table[index]!= null && table[index].getDate().equals(entry.getDate());
}
/**
* Stores the given entry in the hash table.
*
* @param entry The entry to store.
* @return true if the entry is stored successfully, false otherwise.
*/
public boolean storeEntry(DatedEntry entry){
int index = hashKey(entry);
if (entryExists(entry)){
table[index]= entry; // Replace existing entry with the same date
return true;
} else if (table[index]== null){
table[index]= entry; // Store the entry in an empty slot
numElements++; // Increment the count of total entries stored
return true;
}
return false; // Unable to store entry
}
/**
* Retrieves the entry stored for the given key date.
*
* @param keyDate The key date to retrieve the entry for.
* @return The entry stored for the given key date, or null if not found.
*/
public DatedEntry getEntry(String keyDate){
// Create a temporary entry for hashing
int index = hashKey(new DatedEntry(keyDate,""));
return table[index];
}
/**
* Computes the hash key (index) for the given entry.
*
* @param entry The entry for which to compute the hash key.
* @return The computed hash key (index).
*/
public int hashKey(DatedEntry entry){
int month = entry.getMonth();
int bucketStart = bucketStartIdx(month);
int bucketEnd = bucketEndIdx(month);
for (int i = bucketStart; i <= bucketEnd; i++){
int index = i % table.length; // Ensure index stays within array bounds
if (table[index]== null || table[index].getDate().equals(entry.getDate())){
// Return index if slot is empty or contains entry with same date
return index;
}
}
return -1; // Unable to find suitable slot
}
// Other accessor methods
/**
* Retrieves the size of the internal table.
*
* @return The size of the internal table.
*/
public int getTableSize(){
return table.length;
}
/**
* Retrieves the number of elements stored in the hash table.
*
* @return The number of elements stored in the hash table.
*/
public int getNumElements(){
return numElements;
}
/**
* Retrieves the number of entries stored in the corresponding bucket for
* the given month.
*
* @param month The month for which to retrieve the number of entries.
* @return The number of entries stored in the corresponding bucket for the
* given month.
*/
public int getNumElementsInMonth(int month){
int count =0;
int bucketStart = bucketStartIdx(month);
int bucketEnd = bucketEndIdx(month);
for (int i = bucketStart; i <= bucketEnd; i++){
if (table[i % table.length]!= null){
count++;
}
}
return count;
}
/**
* Computes the starting index of the bucket for the given month.
*
* @param month The month for which to compute the starting index.
* @return The starting index of the bucket for the given month.
*/
public int bucketStartIdx(int month){
return (month -1)*(table.length /12);
}
/**
* Computes the ending index of the bucket for the given month.
*
* @param month The month for which to compute the ending index.
* @return The ending index of the bucket for the given month.
*/
public int bucketEndIdx(int month){
if (month ==12){
// Last bucket ends at last index of the table
return table.length -1;
} else {
int endIndex =(month *(table.length /12))-1;
// Ensure the endIndex doesn't exceed the table length
return Math.min(endIndex, table.length -1);
}
}
}
This Code when in a grading scale gives the feedback and i dont know how to fix it:
DatedEntry/test/DateHashTable.java, Line 32, ArrayIndexOutOfBoundsException: overflow Janurary and december and table.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2015 Porto Portugal September 7 11 2015 Proceedings Part 3 Lnai 9286

Authors: Albert Bifet ,Michael May ,Bianca Zadrozny ,Ricard Gavalda ,Dino Pedreschi ,Francesco Bonchi ,Jaime Cardoso ,Myra Spiliopoulou

1st Edition

3319234609, 978-3319234601

More Books

Students also viewed these Databases questions

Question

6. Describe why communication is vital to everyone

Answered: 1 week ago