3.13 ( Employee Class) Create a class called Employee that includes three instan
ID: 3851029 • Letter: 3
Question
3.13 (Employee Class) 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 con- structor 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 Employ- eeTest 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.
Explanation / Answer
/**
* The java program that tests the Employee class
* and its methods and print results to console.
* */
//EmployeeTest.java
public class EmployeeTest {
public static void main(String[] args) {
//Craete two instances of Employee class
Employee emp1=new Employee("John", "Shek", 12000);
Employee emp2=new Employee("Mark", "Polo", 10000);
final int NUMBER_OF_MONTHS=12;
//print yearly salary
System.out.printf("emp1 yearly salary %5.2f ",
emp1.getMonthlySalary()*NUMBER_OF_MONTHS);
System.out.printf("emp2 yearly salary %5.2f ",
emp2.getMonthlySalary()*NUMBER_OF_MONTHS);
//print salary 10 % on monthly salary
emp1.raiseSalary();
emp2.raiseSalary();
//print new yearly salary
System.out.printf("emp1 new yearly salary %5.2f ",
emp1.getMonthlySalary()*NUMBER_OF_MONTHS);
System.out.printf("emp2 new yearly salary %5.2f ",
emp2.getMonthlySalary()*NUMBER_OF_MONTHS);
}
}
--------------------------------------------------------------------------------------------------------
//Employee.java
public class Employee {
private String firstName;
private String lastName;
private double monthlySalary;
//constructor to set class variables
public Employee(String firstName,String lastName,
double monthlySalary) {
this.firstName=firstName;
this.lastName=lastName;
this.monthlySalary=monthlySalary;
}
//Setter methods of class instace variables
public void setFirstName(String firstName){
this.firstName=firstName;
}
public void setLastName(String lastName){
this.lastName=lastName;
}
public void setMonthlySalary(double monthlySalary){
this.monthlySalary=monthlySalary;
}
public void raiseSalary()
{
monthlySalary=monthlySalary+monthlySalary+0.10;
}
//Getter methods of class instace variables
public String getFirstName(){
return firstName;
}
public String getLastName(){
return lastName;
}
public double getMonthlySalary(){
return monthlySalary;
}
}//end of Employee class
--------------------------------------------------------------------------------------------------------
Sample Output:
emp1 yearly salary 144000.00
emp2 yearly salary 120000.00
emp1 new yearly salary 288001.20
emp2 new yearly salary 240001.20
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.