(Employee Class) Create a class called Employee that includes three instance variables—a first name (type String), a last name (type String) and a monthly salary (double). Provide a constructor that initializes the three instance variables. Provide a set and a get method for each instance variable. If the monthly salary is not positive, do not set its value. Write a test app named EmployeeTest that demonstrates class Employee’s capabilities. Create two Employee objects and display each object’s yearly salary. Then give each Employee a 10% raise and display each Employee’s yearly salary again.
Employee Class in java
Employee.java
/*
* Filename: Employee.java
*
* Description: Exercise 3.14 - Employee Class
*
* @Author: Bilal Tahir Khan Meo
* Website: https://codeblah.com
*
* =====================================================================================
*/
public class Employee{
private String fName;
private String lName;
private double monthlySalary;
// constructor
public Employee(String fName, String lName, double monthlySalary){
setFirstName(fName);
setLastName(lName);
setMonthlySalary(monthlySalary);
}
// setters
public void setFirstName(String fName){
this.fName = fName;
}
public void setLastName(String lName){
this.lName = lName;
}
public void setMonthlySalary(double monthlySalary){
if(monthlySalary > 0)
this.monthlySalary = monthlySalary;
}
public void setRaise(double percentage){
setMonthlySalary(monthlySalary += (monthlySalary / 100) * percentage);
}
// getters
public String getFirstName(){
return fName;
}
public String getLastName(){
return lName;
}
public double getMonthlySalary(){
return monthlySalary;
}
public double getYearlySalary(){
return getMonthlySalary() * 12;
}
}
EmployeeTest.java
/*
* Filename: EmployeeTest.java
*
* Description: Exercise 3.14 - Employee Class
*
* @Author: Bilal Tahir Khan Meo
* Website: https://codeblah.com
*
* =====================================================================================
*/
public class EmployeeTest{
public static void main(String[] args){
Employee employee1 = new Employee("Frank", "Freddy", 1000);
printEmployee(employee1);
Employee employee2 = new Employee("Jack", "Jackson", 768);
printEmployee(employee2);
System.out.println("\nAfter 10% raises:\n");
// set raises of 10%
employee1.setRaise(10);
printEmployee(employee1);
employee2.setRaise(10);
printEmployee(employee2);
}
private static void printEmployee(Employee employee){
System.out.printf("%s %s: $%.2f per annum\n",
employee.getFirstName(), employee.getLastName(),
employee.getYearlySalary());
}
}