0

I am getting an error that says: C:\Users\Jasmi\payroll\src\Payroll.java:68:38 java: variable grossPay might not have been initialized, but I am not sure how to fix it.

public class Payroll {
    public String calculateGrossPay;
    public String calculateNetPay;
    private String name;
    private int idNumber;
    private double hourlyPayRate;
    private double hoursworked;

    Payroll(String nameGiven, int idNumbergiven) {
        name = nameGiven;
        idNumber = idNumbergiven;
        hourlyPayRate = 7.15;
        hoursworked = 0;
    }

    public String getName() {
        return name;
    }

    public int getIDNumber() {
        return idNumber;
    }

    public double getHourlyPayrate() {
        return hourlyPayRate;
    }

    public double getHoursWorked() {
        return hoursworked;
    }

    public void setName(String nameGiven) {
        name = nameGiven;
    }

    public void setIDNumber(int idNumbergiven) {
        idNumber = idNumbergiven;
    }

    public void setHourlyPayRate(double hourlypayrategiven) {
        hourlyPayRate = hourlypayrategiven;
    }

    public void setHoursWorked(double hoursworkedgiven) {
        hoursworked = hoursworkedgiven;
    }

    //gross pay plus overtime
    public double calculateGrossPay() {

        double overtime;
        overtime = 0;
        double grossPay;
        if (hoursworked < 40) grossPay = hourlyPayRate * hoursworked;
        else {
            overtime = hoursworked - 40;
            grossPay = (overtime * 1.5 * hourlyPayRate) + (40 * hourlyPayRate);
        }

        return grossPay;

    }

    //deductions
    public double calculateNetPay() {
        double netPay;
        double grossPay;
        double deduction = (.2579) * grossPay;

        return netPay;


    }
}

Here is the second document:

import javax.swing.JOptionPane;

    public class PayrollClassTest {
        public static void main(String[] args) {

            String userInputString;
            String userName;
            int userId;
            double userhourlyPayRate;
            double userHoursworked;

            userName = JOptionPane.showInputDialog("enter the name of this employee: ");
            userInputString = JOptionPane.showInputDialog("Please enter employee ID:  ");
            userId = Integer.parseInt(userInputString);
            userInputString = JOptionPane.showInputDialog("Please enter Hourly Pay Rate:  ");
            userhourlyPayRate = Double.parseDouble(userInputString);
            userInputString = JOptionPane.showInputDialog("Enter the hours worked:  ");
            userHoursworked = Double.parseDouble(userInputString);

            Payroll payroll1 = new Payroll(userName, userId);
            payroll1.setHourlyPayRate(userhourlyPayRate);
            payroll1.setHoursWorked(userHoursworked);
            System.out.println(payroll1.getName() + " has a gross pay of  " + payroll1.calculateGrossPay());
            System.out.println(payroll1.getName() + " has a net pay of  " + payroll1.calculateNetPay());
            System.exit(0);
        }

        private static void calculateGrossPay() {
        }
        private static void calculateNetPay() {
        }


    }

I have tried to change deductions to be shown as this:

//deductions
    public double calculateNetPay() {
        double netPay = 0;
        double grossPay = 0;
        double deduction = (.2579) * grossPay;

        return netPay;


    }
}

It does work, but the results do not show the deductions: Here is an example of the results: Betty has a gross pay of 13000.0 Betty has a net pay of 0.0

Process finished with exit code 0 This is when I put name as Betty, gross pay as 100, and hours worked as 100. It shows the overtime correctly, but not the deductions.

Any help is greatly appreciated. thanks!

1 Answers1

0

If my understanding of the code is correct then the following is where you are wrong for this piece of code.

    public double calculateNetPay() {
        double netPay = 0;
        double grossPay = 0;
        double deduction = (.2579) * grossPay;

        return netPay;


    }

First: netPay has no value assigned
This method will always return 0. Reason : The netPay variable is declared and initialized to 0.

What you probably want to do before your return statement is perhaps;

    netPay = grossPay - deduction;

I could be wrong in that logic, but I am definitely right when I say that you need to put some value to netPay before you return it.


Second: grossPay has no value assigned
In this method, you multiple .2579 with grossPay, but grossPay is 0 that you have now initialized.

You are assuming that the variable grossPay is shared for the two methods calculateGrossPay and calculateNetPay.

That is not true.

These are both two separate local variables that are declared separately in two different methods and have two different scopes.
You can read more about scope of java variables here:



My recommendation is to make grossPay a class variable instead of a method variable so that it could be shared between the two methods of the same class instance. (Assuming you are calling calculateGrossPay before calculateNetPay every time, so that grossPay can have the right value assigned to it.)

Shivam Puri
  • 1,578
  • 12
  • 25