Implement the following classes use Java Class Person a

• Implement the following classes ( use Java ) :

• Class Person (abstract superclass) • protected member variable id of type int.

• A constructor which initializes id.

• Class Employee (subclass of Person) • private member variable name of type String

• Overloaded constructor

• Takes in two parameters, for name and id.

• Initialization: name initialization is done by Employee constructor;

id initialization is done by the Person constructor.

• overrides equal method of the Object class that will compare two employees

• Two employees are the same if they have same id and name.

• Class EmployeeApp which has a main method

• Create an Employee array of size 3

• Accepts user input for name and id for 3 employees; creates 3 Employee objects and stores them in the array.

.Use Constructors of both the classes to initialize the objects.

• Using the equals method you implemented, compare:

• Employee object at index 0 and index 1

• Employee object at index 1 and index 2

• Employee object at index 0 and index 2

and specify if employees are distinct or same.

Solution

Person.java:


public abstract class Person {
   protected int id;
  
   public Person(int id){
       this.id =id;
   }

}

Employee.java


public class Employee extends Person {
   private String name;
  
  
  
   public Employee(int id, String name){
       super(id);
       this.name = name;
     
   }
  
   public boolean equals (Employee emp){
       boolean isEqual = false;
       if ((this.name).equalsIgnoreCase( emp.name)){
          
           if(this.id   == emp.id){
               isEqual =true;
           }
           else{
               isEqual = false;
           }
       }
       else{
           isEqual = false;
       }
      
       return isEqual;
   }

}

EmployeeApp.java

import java.util.Scanner;


public class EmployeeApp {

   public static void main(String[] args) {
      
      
       Employee employeeArr[]= new Employee[3];
       int counter=0;
       Scanner scanner = new Scanner(System.in);
       while (counter<3) {

System.out.print(\"Enter Id : \");
String id = scanner.nextLine();
System.out.print(\"Enter Name : \");
String name = scanner.nextLine();

  
Employee emp =new Employee( new Integer(id), name);
employeeArr[counter]= emp;
  
counter++;
  

}
       System.out.println(\"Comparing Employee object at index 0 and index 1 :\");
       boolean isEqual1 = employeeArr[0].equals(employeeArr[1]);
       if(isEqual1){
           System.out.println(\"Employee object at index 0 and index 1 are equal\");
       }
       else {
           System.out.println(\"Employee object at index 0 and index 1 are not equal\");
       }
      
      
      
       System.out.println(\"Comparing Employee object at index 1 and index 2 :\");
       boolean isEqual2 = employeeArr[1].equals(employeeArr[2]);
      
       if(isEqual2){
           System.out.println(\"Employee object at index 1 and index 2 are equal\");
       }
       else {
           System.out.println(\"Employee object at index 1 and index 2 are not equal\");
       }
      
      
      
      
       System.out.println(\"Comparing Employee object at index 0 and index 2 :\");
       boolean isEqual3 = employeeArr[0].equals(employeeArr[2]);
      
       if(isEqual3){
           System.out.println(\"Employee object at index 0 and index 2 are equal\");
       }
       else {
           System.out.println(\"Employee object at index 0 and index 2 are not equal\");
       }


   }

}

• Implement the following classes ( use Java ) : • Class Person (abstract superclass) • protected member variable id of type int. • A constructor which initiali
• Implement the following classes ( use Java ) : • Class Person (abstract superclass) • protected member variable id of type int. • A constructor which initiali
• Implement the following classes ( use Java ) : • Class Person (abstract superclass) • protected member variable id of type int. • A constructor which initiali

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site