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

STEP 1: Understand the UML Diagram Back to top Notice the change in UML diagram.

ID: 3641179 • Letter: S

Question

STEP 1: Understand the UML Diagram
Back to top

Notice the change in UML diagram. It is common practice to leave out the accessors and mutators (getters and setters) from UML class diagrams, since there can be so many of them. Unless otherwise specified, it is assumed that there is an accessor (getter) and a mutator (setter) for every class attribute.

Employee #firstName : string #lastName : string #gender : char #dependents : int #annualSalary : double #benefit : Benefit -static numEmployees : int = 0 +Employee() +Employee(in fname : string, in lname : string, in gen : char, in dep : int, in benefits : Benefit) +static getNumEmployees() : int +CalculatePay() : double +displayEmployee() : void Benefit -healthinsurance : string -lifeinsurance : double -vacation : int +Benefit() +Benefit(in hins : string, in lins : double, in vac : int) +displayBenefits() : void Salaried -MIN_MANAGEMENT_LEVEL : int = 0 -MAX_MANAGEMENT_LEVEL : int = 3 -BONUS_PERCENT : double = 10 -managementLevel : int +Salaried() +Salaried(in fname : string, in lname : string, in gen : char, in dep : int, in sal : double, in ben : Benefit, in manLevel : int) +Salaried(in sal : double, in manLevel : int) +CalculatePay() : double +displayEmployee() : void Hourly -MIN_WAGE : double = 10 -MAX_WAGE : double = 75 -MIN_HOURS : double = 0 -MAX_HOURS: double = 50 -wage : double -hours : double -category : string +Hourly() +Hourly(in wage : double, in hours : double, in category : string) +Hourly(in fname : string, in lname : string, in gen : char, in dep : int, in wage : double, in hours : double, in ben : Benefit, in category : string) +CalculatePay() : double +displayEmployee() : void

STEP 2: Create the Project
Back to top

Create a new project and name it CIS247B_WK5_Lab_LASTNAME. Copy all the source files from the Week 4 project into the Week 5 project.

Before you move on to the next step, build and execute the Week 5 project.

STEP 3: Modify the Benefits Class
Back to top

All classes that are contained in Java's API as well as any external classes that you or I may create are derived from the java.lang.object class. The Object class contains several methods which are inherited by all other Java classes. One of these methods is called toString. The toString method can be overridden by any class and its purpose is to display an object’s current state. This type of functionality should sound familiar to you. After all, your displayBenefits method was designed to print the current state of a Benefit object! In this week's lab, we are going to move the logic contained in displayBenefits to the toString method. Take a look at Java’s description of the toString method contained in the Object class and pay particular attention to its return type before moving on.

Change the displayBenefits method to override the java.lang.object toString method by simply changing its name from displayBenefits to toString.
Ensure the toString method returns a String and does not explicitly display the state information to the console. Remember, toString does not display information but instead it simply returns a string.

STEP 4: Modify the Employee Class
Back to top

Java provides three explicit access modifiers for attributes and methods. So far, we have dealt with two of them: public and private. This week, we will use a new access modifier: protected. Java states that "The protected modifier specifies that the member can only be accessed within its own package (as with package-private) and, in addition, by a subclass of its class in another package." For additional information on the use of protected as an access modifier, review the Controlling Access to Members of a Class tutorial.

Using the updated Employee class diagram, modify attributes to be protected as necessary.
Change the name of the displayEmployee method to "toString" and modify the new toString method to return an Employee's information as a String. This overrides the java.lang.object toString method.
Delete the iEmployee interface class, and remove the reference from the Employee class.

STEP 5: Create the Salaried Class
Back to top

In this step, it is necessary to implement constant attributes. As the name implies, constants contain values that do not change. In Java, an attribute is made into a constant by adding the keywords "static final" in the declaration. For additional information on the creation and use of constants, review the Understanding Instance and Class Members tutorial.

One other very important concept to review for this step in the lab is the use of the super method. Super is used to access parent-defined attributes and methods within a subclass. A common practice is to use the code super() or super(arg-list) to invoke the constructor in a parent class. For additional information on the use of super in your application and specifically this week’s constructors, review the Using the Keyword super tutorial.

Using the UML Diagrams from Step 1, create the Salaried classes, ensuring to specify that the Salary class inherits from the Employee class.
For each of the constructors listed in the Salaried class, ensure to invoke the appropriate superclass constructor and pass the correct arguments to the superclass constructor. This will initialize the protected attributes and update the numEmployees counter. Don’t forget to initialize the attributes for Salaried as well!
The valid management levels are 0, 1, 2, and 3, and the min and max management level attributes should be implemented as constants. Make sure to enforce this set of valid values in the management level setter.
Override the calculatePay method to add a 10 percent bonus to the annual salary for each of the management levels (i.e., bonus percentage = managementLevel * BONUS_PERCENT). The bonus percentage should be implemented as a constant. Also remember, the value returned from calculatePay should be equal to an employee's weekly pay.
Override the toString method to add the management level to the employee information. Don't forget to call Employee's toString method to capture the state of the inherited Employee attributes!

STEP 6: Create the Hourly Class
Back to top

