Create a class called Employee that contains three instance variables (First Name (String), Last Name (String), Monthly Salary (double)). Create a constructor that initializes these variables. Define set and get methods for each variable. If the monthly salary is not positive, do not set the salary. Create a separate test class called EmployeeTest that will use the Employee class. By running this class, create two employees (Employee object) and print the annual salary of each employee (object). Then set the new salary by giving each employee a 10% raise and display each employee's annual salary on the screen again.
Note: code should be written in java language.
//Employee.java
class Employee
{
String FirstName;
String LastName;
double monthly_salary;
Employee(String fname,String lname,double sal)
{
FirstName=fname;
LastName=lname;
monthly_salary=sal;
}
void setFirstName(String s)
{
FirstName=s;
}
void setLastName(String s)
{
LastName=s;
}
void setSalary(double sal)
{
if(sal>0)
monthly_salary=sal;
else
System.out.println("Salary must be positive to set");
}
void updateSalary()
{
monthly_salary=monthly_salary+monthly_salary*0.10;
}
String getFname(){
return FirstName;
}
String getLname()
{
return LastName;
}
double getSalary(){
return monthly_salary;
}
public String toString()
{
return "First Name :"+this.getFname()+"\nLast Name :
"+this.getLname()+"\nMonthly Salary :
"+this.getSalary()+"\nAnnualSalary: "+(12)*(
this.getSalary());
}
}
//EmployeeTest.java
public class EmployeeTest{
public static void main (String[] args) {
Employee e1=new Employee("Alice","Mary",54300.00);
Employee e2=new Employee("John","BOB",54320.34);
System.out.println("\nEmployee 1 details:\n");
System.out.println(e1);
System.out.println("\nEmployee 2 details :\n");
System.out.println(e2);
//update salary by 10%
e1.updateSalary();
e2.updateSalary();
System.out.println("\nEmployee 1 details after 10% increase of
salary:\n");
System.out.println(e1);
System.out.println("\nEmployee 2 details after 10% increase of
salary : \n");
System.out.println(e2);
}
}
Get Answers For Free
Most questions answered within 1 hours.