Question

Java question need help

List the contents of a hash table of SIZE 10 filled using chaining Use the hash function: index= ((string. length() * 3) The

For the data elements can be item1, item2, item3 ,item4.... etc.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program in Java

HashFunctionDemo .java


package printerjob;

import java.util.Hashtable;
import java.util.Scanner;
import java.util.Set;

/**
*
* @author Harsh
*/
public class HashFunctionDemo {
    // return index of hashtable
    static int HashFunction(String str,int MAX){
        int index=(str.length()*3)%10;// function for hash
       return index;
    }
  
    public static void main(String[] args) {
        Hashtable<Integer,String> hashtable=new Hashtable<>();
        Scanner scanner=new Scanner(System.in);
        String ch;
        int MAX=10;
        for(int i=0;i<MAX;i++){
            hashtable.put(i, "empty");//insert empty intially
        }
        do{
            System.out.println("Enter String");
            String str=scanner.next();//accept string
            int index=HashFunction(str,MAX);
            if(hashtable.get(index).equals("empty")){// insert 1st time
                hashtable.replace(index, str);// replace string
            }
            else{// if more than 1 time to insert
                StringBuilder builder=new StringBuilder(hashtable.get(index));
                builder.append(" "+str);// append string
                hashtable.replace(index, builder.toString());//replace string
            }
            System.out.println("Do you want to continue yes/no");
            ch=scanner.next();
          
        }while(ch.equals("yes") || ch.equals("Yes"));
        Set<Integer> keys = hashtable.keySet();//print all key and value from hashtable
        for(Integer key: keys){
            System.out.println(""+key+" :: "+hashtable.get(key));
        }
    }
  
}

/*

Enter String
bhushan
Do you want to continue yes/no
yes
Enter String
mahajan
Do you want to continue yes/no
yes
Enter String
harsh
Do you want to continue yes/no
yes
Enter String
teja
Do you want to continue yes/no
no
9 :: empty
8 :: empty
7 :: empty
6 :: empty
5 :: harsh
4 :: empty
3 :: empty
2 :: teja
1 :: bhushan mahajan
0 :: empty

*/

Add a comment
Know the answer?
Add Answer to:
List the contents of a hash table of SIZE 10 filled using chaining Use the hash function: index= ...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 5. Hashing (a) Consider a hash table with separate chaining of size M = 5 and...

    5. Hashing (a) Consider a hash table with separate chaining of size M = 5 and the hash function h(x) = x mod 5. i. (1) Pick 8 random numbers in the range of 10 to 99 and write the numbers in the picked sequence. Marks will only be given for proper random numbers (e.g., 11, 12, 13, 14 ... or 10, 20, 30, 40, .. are not acceptable random sequences). ii. (2) Draw a sketch of the hash table...

  • Assume a hash table is implemented using chaining with buckets implemented using binary search trees. What's...

    Assume a hash table is implemented using chaining with buckets implemented using binary search trees. What's the average case time complexity of searching for a data item? Assume the size of data, n, is much larger than the size of the hash table. Oln O(1) O(n) O(login) D Question 24 2 pts Assume that list consists of the following elements. What is the result after the first pass of the selection sort algorithm? Assume the list is to be sorted...

  • Write a method in the HashIntSet class called addAll that accepts another hash set as a...

    Write a method in the HashIntSet class called addAll that accepts another hash set as a parameter and adds all of the elements from the other set into the current set. For example, if the set stores [-5, 1, 2, 3] and the method is passed [2, 3, 6, 44, 79], your set would store [-5, 1, 2, 3, 6, 44, 79]. Write a method in the HashIntSet class called containsAll that accepts another hash set as a parameter and...

  • How to write the insert, search, and remove functions for this hash table program? I'm stuck......

    How to write the insert, search, and remove functions for this hash table program? I'm stuck... This program is written in C++ Hash Tables Hash Table Header File Copy and paste the following code into a header file named HashTable.h Please do not alter this file in any way or you may not receive credit for this lab For this lab, you will implement each of the hash table functions whose prototypes are in HashTable.h. Write these functions in a...

  • JAVA Submit:    HashSet.java    with the following methods added: HashSet.java // Implements a set of...

    JAVA Submit:    HashSet.java    with the following methods added: HashSet.java // Implements a set of objects using a hash table. // The hash table uses separate chaining to resolve collisions. // Original from buildingjavaprograms.com supplements public class HashSet<E> { private static final double MAX_LOAD_FACTOR = 0.75; private HashEntry<E>[] elementData; private int size; // Constructs an empty set. @SuppressWarnings("unchecked") public HashSet() { elementData = new HashEntry[10]; size = 0; } // ADD METHODS HERE for exercise solutions: // Adds the...

  • JAVA (advanced data structures) write a completed program using HashIntSet and HashMain include the method in the main...

    JAVA (advanced data structures) write a completed program using HashIntSet and HashMain include the method in the main if possible. those are just the sample HashIntSet and HashMain (don't have to be the same but follow the Q requirement. thanks HashIntSet.java public class HashIntSet { private static final double MAX_LOAD_FACTOR = 0.75; private HashEntry[] elementData; private int size; // Constructs an empty set. public HashIntSet() { elementData = new HashEntry[10]; size = 0; } // Adds the given element to...

  • IN C++ Please!! Declare a global integer constant called SIZE and initialize it to 10. •...

    IN C++ Please!! Declare a global integer constant called SIZE and initialize it to 10. • Declare a global enum variable that will support 10 values – each representing an ant colony {A, B, C, D, E, F, G, H, I, J}. • In the main function, o Declare a 2-dimensional array of size the global integer constant, SIZE. The number of rows and columns should be equal to SIZE, which would make this a square matrix. This array will...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT