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

For this problem, you have to use following hash function: key modulo the number

ID: 3790514 • Letter: F

Question

For this problem, you have to use following hash function: key modulo the number of buckets.

Input format: This program takes a file name as argument from the command line. The file is either blank or contains successive lines of input. Each line contains a character, either ‘i’ or ‘s’, followed by a tab and then an integer, the same format as in the Second Part. For each of the lines that starts with ‘i’, your program should insert that number in the hash table if it is not present. If the line starts with a ‘s’, your program should search the hash table for that value.

Output format: For each line in the input file, your program should print the status/result of that operation. For an insert, the program should print “inserted” if the value is inserted or “duplicate” if the value is already present. For a search, the program should print ”present” or “absent” based on the outcome of the search. Your program should print “error” (and nothing else) if the file does not exist. The program should also print “error” for input lines with improper structure.

Example Execution: Lets assume we have 2 text files with the following contents: "file1.txt" is empty file2.txt: i 10 i 12 s 10 i 10 s 5 The the results will be: third file1.txt third file2.txt inserted inserted present error duplicate absent /third file3 .txt error

Explanation / Answer

Output:

Enter The Number Of Files: 2

Enter The File Name: np.txt
Inserted
Inserted
Present
Error
Duplicated
Absent

Enter The File Name:

Program:

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashSet;
import java.util.Scanner;

public class test {

   static String FILENAME = "np.txt";

   public static void main(String[] args) {
       Scanner s = new Scanner(System.in);

       System.out.print("Enter The Number Of Files: ");
       int no = s.nextInt();
       HashSet<String> set = new HashSet<>();
       for (int i = 0; i < no; i++) {
           BufferedReader br = null;
           FileReader fr = null;

           try {
               System.out.print(" Enter The File Name: ");
               FILENAME = s.next();
               try {
                   fr = new FileReader(FILENAME);

                   br = new BufferedReader(fr);

                   String sCurrentLine;

                   br = new BufferedReader(new FileReader(FILENAME));

                   while ((sCurrentLine = br.readLine()) != null) {

                       switch (sCurrentLine.split(" ")[0]) {
                       case "i":
                           if(set.contains(sCurrentLine.split(" ")[1]))
                               System.out.println("Duplicated");
                           else
                           {
                               set.add(sCurrentLine.split(" ")[1]);
                               System.out.println("Inserted");
                           }
                           break;
                       case "s":
                           if(set.contains(sCurrentLine.split(" ")[1]))
                               System.out.println("Present");
                           else
                           {
                              
                               System.out.println("Absent");
                           }
                           break;
                       default:
                           System.out.println("Error");
                       }

                   }

               } catch (FileNotFoundException e) {

                   System.out.println("Error");

               }

           } catch (IOException e) {

               e.printStackTrace();

           } finally {

               try {

                   if (br != null)
                       br.close();

                   if (fr != null)
                       fr.close();

               } catch (IOException ex) {

                   ex.printStackTrace();

               }

           }

       }

   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote