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

JAVA Assume that you are developing an Employee class with idNum, firstName, and

ID: 3677539 • Letter: J

Question

JAVA

Assume that you are developing an Employee class with idNum, firstName, and lastName. – Now, you want to keep the Employee objects in an ArrayList. – Develop the correct equals() method of the Employee class.

Sample Run

1. Input a number: 100

2. Input a name: Tom

3. More items?(Y/N) y

4. Input a number: 200

5. Input a name: John

6. More items?(Y/N) n

7. The list contains:

8. 100 Tom

9. 200 John

10. Type the number and name to search

11. Number: 100

12. Name: Tom

13. 100 Tom is in the list

14. More search?(Y/N) n

15. Type the number and name to remove

16. Number: 100

17. Name: Tom

18. 100 Tom is removed from the list

19. The list contains:

20. 200 John

21. More remove?(Y/N) n

Explanation / Answer

/**The java class Employee that represents the object of
* Employee class*/
//Employee.java
public class Employee {
  
   private int idNum;
   private String firstName;
   private String lastName;
  
  
   //default constructor
   public Employee() {
       idNum=0;
       firstName="";
       lastName="";
   }
  
   //parameter constructor to set id num, first number and last name
   public Employee(int idNum, String firstName, String lastName) {
       this.idNum=idNum;
       this.firstName=firstName;
       this.lastName=lastName;
   }
  
   public void setId(int idNum){
       this.idNum=idNum;
   }
   public void setFirstName(String firstName){
       this.firstName=firstName;
   }
   public void setId(String lastName){
       this.lastName=lastName;
   }
  
   public int getId(){
       return idNum;
   }
   public String getFirstName(){
       return firstName;
   }
   public String getLastName(){
       return lastName;
   }
  
   //Override the equals method that returns true
   //if the object is employee is same as the class variables
   @Override
   public boolean equals(Object obj) {
       Employee temp=(Employee)obj;
       return temp.getId()==idNum
               && temp.getFirstName().endsWith(firstName)
               && temp.getLastName().endsWith(lastName);
   }
  
  
   //returns the string representaion of the employee object
   @Override
   public String toString() {      
       return idNum+" "+lastName+" "+firstName;
   }

}

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


/**The java program EmpDriver that prompts user to enter id number,
* first name and last name. Then prompts to enter new object.
* If user choice is n, then the program prompts to id number,
* first and last name and search the name in the list .
* If found then print to screen.
* The program prompts to continue, if user enter n ,
* thne the program prompts to id number, first name,
* last name to remove the object from the arraylist.
* */
//EmpDriver.java
import java.util.ArrayList;
import java.util.Scanner;
public class EmpDriver {
   public static void main(String[] args) {
      
       //Craete an arraylist object of type Employee
       ArrayList<Employee>empList=new ArrayList<Employee>();
       boolean repeat=true;
       String choice;
       Scanner scanner=new Scanner(System.in);
       int idNum;
       String firstName;
       String lastName;
      
       //repeat the loop until user enters n to stop
       while(repeat)
       {
           System.out.println("Input a number:");
           idNum=Integer.parseInt(scanner.nextLine());
           System.out.println("Input first name:");
           firstName=scanner.nextLine();
           System.out.println("Input last name:");
           lastName=scanner.nextLine();
          
           empList.add(new Employee(idNum, firstName, lastName));
          
           System.out.println("More items?(Y/N)");
           choice=scanner.nextLine();
          
           if(choice.charAt(0)=='y'||choice.charAt(0)=='Y')
               repeat=true;
           else
               repeat=false;
       }
      
      
      
       //set repeat to true
       repeat=true;
       int pos=-1;
      
      
       int searchNumber;
       String searchfName;
       String searchLName;
       //repeat the loop until user enters n to stop
       while(repeat)
       {
           System.out.println("Type the number and name to search");
          
           System.out.println("Number:");
           searchNumber=Integer.parseInt(scanner.nextLine());
           System.out.println("First Name:");
           searchfName=scanner.nextLine();
          
           System.out.println("Last Name:");
           searchLName=scanner.nextLine();

           Employee emp=new Employee(searchNumber,searchfName,searchLName);
           pos=search(emp,empList);
          
           if(pos!=-1)
               System.out.println(empList.get(pos)+" is in List");
          
           System.out.println("More items?(Y/N)");
           choice=scanner.nextLine();
          
           if(choice.charAt(0)=='y'||choice.charAt(0)=='Y')
               repeat=true;
           else
               repeat=false;
       }
      
       //set repeat to true
       repeat=true;
       //repeat the loop until user enters n to stop
       while(repeat)
       {
           System.out.println("Type the number and name to remove");
          
           System.out.println("Number:");
           searchNumber=Integer.parseInt(scanner.nextLine());
           System.out.println("First Name:");
           searchfName=scanner.nextLine();
          
           System.out.println("Last Name:");
           searchLName=scanner.nextLine();

           Employee emp=new Employee(searchNumber,searchfName,searchLName);
           pos=search(emp,empList);
          
           if(pos!=-1)
           {
               System.out.println(empList.get(pos)+" is removed from the List");
               //call remove method to remove object at pos
               empList.remove(pos);              
           }
          
           System.out.println("More items?(Y/N)");
           choice=scanner.nextLine();
          
           if(choice.charAt(0)=='y'||choice.charAt(0)=='Y')
               repeat=true;
           else
               repeat=false;
       }
              
   }

   /**The method search that takes Employee object and array list
   * of type Employee and returns pos as return value*/
   private static int search(Employee searchEmp,ArrayList<Employee> empList) {      
       int pos=-1;
       boolean found=false;
       for (int i = 0; i < empList.size()&& !found; i++) {
          
           if(empList.get(i).equals(searchEmp))
           {
               found=true;
               pos=i;
           }
       }  
       return pos;
   }

  
}

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

Sample output:

Input a number:
100
Input first name:
john
Input last name:
j
More items?(Y/N)
n
Type the number and name to search
Number:
100
First Name:
john
Last Name:
j
100 j john is in List
More items?(Y/N)
n
Type the number and name to remove
Number:
100
First Name:
john
Last Name:
j
100 j john is removed from the List
More items?(Y/N)
y
Type the number and name to remove
Number: