Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

insert(elem): Inserts element elem in the hash table. Your program should return

ID: 3863155 • Letter: I

Question

insert(elem): Inserts element elem in the hash table. Your program should return true or false, depending on whether it was inserted or not. Return true if item is inserted, false if it already exists. Throw a NullPointerException if a null value is passed.

contains(elem): Uses the hash table to determine if elem is in the hash table. Your program should return true or false, depending on whether the item exists. Throw a NullPointerException if a null value is passed.

delete(elem): Use the hash table to determine where elem is, delete it from the hash table. Your program should return true if the item is deleted, false if it can’t be deleted (an item can’t be deleted if it does not exist in the hash table). Throw a NullPointerException if a null value is passed.

printTable(): Print out the hash table.

getSize(): returns the number of elements currently stored in the hashtable

Note that in the contains/delete method, you shouldn’t need to search through the entire table to find an element.

Use separate chaining to resolve collisions, keep track of load factor, when its greater than 2/3, double the size and rehash the values. Do not use String.hashCode(), must implement your own algorithm.

public interface InterHashTable {

   boolean insert(String value);
   boolean delete(String value);
   boolean contains(String value);
/* Example output for this function:
   *
   * 0:
   * 1:
   * 2: cheese, food
   * 3: table
   * 4:
   * /
   void printTable();
   int getSize();
}

Explanation / Answer

PROGRAM:

//class operation

package hashtable;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.Collections;

import java.util.Hashtable;

import javax.swing.plaf.synth.SynthSeparatorUI;

public class Operations implements InterHashTable

{

   // global Hashtable for storing the value

   Hashtable<Integer,String> hs=new Hashtable<>();

   int count=0;

  

   @Override

   //method for inserting values to hashtable

   public boolean insert(String value)

   {

       boolean flag=false;

       //Checking whether value exists

       if(hs.contains(value))

       {

           flag =false;

          

       }

       else

       {

           flag=true;

           hs.put(count, value);

           count++;

       }

       return flag;

   }

   //method for deleting values to hashtable

   @Override

   public boolean delete(String value)

   {

       boolean flag=false;

       //removing the value from the hashtable

       try

       {

           flag=hs.values().removeAll(Collections.singleton(value));

       }

       catch(NullPointerException e)

       {

           e.printStackTrace();

       }

       return flag;

   }

   //method for checking a value exists in the string

   @Override

   public boolean contains(String value)

   {

       boolean flag=false;

       try

       {

           if(hs.contains(value))

           {

               flag =true;

           }

       }

       catch(NullPointerException e)

       {

           e.printStackTrace();

       }

       return flag;

   }

   //method for displaying

   @Override

   public void printTable()

   {

      

       System.out.println(hs);

      

      

   }

// method for getting the size

   @Override

   public int getSize()

   {

       int size=hs.size();

       return size;

   }

   //main function

   public static void main(String s[])

   {

       //Object of the class

       InterHashTable inter=new Operations();

       int choice=0;

       InputStreamReader in=new InputStreamReader(System.in);

       BufferedReader br=new BufferedReader(in);

       String value=null;

      

       loop:do

       {

           //getting the choice

           System.out.println("Enter your Choice :");

           try {

               System.out.println("1. Insert");

               System.out.println("2.delete");

               System.out.println("3.contains");

               System.out.println("4. print");

               System.out.println("5. size:");

               System.out.println("6.Exit");

               choice=Integer.parseInt(br.readLine());

               switch(choice)

               {

               case 1:

                   System.out.println("Enter value :");

                   //get the user input

                   value=br.readLine();

                   //calling method insert

                   System.out.println(inter.insert(value));

                   break;

               case 2:

                   System.out.println("Enter value :");

                   //get the user input

                   value=br.readLine();

                   //calling method delete

                  

                   System.out.println(inter.delete(value));

                   break;

               case 3:

                   System.out.println("Enter value :");

                   //get the user input

                   value=br.readLine();

                   //calling method contains

                   System.out.println(inter.contains(value));

                   break;

               case 4:

                   //calling method printTable

                   inter.printTable();

                   break;

               case 5:

                   //calling method size

                   System.out.println("size of the hash table :"+inter.getSize());

                   break;

               case 6:

                   break loop;

                  

                   default:

                       System.out.println("Enter a valid choice");

                      

               }

           }

           catch (NumberFormatException | IOException e)

           {

               System.out.println("Enter a valid choice:");

           }

           }while(true);

   }

}

========================================================================

//interface

package hashtable;

public interface InterHashTable

{

         boolean insert(String value);

      boolean delete(String value);

      boolean contains(String value);

      void printTable();

      int getSize();

}

OUTPUT:

Enter your Choice :

1. Insert

2.delete

3.contains

4. print

5. size:

6.Exit

1

Enter value :

john

true

Enter your Choice :

1. Insert

2.delete

3.contains

4. print

5. size:

6.Exit

1

Enter value :

merry

true

Enter your Choice :

1. Insert

2.delete

3.contains

4. print

5. size:

6.Exit

3

Enter value :

john

true

Enter your Choice :

1. Insert

2.delete

3.contains

4. print

5. size:

6.Exit

4

{1=merry, 0=john}

Enter your Choice :

1. Insert

2.delete

3.contains

4. print

5. size:

6.Exit

5

size of the hash table :2

Enter your Choice :

1. Insert

2.delete

3.contains

4. print

5. size:

6.Exit

2

Enter value :

john

true

Enter your Choice :

1. Insert

2.delete

3.contains

4. print

5. size:

6.Exit

4

{1=merry}

Enter your Choice :

1. Insert

2.delete

3.contains

4. print

5. size:

6.Exit