The data structure that you will individually implement for project one is aHashtable. You must develop the code for this assignment entirely on your own. If you encounter difficulties while working, you are encouraged to discuss general algorithms and debugging strategies with anyone you would like. If you feel that getting assistance will require someone else viewing your code, the course staff are the only people that you are allowed to share or show any part of your code with prior to the late deadline for this assignment. You may not store or share your code in any way that another students can access. And you are not allowed to view or make use of any Hashtable code that is not written entirely by you.
You must define a public class HashtableMap that implementsthis provided MapADT interfaceLinks to an external site.. Your implementation must:
be left in the default package: ie. do not define it within a named package.
be defined with two generic type parameters in this order: which allow it to be used with different types of key-value pairings.
include two constructors with the following signatures:
public HashtableMap(int capacity)
public HashtableMap() // with default capacity = 20
Remember that you cannot instantiate an array with associated generic types in Java. So you should instantiate without the generic types, and then cast to the complete type with generics. This will produce an unchecked cast warning that can either be ignored, or suppressed by adding the @SuppressWarnings("unchecked") annotation in front of your method's definition.
use exactly one private array instance field(update 10/1: this single array instance field should be one dimensional, and additional private primitive fields are allowed).
grow by doubling its capacity and rehashing, whenever its load factor becomes greater than or equal to 80%. Define private helper methods to better organize the code that does this.
store new values in your hash table at the index corresponding to { the absolute value of the key's hashCode() } modulus the HashtableMap's current capacity. When the put method is passed a key that is null or is equal to a key that is already in your hash table, that call should return false without making any changes to the hash table. The put method should only return true after successfully storing a new key-value pair.
include a remove method that returns a reference to the value associated with the key that is being removed. When the key being removed cannot be found in the HashtableMap, this method should instead return null.
include a size method that returns the number of key-value pairs stored in this collection, and include a clear method that removes all key-value pairs from this collection without changing the underlying array capacity.
use chaining to handle hash collisions, and make use of the java.util.LinkedLists class for this purpose. It will also be helpful to create a helper class that allows you to group together a key-value pair within a single object. Then you can store these key-value pair objects within the appropriate java.util.LinkedLists.
throw exceptions as indicated within the comments of the MapADT interface. You should make use of java.util.NoSuchElementException for this.
NOTmake use of any classes from libraries like java.util other than the two exceptions listed above: you may use java.util.LinkedList, and java.util.NoSuchElementException.
In addition to your HashtableMap.java implementation, you must develop and submit some tests in a source file named HashtableMapTests.java. These tests must include at least five distinct methods with the following names and signatures. Each of these methods should return true when the HashtableMap class that they run with behaves correctly, and false when any unexpected (or buggy) behavior is detected. Each test should be designed to test a different feature of the HashtableMap implementation. Note that these methods should not throw exceptions, since any expected exceptions should be caught to verify correct expected behavior, and any unexpected exceptions should be caught and reported as faulty behavior (returning false). Be sure to provide descriptive comments for each of these test methods, which describe the high-level functionality that they are designed to check for. Note that these test methods should NOT be JUnit5 tests for this assignment.
public static boolean test1() { /* test code here */ } public static boolean test2() { /* test code here */ } public static boolean test3() { /* test code here */ } public static boolean test4() { /* test code here */ } public static boolean test5() { /* test code here */ }
Already registered? Login
Not Account? Sign up
Enter your email address to reset your password
Back to Login? Click here