Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

without overriding the methods ase guys help me understand t6:05PMtask by task! In JavaO thank you Today, you will learn about a diferent data structure.

without overriding the methods image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
ase guys help me understand t6:05PMtask by task! In JavaO thank you Today, you will learn about a diferent data structure. You've sem the HashMap and the HashSet. The has in Hask Map and Has Set refers to a data siructure known n the hash You will implemen the ollowing interface. The methods are described in the tasks helow vold print Task I:Sa...hash table. What's up with hat? You are familiar with arrays You are famaliar with linked lists Some of you prefer arrays while others prefier lnked lists Well, soday everyone will be happy! Hash tables bring totherh best of both worlds Ahash table is an array of..wait for itlinked lists 1am not making this up A hash table is acually an array and each aray elemens is a linked list Dala stuctares just Create a class named Hashable E, where E is the type o object that will be stored in the hash table Thas dass w implement the HTableE) "terface given above Task 21 Setting up the Hasbtablec la The field for the class is an array oflinked ists You may lava's Linked ist class for this lab (import java util Linkedit) This makes the type of the array Liked ist P The conntructor must create this aray olinked lists This is a two siep process Fint, you create the aray (let's say of size I5). To make this as fun as possible, Java dees not allow pmeric array creation. To get around this, creatle an array of type Linkodl istand then employ linked lists equal to a Ibrand new linked list Naturally, all the linked lists ane empty at this point The second sep is to You should have two coestructors a default constructor and one that allows the array capacity to Tusk & Adding jects to the bash table an add method or your them" You use the ojct'shash code, of counse Recall the Ob eu' clasa nis dassconans methods every other dass a omalially "kih Task &: Searching a hash table You may be asking yourself Selfwhat is the point of this nuyda you ane ahot to write should anwer your question The method dd the contains method to your lascable class Grn an ojct,te contains method retun true if the ojoct is in the hash able and alse if it is no How do you knew if a given ojct is in the hash table? De you have to scanch the entine table l Fnalily, add a method to your Hashtable class that prints, in a single celamn all the objocts in ho T YOUR PROGRAM A Hashing Intertude Today, you will learn about a difterent data structure. You've seen the HashMap and the HashSetThe "hash" in HashMap and HashSetrefers to data structure known as the hash table. You will implement the following interface. The methods are described in the tasks below. public interface HTablecE> ( public void add (E element) public boolean contains (object o): public void print 0: Task I: 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 E>, where E is the type of object that will be stored in the hash table. This class will implement the HTablecE> interface given above. Task 2: Setting up the HashtablecE> class: The field for the class is an array of linked lists. You may use Java's LinkedList class for this lab (import javautilLinkedlist). This makes the type of the array LinkedLseE0 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 tpe 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 Task 3: Adding objects to the hash table Now it's time to write an add method for your class that puts an object of type E in the hash table. So, you're probably asking yourself Where do the objects actually go? They go into the linked lists. Now you're probably asking yourself, But which linked list? We have 15 of theml" You use the object's hash code, of course. Recall the Object class. This class contains methods that every other class automatically inherits These methods include equals and toString. This means that every object you create has an equals and a toString method. The Object class also contains a method called hashCode. This method returms an integer that uniquely identifies this object. This number is called a hash code Hash code- an integer that uniquely identifies an object Use the object's hash code to derive an array index (hash code % array length). This index specifies which linked list to place the object in. (Note: the integer returned by hashCode may be negative.) Task 4: Searching a hash table You may be asking yourself, "Self, what is the point of this nutty data structure?" The method you are about to write should answer your question Add the contains method to your Hashtable class. Given an object, the contains method returns true if the object is in the hash table and false if it is not. How do you know if a given object is in the hash table? Do you have to search the entire table to find it? Task 5: Printing a hash table Finally add a method to your Hashtable clas that prints, n a gmgle column, all the objects in the table TEST YOUR PROGRAM tzing hApPmeale are quice casty and ake zound chaldrea: A Hashing Intertude Today, you will learn about a difterent data structure. You've seen the HashMap and the HashSetThe "hash" in HashMap and HashSetrefers to data structure known as the hash table. You will implement the following interface. The methods are described in the tasks below. public interface HTablecE> ( public void add (E element) public boolean contains (object o): public void print 0: Task I: 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 E>, where E is the type of object that will be stored in the hash table. This class will implement the HTablecE> interface given above. Task 2: Setting up the HashtablecE> class: The field for the class is an array of linked lists. You may use Java's LinkedList class for this lab (import javautilLinkedlist). This makes the type of the array LinkedLseE0 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 tpe 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 Task 3: Adding objects to the hash table Now it's time to write an add method for your class that puts an object of type E in the hash table. So, you're probably asking yourself Where do the objects actually go? They go into the linked lists. Now you're probably asking yourself, But which linked list? We have 15 of theml" You use the object's hash code, of course. Recall the Object class. This class contains methods that every other class automatically inherits These methods include equals and toString. This means that every object you create has an equals and a toString method. The Object class also contains a method called hashCode. This method returms an integer that uniquely identifies this object. This number is called a hash code Hash code- an integer that uniquely identifies an object Use the object's hash code to derive an array index (hash code % array length). This index specifies which linked list to place the object in. (Note: the integer returned by hashCode may be negative.) Task 4: Searching a hash table You may be asking yourself, "Self, what is the point of this nutty data structure?" The method you are about to write should answer your question Add the contains method to your Hashtable class. Given an object, the contains method returns true if the object is in the hash table and false if it is not. How do you know if a given object is in the hash table? Do you have to search the entire table to find it? Task 5: Printing a hash table Finally add a method to your Hashtable clas that prints, n a gmgle column, all the objects in the table TEST YOUR PROGRAM tzing hApPmeale are quice casty and ake zound chaldrea

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions