Design and implement a class Employee in a java file called
Design and implement a class Employee in a java file called Employee.java. The class should contain instance variables for employee’s name, age, yearsOfService.
• Define the Employee constructor to accept and initialize instance data. The class should also keep track of the number of employee objects created using a static variable. This variable gets incremented every time in the constructor. • Include get and set methods for the three instance variables.
• Include a instance method called isEligibleForRetirement() that returns a boolean.
• true if the employee is eligible for retirement, false if not.
• For an employee to be eligible for retirement, the sum of age and years of service needs to be greater than 60.
• Create a client class called EmployeeApp (in a file called EmployeeApp.java) which has a main method.
• Using Scanner class, it will ask the user for data for 2 employees.
• i.e. name, age, and years of service for emp 1 and then emp 2.
• Take this user input and invoke the constructor of Employee class, passing the user provided input as arguments. You now have 2 initialized Employee instances.
• Now, your main method should print the number of Employee objects created, display data for each, and specify whether each employee is eligible for retirement or not
Solution
public class Employee {
private String name;
private int age;
private int yearsOfService;
private static int noOfEmployees = 0;
/**
* @param name
* @param age
* @param yearsOfService
*/
public Employee(String name, int age, int yearsOfService) {
this.name = name;
this.age = age;
this.yearsOfService = yearsOfService;
noOfEmployees++;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name
* the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the age
*/
public int getAge() {
return age;
}
/**
* @param age
* the age to set
*/
public void setAge(int age) {
this.age = age;
}
/**
* @return the yearsOfService
*/
public int getYearsOfService() {
return yearsOfService;
}
/**
* @param yearsOfService
* the yearsOfService to set
*/
public void setYearsOfService(int yearsOfService) {
this.yearsOfService = yearsOfService;
}
/**
* @return the noOfEmployees
*/
public static int getNoOfEmployees() {
return noOfEmployees;
}
public boolean isEligibleForRetirement() {
if ((getAge() + getYearsOfService()) > 60)
return true;
else
return false;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return \"Employee [name=\" + name + \", age=\" + age + \", yearsOfService=\"
+ yearsOfService + \"]\";
}
}
import java.util.Scanner;
public class EmployeeApp {
public static void main(String[] args) {
Scanner scanner = null;
try {
scanner = new Scanner(System.in);
String name;
int age;
int yearsOfService;
Employee employee1, employee2;
System.out.print(\"Enter the employee name:\");
name = scanner.next();
System.out.print(\"Enter the employee age:\");
age = scanner.nextInt();
System.out.print(\"Enter the employee Years of Service:\");
yearsOfService = scanner.nextInt();
employee1 = new Employee(name, age, yearsOfService);
System.out.print(\"Enter the employee name:\");
name = scanner.next();
System.out.print(\"Enter the employee age:\");
age = scanner.nextInt();
System.out.print(\"Enter the employee Years of Service:\");
yearsOfService = scanner.nextInt();
employee2 = new Employee(name, age, yearsOfService);
System.out.println(\"Employee 1:\" + employee1);
System.out.println(\"Employee 2:\" + employee2);
if (employee1.isEligibleForRetirement())
System.out.println(\"Employee 1 is eligible for Retirment.\");
else
System.out.println(\"Employee 1 is not eligible for Retirment.\");
if (employee2.isEligibleForRetirement())
System.out.println(\"Employee 2 is eligible for Retirment.\");
else
System.out.println(\"Employee 2 is not eligible for Retirment.\");
System.out.println(\"Number of Employees :\"
+ Employee.getNoOfEmployees());
} catch (Exception e) {
// TODO: handle exception
}
}
}
OUTPUT:
Enter the employee name:Srinivas
Enter the employee age:45
Enter the employee Years of Service:16
Enter the employee name:Pavan
Enter the employee age:40
Enter the employee Years of Service:18
Employee 1:Employee [name=Srinivas, age=45, yearsOfService=16]
Employee 2:Employee [name=Pavan, age=40, yearsOfService=18]
Employee 1 is eligible for Retirment.
Employee 2 is not eligible for Retirment.
Number of Employees :2


