Create a class called Employee that includes three pieces of information as instance variables — a first name (type string), a last name (type string) and a monthly salary (double). Your class should have two constructors, one default and one that initializes the three values. Provide the properties with a get and set accessor for all instance variables. If the monthly salary is negative, the set accessor should leave the instance variable unchanged.
Write a test application 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.
Programming language.
Full Answer Section
Java
public
class
Employee
{
private String firstName;
private String lastName;
private
double monthlySalary;
public
Employee() {
}
public
Employee(String firstName, String lastName, double monthlySalary)
{
this.firstName = firstName;
this.lastName = lastName;
if (monthlySalary >= 0) {
this.monthlySalary = monthlySalary;
}
}
public String getFirstName()
{
return firstName;
}
public
void
setFirstName(String firstName)
{
this.firstName = firstName;
}
public String getLastName()
{
return lastName;
}
public
void
setLastName(String lastName)
{
this.lastName = lastName;
}
public double
getMonthlySalary()
{
return monthlySalary;
}
public
void
setMonthlySalary(double monthlySalary)
{
if (monthlySalary >= 0) {
this.monthlySalary = monthlySalary;
}
}
public
double
getYearlySalary() {
return monthlySalary * 12;
}
}
public
class
EmployeeTest
{
public
static
void
main(String[] args)
{
Employee employee1 = new Employee("John", "Doe", 5000);
Employee employee2 = new Employee("Jane", "Smith", 6000);
System.out.println("Employee 1:");
System.out.println("First Name:", employee1.getFirstName());
System.out.println("Last Name:", employee1.getLastName());
System.out.println("Monthly Salary:", employee1.getMonthlySalary());
System.out.println("Yearly Salary:", employee1.getYearlySalary());
System.out.println("\nEmployee 2:");
System.out.println("First Name:", employee2.getFirstName());
System.out.println("Last Name:", employee2.getLastName());
System.out.println("Monthly Salary:", employee2.getMonthlySalary());
System.out.println("Yearly Salary:", employee2.getYearlySalary());
System.out.println("\nGiving employees a 10% raise:");
employee1.setMonthlySalary(employee1.getMonthlySalary() * 1.1);
employee2.setMonthlySalary(employee2.getMonthlySalary() * 1.1);
System.out.println("\nEmployee 1:");
System.out.println("First Name:", employee1.getFirstName());
System.out.println("Last Name:", employee1.getLastName());
System.out.println("Monthly Salary:", employee1.getMonthlySalary());
System.out.println("Yearly Salary:", employee1.getYearlySalary());
System.out.println("\nEmployee 2:");
System.out.println("First Name:", employee2.getFirstName());
System.out.println("Last Name:", employee2.getLastName());
System.out.println("Monthly Salary:", employee2.getMonthlySalary());
System.out.println("Yearly Salary:", employee2.getYearlySalary());
}
}
This code will create two Employee objects and display each object's yearly salary. Then it will give each Employee a 10% raise and display each Employee's yearly salary again.