Question

IN JAVA USING ECLIPSE The objective of this assignment is to create your own hash table...

IN JAVA USING ECLIPSE The objective of this assignment is to create your own hash table class to hold employees and their ID numbers. You'll create an employee class to hold each person's key (id number) and value (name). Flow of the main program: Create an instance of your hash table class in your main method. Read in the Employees.txt file and store the names and ID numbers into Employee objects and store those in your hash table using the SIMPLE modulo operator (%) hash function that we covered in class: "key%N" where N is the size of your array. After you stored all the data in the table, print out all the employee names and their corresponding id numbers. Pick a random employee's ID number and then search for them in the hash table and retrieve their name, print it to the screen. Requirements: 1) Your hash table class must have an array of Employee objects 2) Use the Linear probing technique to handle collisions when you add or search the table 3) You hash function code should be in its own method in the hash table class, NOT in your main method. 4) Your Employee class must contain the ID number and the name You don't have to worry about removing data from the table or keeping track of “Empty Since Start” or “Empty After Removal” buckets. Treat all empty buckets as "Empty since start". You must have these 3 classes: Main.java, HashTable.java, Employee.java

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

/*Test Main java program for Employee.java and HashTable.java classes*/
//Main.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Main
{
   //Create a HashTable class object of size,53 (Random prime number )
   //53 is taken for best results ,larger the size best results
   private static HashTable hashtable=new HashTable(53);
   public static void main(String[] args)
   {
      
       String fileName="empdata.txt";
       Scanner filereader=null;
       try
       {
           filereader=new Scanner(new File(fileName));
           //read data from file
           while(filereader.hasNextLine())
           {
               String data[]=filereader.nextLine().trim().split(",");
               int id=Integer.parseInt(data[0]);
               String name=data[1];
               //Add to the hash table
               hashtable.addEmployee(new Employee(id, name));
           }
          
           filereader.close();
           System.out.println("HASH TABLE ");


           //print to console hash table
           System.out.println(hashtable.toString());  
           Scanner console=new Scanner(System.in);
          
           System.out.println("Enter a employee id #:");
           //read emp id
           int idNumber=Integer.parseInt(console.nextLine());
          
           //Call method to get the emp by id number
           Employee emp=hashtable.getByID(idNumber);
           //check if emp is not null
           if(emp!=null)
           {
               //print emp object
               System.out.println("Employee found!");
               System.out.println(emp.toString());
           }
           else
           {
               System.out.println("Employee "+idNumber+" is not found in hash table.");
           }      
       }
       catch (FileNotFoundException e)
       {
           System.out.println(e.getMessage());
       }
   }  
}

---------------------------------------------------------------------------------------------------------------------------------------

//HashTable.java
public class HashTable
{
   private int N;
   private Employee emps[];
   public HashTable(int size)
   {
       N=size;
       //create an array of size,N
       emps=new Employee[N];
   }
   /*Method to add Employee object to hash table */
   public void addEmployee(Employee emp)
   {
       int k=emp.hashCode();      
       int index=Math.abs(k%N);
       emps[index]=emp;

   }

   /*Returns the object with id number*/
   public Employee getByID(int id)
   {
       Employee emp=null;
       for(Employee temp: emps)
           if(temp!=null)
               if(temp.getId()==id)
                   return temp;
       return emp;
   }
   /*Returns the string represenation of hash table*/
   public String toString() {
       String data=String.format("%-10s\t%-20s\n", "ID","Name");
       for(Employee emp: emps)
       {
           if(emp ==null)
               data+="NULL"+"\n";
           else
               data+=emp.toString();
       }
       return data;
   }
}

---------------------------------------------------------------------------------------------------------------------------------------
//Employee.java
public class Employee
{
   //private data variables
   private int id;
   private String name;
   /*default constructor*/
   public Employee()
   {
       id=0;
       name="N/A";
   }
   /*Parameterized constructor*/
   public Employee(int id, String name)
   {
       this.id=id;
       this.name=name;
   }
   /**
   * return the id
   */
   public int getId() {
       return id;
   }
   /**
   * id the id to set
   */
   public void setId(int id) {
       this.id = id;
   }
   /**
   * return the name
   */
   public String getName() {
       return name;
   }
   /**
   * name the name to set
   */
   public void setName(String name)
   {
       this.name = name;
   }
   /*Returns the hash code*/
   public int hashCode()
   {
       final int prime = 31;
       int result = 1;
       result = prime * result + id;
       result = prime * result + ((name == null) ? 0 : name.hashCode());
       return result;
   }
   public String toString()
   {
       return String.format("%-10d\t%-20s\n", getId(),getName());
   }
} //end of class

---------------------------------------------------------------------------------------------------------------------------------------

Input file:

empdata.txt

135,John Peterman
160,Joe Divola
101,David Putty
68,Jerry Seinfeld
225,George Costanza
100,Elaine Benes
200,Cosmo Kramer

