EASY JAVA PROGRAMMING I NEED IT IN AN HOUR PLEASE Implement the following classe
ID: 3825520 • Letter: E
Question
EASY JAVA PROGRAMMING
I NEED IT IN AN HOUR PLEASE
Implement the following classes:
1. Class Person (abstract superclass)
protected member variable id of type int.
A constructor which initializes id.
2. Class Employee (subclass of Person)
Private member variable name of type String.
Overloaded constructors
One constructor takes no input. Default name: "", Default ID: "0"
Another 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 equals method of the Object class that will compare two employees
Two employees are the same if they have same id and name.
3. Class EmployeeApp which has a main method
Ask users for the number of employees
Create an Employee array based on the given number
Accepts user input for name and id for all employees; creates Employee objects and stores their references in the array. Use the constructor to initialize the objects.
Using the equals method you implemented to find duplicate employee data in the array.
Explanation / Answer
Please Find below the program I executed and provided the output screen also. I attached the github URL also for code. you can refer that too.
https://github.com/BagavathPadmanabhan/Employee_duplicate_found/
Person.java
public abstract class Person {
protected int id;
public Person() {
this.id = id;
}
}
Employee.java
package com.chegg.person;
public class Employee extends Person {
//For accessing from EmployeeApp, we declared the name variable as Protected
protected String name;
public Employee() {
this.name = "";
this.id = 0;
}
public Employee(int id, String name) {
super();
this.name = name;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (!Employee.class.isAssignableFrom(obj.getClass())) {
return false;
}
final Employee other = (Employee) obj;
if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) {
return false;
}
if (this.id != other.id) {
return false;
}
return true;
}
}
EmployeeApp.java
package com.chegg.person;
import java.util.Scanner;
public class EmployeeApp {
public static void main(String[] args) {
Object o = new Object(){};
System.out.println("Enter No. of Employees");
Scanner s= new Scanner(System.in);
int no = s.nextInt();
Employee[] arr = new Employee[no];
System.out.println("Enter Employee details");
Employee e;
for (int i = 0; i < no; i++) {
e = new Employee(){};
s= new Scanner(System.in);
e.id = s.nextInt();
s= new Scanner(System.in);
e.name = s.nextLine();
arr[i] = e;
}
for (int i = 0; i < arr.length - 1; i++) {
for (int j = i+1; j < arr.length; j++) {
if (arr[i].equals(arr[j])) {
System.out.println("Duplicate Entry for the Employee Id: "+ arr[i].id + " Name: " + arr[i].name);
}else{
// System.out.println("No Duplicate Entry found");
}
}
}
}
}
OUTPUT:
Enter No. of Employees
3
Enter Employee details
1
bag
2
john
1
bag
Duplicate Entry for the Employee Id: 1 Name: bag
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.