Design a Payroll class that has fields for an employee\'s name, ID number, hourl
ID: 3885986 • Letter: D
Question
Design a Payroll class that has fields for an employee's name, ID number, hourly pay rate, and number of hours worked. Write the appropriate accessor and mutator methods and a constructor that accepts the employee's name and ID number as arguments. The class should also have a method that returns the employee's gross pay, which is calculated as the number of hours worked multiplied by the hourly pay rate. Write a program that demonstrates the class by creating a Payroll object, then asking the user to enter the data for an employee. The program should display the amount of gross pay earned.Explanation / Answer
//Payroll class
import java.util.*;
import java.util.Scanner;
public class Payroll{
private String employeeName;
private int idNumber;
private double hrlypayrate;
private int hoursWorked;
//constructor
Payroll(){
}
Payroll(String employeeName,int idNumber){
this.employeeName = employeeName;
this.idNumber = idNumber;
}
//Accessors
public String getemployeeName(){
return employeeName;
}
public int getidNumber(){
return idNumber;
}
public int gethoursWorked(){
return hoursWorked;
}
public int gethrlypayrate(){
return hrlypayrate;
}
//Mutators
public void setemployeeName(){
this.employeeName = employeeName;
}
public void setidNumber(){
this.idNumber = idNumber;
}
public void sethoursWorked(){
this.hoursWorked = hoursWorked;
}
public void sethrlypayrate(){
this.hrlypayrate = hrlypayrate;
}
public double employeeGrosspay(){
return (gethoursWorked()*gethrlypayrate());
}
}
// Payrollclass class which contains main function
import java.util.*;
import java.util.Scanner;
public class Payrollclass{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("ENter employee name");
String name = sc.nextLine();
System.out.println("ENter employee id");
int id = sc.nextInt();
Payroll payroll = new Payroll(name,id);
System.out.println("ENter employee hourly pay rate ");
double hrlypayrate = sc.nextDouble();
System.out.println("ENter employee hours worked ");
int hoursWorked = sc.nextInt();
double grosspay = payroll.employeeGorsspay();
System.out.println("Employees Gross pay is "+grosspay);
}
}