In: Computer Science
in Java, 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 names EmployeeTest that demonstrates class EMLOYEE’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.
class Employee{
private String firstName;
private String lastName;
private double salary;
//constructor to initialize the data
public Employee(String aFirstName, String aLastName, double aSalary) {
super();
firstName = aFirstName;
lastName = aLastName;
salary = aSalary;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public double getSalary() {
return salary;
}
public void setFirstName(String aFirstName) {
firstName = aFirstName;
}
public void setLastName(String aLastName) {
lastName = aLastName;
}
public void setSalary(double aSalary) {
if(aSalary>=0)
salary = aSalary;
}
@Override
public String toString() {
return "Employee [firstName=" + firstName + ", lastName=" + lastName + ", salary=" + salary + "]";
}
}
public class TestEmployee {
public static void main(String[] args) {
//creating emp object
Employee e1 = new Employee("Uday", "Kumar", 10000);
System.out.println("Before salary hike");
System.out.println(e1);
// adding 10% hike to the salary
e1.setSalary(e1.getSalary() + e1.getSalary() * 0.1);
System.out.println("After salary hike");
System.out.println(e1);
Employee e2 = new Employee("Keerthi", "Kumar", 10000);
System.out.println("Before salary hike");
System.out.println(e2);
// adding 10% hike to the salary
e2.setSalary(e2.getSalary() + e2.getSalary() * 0.1);
System.out.println("After salary hike");
System.out.println(e2);
}
}