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

Create an Employee class for a basic payroll program to compute the net pay sala

ID: 3904079 • Letter: C

Question

Create an Employee class for a basic payroll program to compute the net pay salary of hourly based employees. Your program should also find the average net pay for a small company. To define the class, include the appropriate data members, member functions, and access modifiers. For simplicity, use a constant tax rate of 30% to compute the tax amount. Employees that work over 40 hours will receive overtime pay of one and a half of their hourly rate for overtime hours worked. The output should display the name of each employee, hours worked, hourly rate, overtime pay, regular (gross) pay, tax amount, and net pay. The average net pay of all employees should also be displayed.

Explanation / Answer

class Employee
{
private String name;
private double netPay;
private int hoursWorked;
private double hourlyRate;

public Employee(String name,int hoursWorked,double hourlyRate)//constructor
{
this.name = name;
this.hoursWorked = hoursWorked;
this.hourlyRate = hourlyRate;
}

public double computeGrossPay()
{
double grossPay;
if(hoursWorked <= 40)
grossPay = hoursWorked*hourlyRate;
else
grossPay = 40*hourlyRate + computeOverTimePay();
return grossPay;
}

public double computeTax()
{
return computeGrossPay()*0.3;
}
public double computeNetPay()
{
return computeGrossPay()- computeTax();
}

public double computeOverTimePay()
{

if(hoursWorked > 40)
return 1.5*(hoursWorked-40)*hourlyRate;
else
return 0;
}
public String toString()
{
return "Employee Name : "+name
+ " Hours Worked : "+hoursWorked
+ " Hourly Rate : "+hourlyRate
+ " Overtime Pay : $"+computeOverTimePay()
+ " Gross Pay : $"+computeGrossPay()
+ " Tax Amount : $"+computeTax()
+ " Net Pay : $"+computeNetPay();
}
}

class TestEmployee
{
public static void main (String[] args)
{
Employee emp = new Employee("John Smith",43,4.5);

System.out.println(emp);

Employee emp1 = new Employee("Samara Williams",39,3.5);

System.out.println(emp1);

System.out.println("Average salary : $"+(emp.computeNetPay()+ emp1.computeNetPay())/2);
}
}

Output:

Employee Name : John Smith
Hours Worked : 43
Hourly Rate : 4.5
Overtime Pay : $20.25
Gross Pay : $200.25
Tax Amount : $60.074999999999996
Net Pay : $140.175
Employee Name : Samara Williams
Hours Worked : 39
Hourly Rate : 3.5
Overtime Pay : $0.0
Gross Pay : $136.5
Tax Amount : $40.949999999999996
Net Pay : $95.55000000000001
Average salary : $117.86250000000001

Do ask if any doubt. Please upvote

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote