- static numEmployees: int = 0 The following behaviors have been added: + static
ID: 3640549 • Letter: #
Question
- static numEmployees: int = 0
The following behaviors have been added:
+ static getNumEmployees( ) : int
+ setDependents(in dep : String) : void
+ setAnnualSalary(in sal : String) : void
STEP 2: Create the Project in Java
You will want to use the Week 2 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 2 project.
Create a new project named "CIS247B_WK3_Lab_LASTNAME". An empty project will then be created.
Copy your main class and employee class files from Week 2 and paste them into the Week 3 project.
Before you move on to the next step, build and execute the Week 3 project.
For each week's assignments, you will follow these steps create a new project that reuses the program from the previous week.
STEP 3: Modify the Employee
Using the UML Diagrams from Step 1, code the changes to the Employee class.
Create a static numEmployees variable and initialize it to zero.
Increment numEmployees by 1 in each of the constructors.
Create an overloaded setDependents method and, this time, make the parameter a string.
Create an overloaded setAnnualSalary method and, this time, make the parameter a string.
Remember that you will have to convert the string in the above two "set" methods to the data type of the attribute.
Make the getNumEmployees a static method. (This way, you can call it with the class name instead of an object name.)
Be sure you follow proper commenting and programming styles (indentation, line spacing, etc.).
STEP 4: Modify the Main Method
In the Main class, create code statements that perform the following operations. Be sure you follow proper commenting and programming styles (header, indentation, line spacing, etc.). Note that several of the steps below were accomplished in last week's assignment. 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 dependents and annual salary using the new overloaded setters.
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 second Employee object using the multi-arg constructor, setting each of the attributes with the following values: "Mary", "Noia", 'F', 5, 24000.0
Using your code from Week 1, display a divider that contains the string "Employee Information".
Display the employee information for the second Employee object.
Display the number of employees created using getNumEmployees. Remember to access getNumEmployees using the class name, not the Employee object.
STEP 5: Compile and Test
When done, compile and run your code. 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:
Explanation / Answer
Java class package employee; import java.text.DecimalFormat; public class Employee { private String firstName; private String lastName; private char gender; private int dependents; private double annualSalary; private static int numEmployees = 0; public Employee() { numEmployees++; } public Employee(String firstName, String lastName, char gender, int dependents, double annualSalary) { this.firstName = firstName; this.lastName = lastName; this.gender = gender; this.dependents = dependents; this.annualSalary = annualSalary; 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: "+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); } 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); employee.displayEmpoyee(employee); Employee employee2 = new Employee("Mary", "Noia", 'F', 5, 24000.0); employee.displayEmpoyee(employee2); } } ******************************************************************************************************* Sample OutPut: 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 7 Please enter Employee's annual salary 32500 *********************Employee Information********************* First Name: John Last Name: Doe Gender: M Dependents: 7 Annual Salary: $32,500 Weekly Pay: $625 Total Employees: 1 *********************Employee Information********************* First Name: Mary Last Name: Noia Gender: F Dependents: 5 Annual Salary: $24,000 Weekly Pay: $461.54 Total Employees: 2
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.