Create a class called Employee that includes three instance variables—a first na
ID: 3880069 • Letter: C
Question
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.
13 public class Employee 14 t 15 private String FirstName: 16 private String LastName: 17 private double MonthlySalary: 18 ge public Employee (String PirstName, String LastName, double Honthiysalary) 20 21 this. FirstName = FirstName ; this.LastNameLastName; this.MonthlySalary Monthlysalary: 23 24h 25 26 28 publio void setFirstName (String firstName) 29t 30 this FirstName FirstName:
Explanation / Answer
Your code is perfect !
To raise the salary by 10%, you just need to multiply the salary by 1.10 like employee1.setMonthSalary(employee1.getMonthlySalary() * 1.10);
This is what you are doing in the code. So its correct.
But there is one bug in the Employee class's setMonthlySalary(double salary); method. You are passing a parameter named salary , but are only accessing MonthlySalary member variable which is not correct. Your intention is to use the variabe name salary in the if condition(). It should be a typo. Also your code should not modify the member variable if the salary is not positive , according to the specification in the question. So please modify the setMonthlySalary(double salary) as follows
public void setMonthlySalary(double salary)
{
if(salary > 0)
{
this.MonthlySalary = salary;
}
}
Please do rate the answer if you found it helpful. Thank you.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.