Please guys help me understand this assighnment task by task! In Java progarmmin
ID: 3731326 • Letter: P
Question
Please guys help me understand this assighnment task by task! In Java progarmming language
thank you!
Today, you will learn about a different data structure. You've seen the HashMap and the HashSet. The "hash" in HashMap and HashSet refers to a data structure known as the hash table You will implement the following inerface. The methods are described in the tasks below. public interface HTableKE> public void add (E element) public boolean contains (Object o)i public void print Task 1: So...hash table. What's up with that? You are familiar with arrays. You are familiar with linked lists. Some of you prefer arrays while others prefer linked lists. Well, today everyone will be happy! Hash tables bring together the best of both worlds. A hash table is an array of... wait for it..linked lists! I am not making this up. A hash table is actually an array and each array element is a linked list. Data structures just don't get more awesome than this. Ready? Off we go... Create a class named Hashtable, where E is the type of object that will be stored in the hash table. This class will implement the HTable interface given above. Task 2: Setting up the Hashtable class: The field for the class is an array of linked lists. You may use Java's LinkedList class for this lab (import java.util.LinkedList). This makes the type of the array LinkedList0 The constructor must create this array of linked lists. This is a two step process. First, you create the array (let's say of size 15). To make this as fun as possible, Java does not allow generic array creation. To get around this, create an array of type LinkedList[] and then employ type casting. Remember that creating an array just creates an array of empty boxes. No actual linked lists are created. The second step is to then traverse the array and set each array element equal to a brand new linked list. Naturally, all the linked lists are empty at this point. You should have two constructors: a default constructor and one that allows the array capacity to be specified as a parameter.Explanation / Answer
Hello, I have a solution for you. Implemented everything as per the requirements. Defined following things in this answer.
// HTable.java interface (no changes)
public interface HTable<E> {
public void add(E element);
public boolean contains(Object o);
public void print();
}
// HashTable.java
import java.util.LinkedList;
public class HashTable<E> implements HTable<E> {
/**
* Array of linked list attribute
*/
private LinkedList<E>[] arrayOfList;
// constant for default capacity
private static final int DEFAULT_CAPACITY = 15;
/**
* Default constructor, will initialize the array of 15 capacity
*/
public HashTable() {
arrayOfList = new LinkedList[DEFAULT_CAPACITY];
for (int i = 0; i < arrayOfList.length; i++) {
/**
* Initializing all elements
*/
arrayOfList[i] = new LinkedList();
}
}
/**
* paremeterized constructor, will initialize the array of given capacity
*/
public HashTable(int capacity) {
arrayOfList = new LinkedList[capacity];
for (int i = 0; i < arrayOfList.length; i++) {
arrayOfList[i] = new LinkedList();
}
}
@Override
public void add(E element) {
/**
* Finding the hash code. Hash codes can be negative, which can't be
* allowed here because if the hash is negative, performing modulus
* operation will yield a negative value which cannot be used as the
* array index, so here we use Math.abs() to remove the nagative sign
*/
int hash = Math.abs(element.hashCode());
/**
* finding the possible index
*/
int index = hash % arrayOfList.length;
/**
* adding to the linked list of found index
*/
arrayOfList[index].add(element);
}
@Override
public boolean contains(Object o) {
/**
* finding the hash
*/
int hash = Math.abs(o.hashCode());
/**
* finding the possible index. so that we have to search in the linked
* list of that index ONLY, avoiding the need to search the entire array of lists
*/
int possibleIndex = hash % arrayOfList.length;
if (arrayOfList[possibleIndex].contains(o)) {
//found
return true;
}
//not found
return false;
}
@Override
public void print() {
System.out.println("HASH TABLE CONTENTS:");
/**
* looping through all array elements
*/
for (LinkedList list : arrayOfList) {
/**
* looping through each element in the current linked list
*/
for (Object o : list) {
//displaying
System.out.println(o);
}
}
}
}
//Test.java
public class Test {
public static void main(String[] args) {
HashTable<String> h = new HashTable<String>();
String s = "happy meals are quite tasty and make round children";
String a[]=s.split(" ");
for(String w: a){
h.add(w);
}
System.out.println(h.contains("tasty"));
System.out.println(h.contains("fruity"));
h.print();
}
}
/*OUTPUT*/
true
false
HASH TABLE CONTENTS:
meals
children
happy
and
make
tasty
are
quite
round