Write a program that keeps a map in which the keys of the map are objects of cla
ID: 3761307 • Letter: W
Question
Write a program that keeps a map in which the keys of the map are objects of class Employee. An employee should have a first name, a last name, and a unique integer ID. For the performance changes and removals, lookup should be by ID. Prompt the user of the program to add or remove employees, to modify the performances, or to print all the performaces. The printout should be sorted by last name. If two employees have the same last name, then use the first name as a tie breaker. If the first names are also identical, then use the integer ID. Supply compatible hashCode and equals methods to the Employee class. Test the hash code by adding the Employee objects to a hash set.
Explanation / Answer
Answer:
Note: language is not specified hence I implemented in JAVA
Program code:
import java.util.HashSet;
import java.util.Set;
import java.util.*;
//employee class
class employee
{
//variable declarations
private int Id;
private string First_Name;
private string Last_Name;
private string Department;
//default constructor
public employee()
{
}
//method getInt
public int GetId()
{
return Id;
}
//set Id method
public void set_Id(int id)
{
this.Id=id;
}
//getting the firstName
public string getFirst_Name()
{
return First_Name;
}
//setting the firstName
public void setFirst_Name(string Last_Name)
{
this. Last_Name= Last_Name;
}
//method get_Department
public string get_Department()
{
return Department;
}
//Method set_Department
public void setDepartment(string dept)
{
this.Department=Department;
}
//boolean Equals method
public boolean equals(Object ob)
{
if(ob==null)
return false;
if(ob==this)
return true;
return true;
}
//method code for generating the code
public int hashCode()
{
final int HASH_MULTIPLIER1 = 29;
int h1 = HASH_MULTIPLIER1 * First_Name.hashCode() + Last_Name.hashCode();
h1 = HASH_MULTIPLIER1 * h1 + ((Integer)Id).hashCode();
return h1;
}
}
//method main
public class testHash
{
public static void main(String[] args)
{
//instance for the employee class
employee e1=new employee();
employee e2=new employee();
System.out.println("hash code of Emp=" + e1.hashCode());
System.out.println("hash code of Emp2=" + e2.hashCode());
Set<employee> emps=new HashSet<employee>();
emps.add(e1);
emps.add(e2);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.