Respuesta :
Answer:
public class Employee {
private String Fname;
private String Lname;
private double monthlySalary;
//Constructor
public Employee(String fname, String lname, double monthlySalary) {
Fname = fname;
Lname = lname;
this.monthlySalary = monthlySalary;
}
//Set and Get Methods
public String getFname() {
return Fname;
}
public void setFname(String fname) {
Fname = fname;
}
public String getLname() {
return Lname;
}
public void setLname(String lname) {
Lname = lname;
}
public double getMonthlySalary() {
return monthlySalary;
}
public void setMonthlySalary(double monthlySalary) {
if(monthlySalary>0) {
this.monthlySalary = monthlySalary;
}
}
}
public class EmployeeTest {
public static void main(String[] args) {
Employee employeeOne = new Employee("Dave","Jones",1800);
Employee employeeTwo = new Employee("Junely","Peters",1200);
double empOnecurrentYrSalary =(employeeOne.getMonthlySalary()*12);
System.out.println("Employee One Yearly salary is " +
""+empOnecurrentYrSalary);
double empTwocurrentYrSalary =(employeeTwo.getMonthlySalary()*12);
System.out.println("Employee Two Yearly salary is " +
""+empTwocurrentYrSalary);
// Incremments of 10%
double newSalaryOne = empOnecurrentYrSalary+(empOnecurrentYrSalary*0.1);
double newSalaryTwo = empTwocurrentYrSalary+(empTwocurrentYrSalary*0.1);
employeeOne.setMonthlySalary(newSalaryOne);
employeeTwo.setMonthlySalary(newSalaryTwo);
System.out.println("Employee One's New Yearly Salary is " +
""+(employeeOne.getMonthlySalary()));
System.out.println("Employee Two's New Yearly Salary is " +
""+(employeeTwo.getMonthlySalary()));
}
}
Explanation:
As required by the question Two Java classes are created Employee and EmployeeTest
The required fields, constructor, get and set methods are all created in the Employee class, pay attention to the comments in the code
In the EmployeeTest, two objects of the class are created as required by the question.
The current salaries are printed as initialized by the constructor
Then an increase of 10% is added and the new salary is printed.
Note the use of the setMonthlySalary and getMonthlySalary for the update and display of the salaries respectively