This is called a hash map. The following code shows a simple Java implementation
ID: 3639298 • Letter: T
Question
This is called a hash map. The following code shows a simple Java implementation of a HashMap class (parts of the code is missing, you are to complete it)./**
* class HashMap
*
*/
public class HashMap
{
public int CAPACITY = 20;
public Object[] keys = new Object[CAPACITY];
public Object[] values = new Object[CAPACITY];
public String[] status = new String[CAPACITY];
/**
* Initialize this HashMap object to be empty.
*/
public HashMap()
{
for (int i=0;i<CAPACITY;i++)
{
status[i] = "empty";
keys[i] = "empty";
values[i] = null;
}
}
/**
* Determines if this object contains no elements
*
* @return true - if this object contains no elements
*/
public boolean isEmpty()
{
int count = 0;
for (int i=0;i<CAPACITY;i++)
{
if (status[i] == "occup")
count++;
}
if (count==0) return true;
else return false;
}
/**
* Determines the number of elements
*
* @return the number of elements */
public int size()
{
int count = 0;
for (int i=0;i<CAPACITY;i++)
{
if (status[i] == "occup")
count++;
}
return count;
}
/**
* Puts a new key/value pair in this HashMap
* Calculates array position from hashcode, and rehashes
* if that position is occupied
*
* @param key - the key to be stored
* @param value - the value to be stored
*/
public void put(Object key, Object value)
{
int hashcode = Math.abs(key.hashCode());
*******************
System.out.println("Hashcode calculated:" + hashcode);
if (status[hashcode]=="occup")
{
*******************
System.out.println("Hashcode recalculated:" +
hashcode);
}
keys[hashcode] = key;
values[hashcode] = value;
status[hashcode] = "occup";
}
/**
* Checks whether the specifed key is present
*
* @param key - the key to be checked
* @return true - if the key is present
*/
public boolean containsKey(Object key)
{
boolean found = false;
*******************
*******************
return found;
}
/**
* Returns the value for a specified key
* Uses hashcode to select starting point for search,
* and uses linear probing to search from there
*
* @param key - the key to be found
* @return the value associated with key
*/
public Object get(Object key)
{
int hashcode = Math.abs(key.hashCode());
*******************
System.out.println(hashcode);
int attempts = 0;
if (keys[hashcode].equals(key))
return values[hashcode];
else
{
while (!keys[hashcode].equals(key) &&
status[hashcode] != "empty" && hashcode <=
CAPACITY && attempts < 10)
{
hashcode++;
if (status[hashcode] == "occup")
{
if(keys[hashcode].equals(key))
return values[hashcode];
}
else
attempts++;
}
return null;
}
}
/** * Recursively recalculates array position to find * unoccupied position, based on linear probing method
* Checks whether allowed number of attempts has been
* exceeded.
*
* @param hashcode - the hashcode of an object
* @param attempts - the number of attempts allowed
* @return the new array position
*/
private int rehash(int hashcode, int attempts)
{
if (hashcode < CAPACITY && attempts < CAPACITY)
{
hashcode++;
if (hashcode==CAPACITY)
hashcode = 0;
System.out.println("rehashing:" + hashcode);
if (status[hashcode] == "occup")
{
hashcode = rehash(hashcode,attempts+1);
}
return hashcode;
}
else
return -1;
}
}
Looking at a HashMap
Create a new NetBeans project called Homework9, and add a new class HashMap using the above completed code. Add new classes Car and CarInventory using the following uncompleted code:
/**
* class Car
*
*/
public class Car
{
private String registration;
private String make;
/**
* Constructor for objects of class Car
*/
public Car(String registration, String make)
{
this.registration = registration;
*******************
this.model = model;
}
}
/**
* class CarInventory
*
*/
public class CarInventory
{
public HashMap map;
/**
* Constructor for objects of class CarInventory
*/
public CarInventory(HashMap map)
{
this.map = map;
}
/**
* Constructs a Car object and stores in the map
* using the registration number as the key
*
* @param reg the registration number
* @param make the make
* @param model the model
*/
public void storeCar(String reg, String make)
{
Car car = new Car(reg, make);
*******************
}
/**
* Retrieves a Car object from the map
* using the registration number as the key
*
* @param reg the registration number
* @return the Car object found
*/
public Car findCar(String reg)
{
Car car = (Car) map.get(reg);
return car;
}
}
Create a driver main class that creates a new instance of HashMap called hashmap1. Create a new instance of CarInventory called carInventory1 and supply hashMap1 as the parameter for the constructor. You can now use carInventory1 to store and find Car objects in hashMap1, and also inspect the map directly.
a. (20 pts) Draw the UML “compilable” Classes Diagram for the Homework9 Project.
ANSWER:
b. (50 pts) Adding Data In driver main class add the following elements then display the contents of the hash table:
Registration Make
"DF52TYU", "Audi"
"RT02GHT", "Volvo"
"FD52HBC", "Saab"
"DR02TRG", "Mini"
"TU02XYZ", "Renault"
"YU51TRH", "Hyundai"
Answer:
c. Finding Data In driver main class search for the following key:
YU51TRH
Answer:
d. Updating Data Add a method set (key, value) to your HashMap class to update the value object for a specified key. Modify your CarInventory class so that you can test this method, call it modifyCar (key, value). In driver main class call the modifyCar method of carInventory1 with “YU51TRH” and “Hyundai” and “Mercedes” as parameters.
Explanation / Answer
Hi! Cramster Terms & Conditions were changed recently (I hope you have read them). Now, experts have to send the answers to askers' inbox after you rate them. That is because of people copying from older threads which have public answers. Hence we can only send the answer after you rate. So please give me a Lifesaver rating and I'll send the solution to your inbox or e-mail. You've got no other option to get the answer, because no one can give you the answer over here. Even you might be banned for rating a user that gave the answer directly on your question. You need not worry as I have the solution ready in my notebook. Hope you rate me :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.