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 a constructor that initializes the three instance variables. Provide a set and a get method for each instance variable. 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.
Updated: April 11, 2021 — Training Time: 3 minutes
Overseen by: Archangel Macsika
Difficulty: Advance.
Companies who previously asked this: -.
Objective: Create a class called Employee
that includes three pieces of information as instance variables a firstName
(type String
), a lastName
(type String
) and a monthly salary
(type double
). Your class should have a constructor that initializes the three instance variables. Provide a setter and a getter method for each instance variable. 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.
Input: Name of employee and salary.
Expected Output: Displays the yearly salary, and the yearly salary after a 10% raise based on the user input.
Sikademy Solution
Employee.java
package sikademy;
/**
*
* @author Archangel Macsika
*/
// This class holds the information about an employee It has setter and getter methods to set the values and retrieve the values of the instance variables
public class Employees {
private String firstName;
private String lastName;
private double salary = 0;
// The default constructor
public Employees() {
}
// This parametrized constructor is used to set the values of instance variables
public Employees(String first, String last, double salary) {
this.firstName = first;
this.lastName = last;
this.salary = salary;
}
// This function is used to get the first name
public String getFirstName() {
return firstName;
}
// This function sets the first name
public void setFirstName(String firstName) {
this.firstName = firstName;
}
// This method is used to get the last name
public String getLastName() {
return lastName;
}
// This method sets the last name
public void setLastName(String lastName) {
this.lastName = lastName;
}
// This method is used to get the monthly salary
public double getSalary() {
return salary;
}
// This method sets the monthly salary
public void setSalary(double salary) {
this.salary = salary;
}
}
EmployeeTest.java
package sikademy;
/**
*
* @author Archangel Macsika
*/
// This class demonstrates the capabilities of Employee Class
public class EmployeeTest{
// This method displays the yearly salary of the Employee objects
public void display(Employees emp){
System.out.println("The yearly salary of " + emp.getFirstName() + " " + emp.getLastName() + " is: \t" + emp.getSalary()*12);
}
// This method is used to set the raised value of salary in Employee class
public void setRaise(Employees emp){
emp.setSalary(emp.getSalary()*1.1);
}
public static void main(String[] args) {
Employees employee1=new Employees("Archangel", "Macsika",1000);
Employees employee2=new Employees("Nsikak", "Imoh",1000);
EmployeeTest emptest=new EmployeeTest();
emptest.display(employee1);
emptest.display(employee2);
emptest.setRaise(employee1);
emptest.setRaise(employee2);
emptest.display(employee1);
emptest.display(employee2);
}
}