Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please fix hourlyEmployee.java and SalariedEmployee.java, Thanks! Employee.java (Completed) HourlyEmployee.java SalariedEmployee.java HumanResource.java (Completed) Employee is the superclass, and both HourlyEmployee and SalariedEmployee extend the Employee class.

Please fix hourlyEmployee.java and SalariedEmployee.java, Thanks!

Employee.java (Completed)

HourlyEmployee.java

SalariedEmployee.java

HumanResource.java (Completed)

Employee is the superclass, and both HourlyEmployee and SalariedEmployee extend the Employee class.

(1) Complete the definition of the SalariedEmployee class following the instructions in the code.

(2) In order for the company to pay its employees weekly, the getWeeklyPay() method is added to Employee, HourlyEmployee and SalariedEmployee. The method in Employee class has been completed. You need to complete the method definition in the HourlyEmployee class and the SalariedEmployee class.

(3) In HumanResource.java, an ArrayList of Employee objects are created, and two HourlyEmployee objects and two SalariedEmployee objects are added to the ArrayList. Run the program and check out the output to understand how polymorphism works.

public class Employee { private String name; private String department; private String id; // Default constructor. Set protected variables to the empty string or 0 public Employee() { name = ""; department = ""; id = ""; } // Constructor with parameters to set the private variables public Employee(String name, String department, String id) { this.name = name; this.department = department; this.id = id; } public String getName() { return name; } public String getDepartment() { return department; } public String getId() { return id; } public void setDepartment(String department) { this.department = department; } public double getWeeklyPay() { return 0.0; } public String toString() { return "Name: " + name + ", Department: " + department + ", ID: " + id; } }

---------------------------------------------------------------------------------------------------------------

import java.util.Scanner; public class HourlyEmployee extends Employee {

private double hourlyRate;

/*FIXME(2) Complete the default constructor to set hourlyRate to 0.0*/

public HourlyEmployee() {

super(); hourlyRate=0.0;

}

/*FIXME(3) Complete the argumented constructor to set each field properly

Note that you can call the superclass constructor to initialize the fields defined in the superclass

The second video in Canvas shows an example on this.*/

public HourlyEmployee(String name, String department,

String id, double rate) {

super(name,department,id);

setHourlyRate(rate);

}

/*FIXME(4) Complete the definition of the accessor for hourlyRate*/

public double getHourlyRate() {

return hourlyRate;

}

/*FIXME(5) Complete the definition of the mutator for hourlyRate*/

public void setHourlyRate(double rate) {

hourlyRate= ( rate < 0.0 ) ? 0.0 : rate;

}

/*FIXME(6) Complete the definition of the toString method,

which will return a String includes name, department, id, and hourlyRate of the employee

Note that you can call the overriden toString() method defined in the superclass and append the information about the hourlyRate

Example: Name: John Smith, Department: Sales, ID: jsmith1, Hourly Rate: $18.2*/

@Override

public String toString() {

return "hourly employee: "

+ super.toString()

+ String.format(" hourly wage: $%.2f ",

getHourlyRate());

}

}

--------------------------------------------------------------------------------------------------------

public class SalariedEmployee extends Employee{ private double annualSalary; /*FIXME(1) Complete the default constructor to set annualSalary to 0.0*/ public SalariedEmployee() { } /*FIXME(2) Complete the argumented constructor to set each field properly Note that you can call the super class constructor to initialize the fields defined in the super class*/ public SalariedEmployee(String name, String department, String id, double salary) { } /*FIXME(3) Complete the definition of the accessor for annualSalary*/ public double getAnnualSalary() { } /*FIXME(4) Complete the definition of the mutator for annualSalary*/ public void setAnnualSalary(double salary) { } /*FIXME(5) Complete the definition of getweeklyPay(). Assume that there are 52 weeks per year.*/ @Override public double getWeeklyPay() { } /*FIXME(6) Complete the definition of the toString method, which will return a String includes name, department, id, and annualSalary of the employee Note that you can call the toString() method defined in the superclass. Example: Name: Kevin Johnson, Department: Accounting, ID: kjohnson1, Annual Salary: $60000.0*/ @Override public String toString() { } }

----------------------------------------------------------------------------------------------------------------------

import java.util.ArrayList; public class HumanResource{ public static void main(String[] args) { ArrayList employees = new ArrayList(); HourlyEmployee e1 = new HourlyEmployee("John Smith", "Sales", "jsmith1", 18.2); employees.add(e1); SalariedEmployee e2 = new SalariedEmployee("Amy Brown", "Marketing", "abrown3", 45000); employees.add(e2); SalariedEmployee e3 = new SalariedEmployee("Henry Chuck", "Accounting", "cchuck2", 50000.0); employees.add(e3); SalariedEmployee e4 = new SalariedEmployee("Mary Miller", "IT", "mmiller5", 75000); employees.add(e4); for(int i = 0; i < employees.size(); i++) { System.out.println(employees.get(i).getName() + " $" + employees.get(i).getWeeklyPay()); } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Driven Web Sites

Authors: Joline Morrison, Mike Morrison

2nd Edition

? 061906448X, 978-0619064488

More Books

Students also viewed these Databases questions