Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

STEP 2: Create the Project You will want to use the Week 3 project as the starti

ID: 3640823 • Letter: S

Question

 


STEP 2: Create the Project


You will want to use the Week 3 project as the starting point for the lab. To do this, you will want to create a new project by following these steps:

Open the same workspace you created for the Week 3 project.
Create a new project and name it "CIS247B_WK4_Lab_LASTNAME".
Copy all the source files from the Week 3 project into the Week 4 project.
Before you move on to the next step, build and execute the Week 4 project.

 

 


STEP 3: Modify the Employee Class


Using the UML Diagrams from Step 1, create the Benefit class. To get an idea of how to format displayBenefits, take a look at the output in Step 5.
Add a Benefit attribute to the Employee class.
Initialize the new Benefit attribute in both Employee constructors. Again, take note of the multi-arg constructors parameter list!
Create the iEmployee interface.
Modify the Employee class to implement the new interface so that Employee will have to implement the calculatePay method.
Modify the Employee class to call displayBenefit when displaying Employee information.

 


STEP 4: Modify the Main Method


Notice that the Employee class now has a public benefit object inside it. This means that you can access the set methods of the Benefit object with the following code:

.benefit.
As an example, to set the lifeInsurance attribute inside an Employee object called emp, we could execute the following code:

emp.benefit.setLifeInsurance(lifeInsurance);
The steps required to modify the Main class are below. New steps are in bold.

Create an Employee object using the default constructor.

 
Prompt for and then set the first name, last name, and gender. Consider using your getInput method from Week 1 to obtain data from the user for this step as well as Step 3.

 
Prompt for and then set the dependents and annual salary using the overloaded setters that accept Strings.
Prompt for and set healthInsurance, lifeInsurance, and vacation.


Using your code from Week 1, display a divider that contains the string "Employee Information".
Display the Employee Information.


Display the number of employees created using getNumEmployees(). Remember to access getNumEmployees using the class name, not the Employee object.


Create a Benefit object called benefit1 using the multi-arg constructor. Use any information you want for health insurance, life insurance, and vacation.


Create another Employee object and use the constructor to fill it with the following:
"Mary", "Noia", 'F', 5, 24000.0, benefit1


Using your code from Week 1, display a divider that contains the string "Employee Information".
Display the employee information.


Display the number of employees created using getNumEmployees(). Remember to access getNumEmployees using the class name, not the Employee object.

 

 

 

Explanation / Answer

