Answer:
import java.util.Scanner;
public class Payroll {
//set variable field
private String name;
private int idNumber;
private double hourlyRate;
private int hoursWorked;
private double grossPay;
//methods to get values of private class variables
public String getName()
{
return name;
}
public int getIdNumber()
{
return idNumber;
}
public double getHourlyRate()
{
return hourlyRate;
}
public int getHoursWorked()
{
return hoursWorked;
}
public double getGrossPay()
{
return hoursWorked * hourlyRate;
}
//methods to initialize or change the private class values.
public void setName( String nameGiven)
{
name = nameGiven;
}
public void setIdNumber(int idNumberGiven)
{
idNumber = idNumberGiven;
}
public void setHourlyRate(double rateGiven)
{
hourlyRate = rateGiven;
}
public void setHoursWorked(int hoursGiven)
{
hoursWorked = hoursGiven;
}
//Constructors
public Payroll(String nameGiven, int idNumberGiven, double rateGiven, int hoursGiven)
{
name = nameGiven;
idNumber = idNumberGiven;
hourlyRate = rateGiven;
hoursWorked = hoursGiven;
}
public static void main(String[] args)
{
double userGrossPay;
String userEmplName;
int userIdNum;
double userRate;
int userHours;
Scanner scanner = new Scanner(System.in);
System.out.print("Enter employee's name:");
userEmplName = scanner.nextLine();
System.out.print("Enter employee's ID number:");
userIdNum = scanner.nextInt();
System.out.print("Enter hourly rate:");
userRate = scanner.nextDouble();
System.out.print("Enter number of hours worked:");
userHours = scanner.nextInt();
Payroll payroll1 = new Payroll(userEmplName, userIdNum, userRate, userHours);
payroll1.setName(userEmplName);
payroll1.setIdNumber(userIdNum);
payroll1.setHourlyRate(userRate);
payroll1.setHoursWorked(userHours);
System.out.printf(userEmplName + ", employee number " + userIdNum + ", made $%.2f in gross pay.\n", payroll1.getGrossPay());
}
}
Explanation:
The class "Payroll" is used to hold data of employees to be paid. the main function creates an instance of the class "payroll1", sets the name, id-number, hourly rate, and hours worked by the employee, then prints on screen the details of the payroll1 object.