design a payroll class that has fields for an employee\'s name, ID number, hourl
ID: 3546313 • Letter: D
Question
design a payroll class that has fields for an employee's name, ID number, hourly pay rate and the 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.
Java please :))
Explanation / Answer
import java.util.*;
class payroll
{
private String employee_name;
private int emp_ID;
private double pay_rate;
private int no_of_hours_worked;
public void setEmployee_name(String name)
{
employee_name = name;
}
public void setEmp_ID(int ID)
{
emp_ID = ID;
}
public void setPay_rate(double rate)
{
pay_rate = rate;
}
public void setNo_of_hours_worked(int hours)
{
no_of_hours_worked = hours;
}
public payroll(String name,int ID)
{
employee_name = name;
emp_ID = ID;
}
public double gross_pay()
{
return pay_rate*no_of_hours_worked;
}
}
class payrollTest
{
public static void main(String[] args)
{
String emp_name="";
int no=0;
int hours=0;
double pay_rate=0;
Scanner in = new Scanner(System.in);
System.out.println("Enter Employee Name :");
emp_name = in.next();
System.out.println("Enter Employee Number :");
no = in.nextInt();
System.out.println("Enter Employee No of hours worked :");
hours = in.nextInt();
System.out.println("Enter Employee Pay Rate :");
pay_rate = in.nextDouble();
payroll P1 = new payroll(emp_name,no);
P1.setNo_of_hours_worked(hours);
P1.setPay_rate(pay_rate);
System.out.println("Employee Gross Pay given by "+P1.gross_pay());
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.