Sample Output:

HASH TABLE
ID     Name
NULL
NULL
135    John Peterman   
NULL
NULL
NULL
101    David Putty   
NULL
NULL
NULL
NULL
NULL
100    Elaine Benes
NULL
NULL
225    George Costanza   
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
68     Jerry Seinfeld
NULL
NULL
NULL
NULL
NULL
160    Joe Divola
NULL
NULL
200    Cosmo Kramer
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL

Enter a employee id #:
200
Employee found!
200    Cosmo Kramer

Add a comment
Know the answer?
Add Answer to:
IN JAVA USING ECLIPSE The objective of this assignment is to create your own hash table...
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
  • Java using data structures The objective is to create your own Hash Table class to hold...

    Java using data structures The objective is to create your own Hash Table class to hold a list of employees and their ID numbers. I've provided the TableEntry class which will be each data object in the hash table. The list of employees will be provided as a .txt file and must be read with the code. please create a .txt file called Employees.txt with the info provided so that the java code can read it in. Employees.txt: (No WhiteSpace...

  • This should be in Java Create a simple hash table You should use an array for...

    This should be in Java Create a simple hash table You should use an array for the hash table, start with a size of 5 and when you get to 80% capacity double the size of your array each time. You should create a class to hold the data, which will be a key, value pair You should use an integer for you key, and a String for your value. For this lab assignment, we will keep it simple Use...

  • The task of this project is to implement in Java Hash Table structure using Linear Probing...

    The task of this project is to implement in Java Hash Table structure using Linear Probing Collision Strategy.   You can assume that no duplicates or allowed and perform lazy deletion (similar to BST). Specification Create a generic class called HashTableLinearProbe <K,V>, where K is the key and V is the value. It should contain a private static class, HashEntry<K,V>. Use this class to create array to represent Hashtable:             HashEntry<K,V> hashtable[]; Implement all methods listed below and test each method...

  • We are using blueJ for my java class, if you can help me id greatly appreciate...

    We are using blueJ for my java class, if you can help me id greatly appreciate it. Create a new Java class called AverageWeight. Create two arrays: an array to hold 3 different names and an array to hold their weights. Use Scanner to prompt for their name and weight and store in correct array. Compute and print the average of the weights entered using printf command. Use a loop to traverse and print the elements of each array or...

  • Following class is only part of my program that uses a hash table to simulate a...

    Following class is only part of my program that uses a hash table to simulate a market's client database, client class is my main class which asks user to input client names and then creates a hash table which then users can look up client names. wasn't able to upload everything as it is too much code, but what I need is to modify my client class instead of me inputting data line by line, the program should read from...

  • Please complete the following task: Create a C++ hash table program for generating a hash from a string. Create a hash f...

    Please complete the following task: Create a C++ hash table program for generating a hash from a string. Create a hash function then test and evaluate the results. The set of strings are from a "words.txt" file (there are 45,402 words in the file, no repeating words. Map the words to 45,402 buckets using a hash function). Requirements: Create an array of 45,402 integers and initialize each to zero. These will be used to store how many times each index...

  • We use bluej for our JAVA class. If you can help me id greatly appreciate it....

    We use bluej for our JAVA class. If you can help me id greatly appreciate it. Create a new Java class called AverageWeight. Create two arrays: an array to hold 3 different names and an array to hold their weights. Use Scanner to prompt for their name and weight and store in correct array. Compute and print the average of the weights entered using printf command. Use a loop to traverse and print the elements of each array or use...

  • Implement a software program in C++ t stores and searches the Student records using double-hashing algorithm.  Double...

    Implement a software program in C++ t stores and searches the Student records using double-hashing algorithm.  Double hashing uses the idea of applying a second hash function to key when a collision occurs.   The software program will be based on the following requirements: Development Environment: If the software program is written in C++, its project must be created using Microsoft Visual Studio 2017. If the software program is written in Java, its project must be created using NetBeans v8.2. Algorithm: If...

  • I really need assistance with creating a C++ hash table program for generating a hash from a string. Create a hash funct...

    I really need assistance with creating a C++ hash table program for generating a hash from a string. Create a hash function then test and evaluate the results. The set of strings are from a "words.txt" file (there are 45,402 words in the file, no repeating words. Map the words to 45,402 buckets using a hash function). Requirements: Create an array of 45,402 integers and initialize each to zero. These will be used to store how many times each index...

  • I really need assistance with creating a C++ hash table program for generating a hash from a string. Create a hash funct...

    I really need assistance with creating a C++ hash table program for generating a hash from a string. Create a hash function then test and evaluate the results. The set of strings are from a "words.txt" file (there are 45,402 words in the file, no repeating words. Map the words to 45,402 buckets using a hash function). Requirements: Create an array of 45,402 integers and initialize each to zero. These will be used to store how many times each index...

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