Employee class ****************** package employee; import java.text.DecimalFormat; public class Employee implements IEmployee { private String firstName; private String lastName; private char gender; private int dependents; public Benefit benefit = new Benefit(); public Benefit getBenefit() { return benefit; } public void setBenefit(Benefit benefit) { this.benefit = benefit; } private double annualSalary; private static int numEmployees = 0; public Employee() { numEmployees++; } public Employee(String firstName, String lastName, char gender, int dependents, double annualSalary, Benefit benefit) { this.firstName = firstName; this.lastName = lastName; this.gender = gender; this.dependents = dependents; this.annualSalary = annualSalary; this.benefit = benefit; numEmployees++; } 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 char getGender() { return gender; } public void setGender(char gender) { this.gender = gender; } public int getDependents() { return dependents; } public void setDependents(int dependents) { this.dependents = dependents; } public void setDependents(String dependents) { this.dependents = Integer.parseInt(dependents); } public double getAnnualSalary() { return annualSalary; } public void setAnnualSalary(double annualSalary) { this.annualSalary = annualSalary; } public void setAnnualSalary(String annualSalary) { this.annualSalary = Integer.parseInt(annualSalary); } public static int getNumEmployees() { return numEmployees; } public static void setNumEmployees(int numEmployees) { Employee.numEmployees = numEmployees; } public double calcPay() { return annualSalary; } public void displayEmpoyee(Employee employee) { System.out .println("*********************Employee Information*********************"); System.out.println("First Name: " + employee.getFirstName()); System.out.println("Last Name: " + employee.getLastName()); System.out.println("Gender: " + String.valueOf(employee.getGender())); System.out.println("Dependents: " + employee.getDependents()); double annSal = employee.getAnnualSalary(); DecimalFormat formatter = new DecimalFormat("#,###,###"); System.out.println("Annual Salary: $" + formatter.format(annSal)); double weekSal = annSal / 52; DecimalFormat decimalFormat = new DecimalFormat("#.##"); String weekSalary = decimalFormat.format(weekSal); System.out.println("Weekly Pay: $" + weekSalary); System.out.println("Total Employees: " + Employee.numEmployees); Benefit benef = employee.getBenefit(); benef.displayBenefits(employee.getBenefit()); } public static void main(String args[]) { Employee employee = new Employee(); java.util.Scanner console = new java.util.Scanner(System.in); System.out.println("Please enter Employee's first name"); String tempFName = console.next(); employee.setFirstName(tempFName); System.out.println("Please enter Employee's last name"); String tempLName = console.next(); employee.setLastName(tempLName); System.out.println("Please enter Employee's gender"); String tempGender = console.next(); employee.setGender(tempGender.charAt(0)); System.out.println("Please enter Employee's dependents"); String tempDepend = console.next(); employee.setDependents(tempDepend); System.out.println("Please enter Employee's annual salary"); String tempAnnSal = console.next(); employee.setAnnualSalary(tempAnnSal); System.out.println("Please enter Employee's health Insurance"); String tempHlthInsur = console.next(); employee.benefit.setHealthInsurance(tempHlthInsur); System.out.println("Please enter Employee's Life Insurance"); double tempLifeInsur = console.nextDouble(); employee.benefit.setLifeInsurance(tempLifeInsur); System.out.println("Please enter Employee's vacation details"); int tempVacation = console.nextInt(); employee.benefit.setVacation(tempVacation); employee.displayEmpoyee(employee); Benefit ben = new Benefit("AVA", 23.5, 6); Employee employee2 = null; employee2 = new Employee("Mary", "Noia", 'F', 5, 24000.0, ben); employee.displayEmpoyee(employee2); } @Override public double calculatePay() { // TODO Auto-generated method stub return 0; } } *************************************************************************************************************** Benefit class *************** package employee; public class Benefit { private String healthInsurance; private double lifeInsurance; private int vacation; public Benefit(){ } public Benefit(String healthInsurance, double lifeInsurance, int vacation){ this.healthInsurance = healthInsurance; this.lifeInsurance = lifeInsurance; this.vacation = vacation; } public String getHealthInsurance() { return healthInsurance; } public void setHealthInsurance(String healthInsurance) { this.healthInsurance = healthInsurance; } public double getLifeInsurance() { return lifeInsurance; } public void setLifeInsurance(double lifeInsurance) { this.lifeInsurance = lifeInsurance; } public int getVacation() { return vacation; } public void setVacation(int vacation) { this.vacation = vacation; } public void displayBenefits(Benefit benefit){ System.out.println("Health Insurance : "+benefit.getHealthInsurance()); System.out.println("Life Insurance : "+String.valueOf(benefit.getLifeInsurance())); System.out.println("Vacation : "+String.valueOf(benefit.getVacation())); } } *************************************************************************************************************** IEmloyee Interface ************************ package employee; public interface IEmployee { public double calculatePay(); } Sample Input: Please enter Employee's first name John Please enter Employee's last name Doe Please enter Employee's gender M Please enter Employee's dependents 2 Please enter Employee's annual salary 73000 Please enter Employee's health Insurance Partial Please enter Employee's Life Insurance 500 Please enter Employee's vacation details 10 Sample OutPut *********************Employee Information********************* First Name: John Last Name: Doe Gender: M Dependents: 2 Annual Salary: $73,000 Weekly Pay: $1403.85 Total Employees: 1 Health Insurance : Partial Life Insurance : 500.0 Vacation : 10 *********************Employee Information********************* First Name: Mary Last Name: Noia Gender: F Dependents: 5 Annual Salary: $24,000 Weekly Pay: $461.54 Total Employees: 2 Health Insurance : AVA Life Insurance : 23.5 Vacation : 6