Answered step by step
Verified Expert Solution
Question
1 Approved Answer
URGENT Can someone help me solve this problem in java? T In this assignment, you will write a HashTable class and a TestHashTable class. The
URGENT Can someone help me solve this problem in java?
T
In this assignment, you will write a HashTable class and a TestHashTable class.
The HashItem Class
Here is the UML diagram for HashItem:
UML diagram showing attributes and methods for HashItem
There is no setKey method, because if the key were to change, it would be a different item altogether. I am providing this code for you. Here is the code that you will use. Download Here is the code that you will use. Do not modify this file!
The HashTable Class
Here is the UML diagram for HashTable.
UML diagram showing class name, properties, and methods
The properties are as follows:
nSlots
The number of slots in the table; this is set by the constructor.
size
The number of items in the table. This allows users to retrieve the total in an O operation.
slots
This is an ArrayList; that is an ArrayList of ArrayList. Each item in the outer ArrayList represents a slot; the inner ArrayList contains those HashItems with the corresponding hash code slot number
The properties are all public to make them easier to access outside the class.
The constructor takes one parameter: the number of slots in the HashTable. The constructor should use this parameter to build the initial slots attribute. You can leave the inner ArrayLists as null, or you can create an empty ArrayList for each slot. The former is quicker, the latter may make your other code a bit easier. The constructor also sets the size attribute to zero.
The private hashFunction method takes a key object and returns an integer in the range nSlots How can you get a hash code for any arbitrary object? The answer is to use the keys hashCode method. Every Object has this method built in; see the documentation Links to an external site.. Note: the builtin hashCode method may return a negative number.
The put method inserts the given key and value into the table if the key isnt already in the table. If the key already exists in the table, the value is changed. Remember that keys must be unique.
The get method takes a key as its parameter and returns the corresponding value, or null if the key is not in the table.
The private getItem method takes a key as its parameter and returns the corresponding keyandvalue HashItem, or null if the key is not in the table. This is an optional method. You do not have to implement it but you might find it to be very useful to avoid duplicated code in get and put
Finally, the getSize and getNSlots retrieve the number of items and number of slots in the table; toString returns a String representation of the hash table.
The TestHashTable Class
Write a Java class named TestHashTable with a main method that does the following:
Creates a HashTable named cityTable. Give it as many or as few slots as you like.
Opens a file of city information. Here is a large file Download Here is a large file, and here is a smaller file Download here is a smaller file that you can use for testing. Note: The large file has one city name that is duplicated on lines and This is probably a typographical error in the original file, but I am leaving it in because it is a good test to see that your put method works properly when given an entry that is already in the table.
Reads the file and enters each city and population into the table.
Prints the number of entries in the table.
Displays how many items are in each slot of the table.
Repeatedly asks the user for a city name. If the city is in the table, the program displays the population; otherwise, it says the city is not found.
Here is the starting point for the TestHashTable program. Download Here is the starting point for the TestHashTable program.
Sample Output
Heres the output from the program using slots I have omitted some lines to save space Your output does not have to match this example exactly, but it should contain similar information:
Entries in table:
Slot has items.
Slot has items.
Slot has items.
Slot has items.
Slot has items.
Slot has items.
Slot has items.
Type a city name, or press ENTER to quit: Paris
Paris has population
Type a city name, or press ENTER to quit: Duluth
Duluth not found.
Type a city name, or press ENTER to quit: Bangalore
Bangalore has population
Type a city name, or press ENTER to quit:
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