Jessica Lim Jessica Lim - 1 month ago 22
Java Question

Payroll in java is resulting 0

The problem is:

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 abd a constructor that accepts the employee's name and ID number as agruments. The class should also have a method that returns the employee's gross pay, which is calculate as the number of hours worked multiplied by the hourly pay rate. Write a program that demonstrates the class by creating a Payroll objectm then asking the user to enter the data for an employee. The program should display the amount of gross pay earned.

Heres what I have for the class

import java.util.Scanner; //Needed for scanner class.
public class Payroll
{
private String EmployeeName;
private int IDnumber;
private double HourlyPayRate;
private double HoursWorked;
private double GrossPay;

/**
Constructor
@param Name The name to store in EmployeeName.
@param ID The ID to store in Employee ID number.
*/
public Payroll(String Name, int ID)
{
EmployeeName = Name;
IDnumber = ID;
}
public String getEmployeeName()
{
return EmployeeName;
}
public int getIDnumber()
{
return IDnumber;
}
public void setHourlyPayRate(double HourlyRate)
{
HourlyPayRate = HourlyRate;
}
public double getHourlyPayRate()
{
return HourlyPayRate;
}
public void setHoursWorked(double hoursWorked)
{
HoursWorked = hoursWorked;
}
public double getHoursWorked()
{
return HoursWorked;
}
public double getGrossPay()
{
return HourlyPayRate * HoursWorked;
}
}


The constructor

import java.util.Scanner; //Needed for Scanner class.

public class PayrollTest
{
public static void main(String[] args)
{
String EmployeeName;
int IDnumber;
double HoursWorked;
double HourlyPayRate;
double GrossPay;

//Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);

//Get the employee's name.
System.out.println("Enter an employee's name: ");
EmployeeName = keyboard.nextLine();

//Get the employee's ID.
System.out.println("Enter the employee's ID " );
IDnumber = keyboard.nextInt();

//Get the number of hours worked by the employee.
System.out.println("Enter the amount of hours worked by this employee: ");
HoursWorked = keyboard.nextDouble();

//Get the hourly pay rate of the employee.
System.out.println("Enter the hourly pay rate for this employee: ");
HourlyPayRate = keyboard.nextDouble();

//Create a payroll object, passing EmployeeName and IDnumber
// as arguments to the constructor.
Payroll pay = new Payroll(EmployeeName, IDnumber);

//Get the Gross Pay of the employee.
System.out.println("The gross pay of " + EmployeeName + " is: " + pay.getGrossPay());
}
}


When I compile it, I didn't get any error but i keep getting the result of grosspay = 0.0 at the end.

Answer

You create a new Payroll object

Payroll pay = new Payroll(EmployeeName, IDnumber);

and call the getGrossPay() method on it in your print statement

pay.getGrossPay()

that method does the following:

return HourlyPayRate * HoursWorked;

since you never set HourlyPayRate or HoursWorked, of course the method is going to return 0.

To fix this, set those values to the variables you read from the user:

pay.setHoursWorked(HoursWorked);
pay.setHourlyPayRate(HourlyPayRate);

You can add those lines after you create the Payroll object but before you call the getGrossPay() method. So the last four lines of your code should now look like this:

Payroll pay = new Payroll(EmployeeName, IDnumber);

//set pay rate and hours worked
pay.setHoursWorked(HoursWorked);
pay.setHourlyPayRate(HourlyPayRate);

//Get the Gross Pay of the employee.
System.out.println("The gross pay of " + EmployeeName + " is: " + pay.getGrossPay());
Comments