Using the UML Diagrams from Step 1, create the Hourly class, ensuring to specify that the Hourly class inherits from the Employee class.
For each of the constructors listed in the Hourly class, ensure to invoke the appropriate base class constructor and pass the correct arguments to the base class constructor. This will initialize the protected attributes and update the numEmployees counter.
The valid category types are "temporary", "part time", and "full time". (Hint: The use of String.equalsIgnoreCase may be useful when setting the value of category. Search through Java's API for more details on the use of equalsIgnoreCase.)
The value for hours must be greater than or equal to 0 and less than or equal to 50. The limits should be implemented as constants.
The provided wage must be between 10 and 75 inclusive, and the limits should be implemented as constants.
Make sure to update the value of annualSalary appropriately every time the value of wage or hours changes. Remember, an Hourly employee's annual salary is calculated using the following formula: wage * hours * 52. Therefore, any change to wage or hours will affect the employee's annual salary!
Override the toString method to add the category to the hourly employee information.

STEP 7: Construct the Main Method
Back to top

Using previous weeks' assignments as an example, create at least one Employee, Hourly, and Salaried employee.
For each object created, write statements to exercise each of the public methods listed in the class diagram.
For each object created, invoke the object's toString method to display the employee's information.
For each object created, display the number of employees created.

STEP 8: Compile and Test
Back to top

When done, compile and execute your code, and debug any errors until your code is error-free.

Check your output to ensure that you have the desired output, modify your code as necessary, and rebuild.

Your output should resemble the following. Make sure to fully exercise all of your new and overridden subclass methods. This could result in output that is lengthier than the example below.

Screenshot of program that reads: ************** Employee Information ************** First Name: George Last Name: Anderson Gender: M Dependents: 5 Annual Salary: $42,000.00 Weekly Pay: $807.69 Health Insurance: Full Life Insurance: 2000.0 Vacation: 10 Total employees: 1 *************** Employee Information *************** First Name: Mary Last Name: Noia Gender: F Dependents: 5 Annual Salary: $24,000.00 Weekly Pay: $507.69 Health Insurance: Full Life Insurance: 1000.0 Vacation: 5 Management Level: 1 Total employees: 2*************** Employee Information *************** First Name: Frank Last Name: Jones Gender: M Dependents: 3 Annual Salary: $23,400.00 Weekly Pay: $450.00 Health Insurance: None Life Insurance: 500.0 Vacation: 3 Category: Part-time Total employees: 3*************** Revised Employee Information *************** First Name: Frank Last Name: Jones Gender: M Dependents: 3 Annual Salary: $33,280.00 Weekly Pay: $640.00 Health Insurance: None Life Insurance: 500.0 Vacation: 3 Category: Part-time Total employees: 3

STEP 9: Submit Deliverables

Explanation / Answer

employee.java ******************** package employee; import java.text.DecimalFormat; public class Employee { 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 double weeklySalary; public double getWeeklySalary() { return weeklySalary; } public void setWeeklySalary(double weeklySalary) { this.weeklySalary = weeklySalary; } 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 String toString() { super.toString(); double annSal = getAnnualSalary(); DecimalFormat formatter = new DecimalFormat("#,###,###"); DecimalFormat decimalFormat = new DecimalFormat("#.##"); String weekSalary = decimalFormat.format(getWeeklySalary()); Benefit benef = getBenefit(); return "*********************Employee Information*********************" +" First Name: " + getFirstName() + " Last Name:= " + getLastName() + " Gender: " + String.valueOf(getGender()) +" Dependents: " + getDependents() +" Annual Salary: $" + formatter.format(annSal) +" Weekly Pay: $" + weekSalary +" Health Insurance : "+benef.getHealthInsurance() +" Life Insurance : "+String.valueOf(benef.getLifeInsurance()) +" Vacation : "+String.valueOf(benef.getVacation()); } 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.setWeeklySalary(Double.parseDouble(tempAnnSal)/52); System.out.println(employee); System.out.println(" Total Employees: "+Employee.numEmployees); Benefit ben = new Benefit("FULL", 1000.0, 5); Salaried salaried = new Salaried("Mary", "Noia", 'F', 5,24000, ben,1); double weekly_pay = salaried.calculatePay(); salaried.setWeeklySalary(weekly_pay); System.out.println(salaried); System.out.println(" Total Employees: "+numEmployees); Benefit ben2 = new Benefit("None", 500.0, 3); String category = "Part-Time"; double annSal = 20.0*20.0*52; Hourly hourly = new Hourly("Frank", "Johnes", 'M', 3, annSal,ben2,20.0,20.0,category ); hourly.setWeeklySalary(annSal/52); System.out.println(hourly); System.out.println(" Total Employees: "+numEmployees); } public double calculatePay() { return 0; } } Hourly.java *************** package employee; public class Hourly extends Employee { static final double MIN_WAGE = 10; static final double MAX_WAGE = 75; static final double MIN_HOURS = 0; static final double MAX_HOURS = 50; static final String[] categoryArray = {"temporary", "part time","full time"}; private double wage; private double hours; private String category; public Hourly(){ super(); } public Hourly(double wage, double hours, String category){ super(); this.wage= wage; this.hours = hours; this.category = category; } public Hourly(String firstName, String lastName, char gender, int dependents, double annualSalary, Benefit benefit,double wage, double hours, String category){ super(firstName,lastName,gender,dependents,annualSalary,benefit); this.wage= wage; this.hours = hours; this.category = category; } public String toString(){ String str = super.toString(); return str + " Category :"+category; } public double getWage() { return wage; } public void setWage(double wage) { if(wage>=10&& wage