Answered step by step
Verified Expert Solution
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 DateHashTableint size
table new DatedEntrysize;
numElements ;
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 entryExistsDatedEntry entry
int index hashKeyentry;
return tableindex null && tableindexgetDateequalsentrygetDate;
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 storeEntryDatedEntry entry
int index hashKeyentry;
if entryExistsentry
tableindex entry; Replace existing entry with the same date
return true;
else if tableindex null
tableindex 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 getEntryString keyDate
Create a temporary entry for hashing
int index hashKeynew DatedEntrykeyDate;
return tableindex;
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 hashKeyDatedEntry entry
int month entry.getMonth;
int bucketStart bucketStartIdxmonth;
int bucketEnd bucketEndIdxmonth;
for int i bucketStart; i bucketEnd; i
int index i table.length; Ensure index stays within array bounds
if tableindex null tableindexgetDateequalsentrygetDate
Return index if slot is empty or contains entry with same date
return index;
return ; 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 getNumElementsInMonthint month
int count ;
int bucketStart bucketStartIdxmonth;
int bucketEnd bucketEndIdxmonth;
for int i bucketStart; i bucketEnd; i
if tablei 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 bucketStartIdxint month
return month tablelength ;
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 bucketEndIdxint month
if month
Last bucket ends at last index of the table
return table.length ;
else
int endIndex month tablelength ;
Ensure the endIndex doesn't exceed the table length
return Math.minendIndex table.length ;
This Code when in a grading scale gives the feedback and i dont know how to fix it:
DatedEntrytestDateHashTablejava, Line ArrayIndexOutOfBoundsException: overflow Janurary
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started