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 variable .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 demonstrate 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 display each employee’s yearly salary again.
public class Employee {
private String firstName;
private String lastName;
private double monthlySalary;
public Employee(String firstName, String lastName, double monthlySalary) {
this.firstName = firstName;
this.lastName = lastName;
if (monthlySalary >= 0) {
this.monthlySalary = monthlySalary;
}
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getFirstName() {
return firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getLastName() {
return lastName;
}
public void setMonthlySalary(double monthlySalary) {
if (monthlySalary >= 0) {
this.monthlySalary = monthlySalary;
}
}
public double getMonthlySalary() {
return monthlySalary;
}
}
public class EmployeeTest {
public static void main(String[] args) {
Employee joe = new Employee("Joe", "Rogan", 9696);
Employee tod = new Employee("Tod", "Howard", 8595);
System.out.println(joe.getFirstName() + "'s monthly salary is " + joe.getMonthlySalary());
System.out.println(tod.getFirstName() + "'s monthly salary is " + tod.getMonthlySalary());
joe.setMonthlySalary(joe.getMonthlySalary() * 1.1);
tod.setMonthlySalary(tod.getMonthlySalary() * 1.1);
System.out.println("Salary after 10% boost");
System.out.println(joe.getFirstName() + "'s monthly salary is " + joe.getMonthlySalary());
System.out.println(tod.getFirstName() + "'s monthly salary is " + tod.getMonthlySalary());
}
}
Comments
Hellow let me say thank
Leave a comment