hey all iv have an assignment(wrtie an employee payroll program) which iv seen alot of other people have posted about so sorry for bringing it up again but im stuck. Iv made the program and it runs fine but it always calculates grosspay and netpay as 0.0 everything else (firstname etc shows up fine) shows up fine i cant figure out what isnt working properly. please help :)

below is the first part (EmployeePayroll.java) i can post the other part if you want me to(TestEmployeePayroll.java)

public class EmployeePayroll
{

    private String firstName, lastName, id;
    private double hoursWorked, payRate, grossPay, netPay, tax;


    public EmployeePayroll()
    {
      firstName=""; lastName=""; id=""; hoursWorked=0; payRate=0;
      tax = 0.42; //fixed tax rate(42%)
    }//constructor

    public EmployeePayroll(String firstName, String lasName, String id, double hoursWorked, double payRate)
      {
       this.firstName=firstName; this.lastName=lastName; this.id=id; this.hoursWorked=hoursWorked; this.payRate=payRate;
      }//constructor

     public void set(String firstName, String lastName, String id, double hoursWorked, double payRate, double grossPay, double netPay)
       {
        this.firstName=firstName; this.lastName=lastName; this.id=id; this.hoursWorked=hoursWorked; this.payRate=payRate; this.grossPay=grossPay; this.netPay=netPay;
       }

       public void calcGrossPay()
       {
        if (hoursWorked <= 38)
               {
             grossPay = hoursWorked*payRate;
               }
              else if (hoursWorked <= 42)
               {
               grossPay = 38* payRate + (hoursWorked - 38) * 1.5 * payRate;
               }
                else if (hoursWorked <= 60)
                {
                grossPay = 38 * payRate + 4 * 1.5 * payRate + (hoursWorked - 42) * 2 * payRate;
                }
                  else
                  {
                   grossPay=hoursWorked*payRate;
                  }        
        }//end if

    public void calcNetPay()
    {
    netPay=grossPay-(grossPay*tax);
    }

    public String GetFirstName()
     {
      return firstName;
     }

    public String GetLastName()
     {
      return lastName;
     }

    public double GetHoursWorked()
     {
      return hoursWorked;
     }

    public double GetHourlyPay()
     {
      return payRate;
     }

    public double GetGrossPay()
     {
      return grossPay;
     }  
    public double GetNetPay()
     {
      return netPay;
     }  
    public String GetId()
     {
      return id;
     }  


}

Recommended Answers

All 10 Replies

I think i need to add a switch statement

It seems the fundamental difference between the values you said are working (first name, etc) and the ones that aren't working is that you have to calculate the ones that aren't working.

In the code that uses the EmployeePayroll instance, are you calling calcGrossPay before trying to display the gross pay? Same question with net pay.

If those questions/thoughts don't get you headed in the right direction, you may need to post the other file for us.

i think i know what you mean. i dont think i am calling calcGrossPay heres the other file tho

there is also a "hint" in the handout the says "Use a switch statement to allocate employee pay rate based on the entered pay category value" could that be an issue becuase i dont have one?? p.s. thnx for replying :)

public class TestEmployeePayroll {

 public static void main (String[] args) 
   {
    ReadKb KB = new ReadKb();
    int category;
    String firstName1, lastName1, id1, firstName2, lastName2, id2;
    double hoursWorked1, grossPay1, netPay1, hoursWorked2, grossPay2, netPay2, payRate;
    category=0;

    System.out.println("Please enter the empolyees first name;");
    firstName2= KB.getString();
    System.out.println("Please enter the empolyees last name;");
    lastName2=KB.getString();
    System.out.println("Please enter the employees idenification number");
    id2=KB.getString();
    System.out.println("Please enter the hours worked by the employee;");
    hoursWorked2=KB.getDouble();
    System.out.println("Please enter the employees pay category;");
    payRate=KB.getDouble();


    if(category==1)
    {
     payRate=55;
    }
     else if(category==2)
      {
       payRate=30;
      }
        else if(category==3)
         {
          payRate=25;
         }
            else
            {
            payRate=20;
            }//end if 


     EmployeePayroll E1 = new EmployeePayroll("john", "smith", "123456", 38.0, 20.00);

            firstName1=E1.GetFirstName();
            lastName1=E1.GetLastName();
            id1=E1.GetId();
            hoursWorked1=E1.GetHoursWorked();
            grossPay1=E1.GetGrossPay();
            netPay1=E1.GetNetPay();

      System.out.println(" Pay slip for " + firstName1 + " " + lastName1 + ". Identification Number: " + id1 +".");
      System.out.println("Hours Worked: " + hoursWorked1);
      System.out.println("Gross Pay: " + grossPay1);
      System.out.println("Net Pay: " + netPay1);

     EmployeePayroll E2 = new EmployeePayroll(firstName2, lastName2, id2, hoursWorked2, payRate);


            firstName2=E2.GetFirstName();
            lastName2=E2.GetLastName();
            id2=E2.GetId();
            hoursWorked2=E2.GetHoursWorked();
            grossPay2=E2.GetGrossPay();
            netPay2=E2.GetNetPay();

      System.out.println("Pay slip for " + firstName2 + " " + lastName2 + ". Identification Number: " + id2 +".");
      System.out.println("Hours Worked: " + hoursWorked2);
      System.out.println("Gross Pay: " + grossPay2);
      System.out.println("Net Pay: " + netPay2);  

   }
}

Well a switch structure is never REQUIRED for something to work right. It accomplishes the same thing a big if-else structure can accomplish. A switch structure is usually a better idea than a bunch of if-elses as it's more readable (and often faster), though.

The hint is telling you that where you did if(category==1)... and so on, you could replace that with a switch structure to clean it up a bit. For the sake of using/experiencing more programming constructs, I think you should use that hint.

As for your initial issue, sounds like we found that one in the previous message. Right now your program is returning you exactly what the default value being assigned to the field grossPay is, which as you've seen is 0.0 for a double value.

i think i get what you mean it cant display the right info for grossPay because it needs the information from calcGrossPay so it just shows the default i.e. 0

Got it working i just got rid of calcGrossPay and put the formula in GetGrossPay and returned grossPay same with netPay thanks heaps for the help :)

its still not working porperly i get an answer but its making gross and net pay the same which it should and the category part isnt working it keeps defaulting to a payrate of 20.00 even when i choose category 1 were payrate should be 55.00

this is with cat1
Please enter the empolyees first name;
jim
Please enter the empolyees last name;
bob
Please enter the employees idenification number
123456
Please enter the hours worked by the employee;
30
Please enter the employees pay category;
1

Pay slip for john smith. Identification Number: 123456.
Hours Worked: 38.0
Gross Pay: 760.0
Net Pay: 760.0
Pay slip for jim bob. Identification Number: 123456.
Hours Worked: 30.0
Gross Pay: 600.0
Net Pay: 600.0

this is with cat4
Please enter the empolyees first name;
jim
Please enter the empolyees last name;
bob
Please enter the employees idenification number
123456
Please enter the hours worked by the employee;
30
Please enter the employees pay category;
4

Pay slip for john smith. Identification Number: 123456.
Hours Worked: 38.0
Gross Pay: 760.0
Net Pay: 760.0
Pay slip for jim bob. Identification Number: 123456.
Hours Worked: 30.0
Gross Pay: 600.0
Net Pay: 600.0

Writing the logic in the main() method is not a good practice. Create seperate .java files fo each functionality and then build an object for these classes in the main
Writing everyting in main() will never suit the real world scenario.

Thanks
Vilas

Alright, so we've got a couple of things going on and going wrong here. The info it's printing to you is the same regardless of what you enter. That's because in your main method (lines 41 - 53 of the code you posted above), you create an employee using information that you've hard coded (typed into the program, rather than reading from the user). So regardless of what you enter, that information isn't changing.

The second print out that your program does is using the information you type in, so that one should at least change things like the name if you type in a different name.

On to the payrate issue. If you follow what your code does mentally (as if you're being the virtual machine that runs the code), you'll note that you do this:

System.out.println("Please enter the employees pay category;");
payRate=KB.getDouble();

So you're asking the user to enter the pay category and then you're saving the value they type in the variable payRate (not category as you likely mean). So when it hits the if-else structure (which is hopefully gonna become a switch statement :) ) it compares category and ends up using the final else because category is equal to 0 (per line 9 above where you assign it 0 and never update that value). Note that at this point it's putting a new value (20) into payRate which overwrites the value the user enters into payRate.. otherwise the 1 that you typed in would have ended up being the pay rate.

Now, why do the gross and net pays match? In your Employee class, you have 3 constructors. Only one of those is being called for any given employee when you create him/her. For the constructor with no parameters, you manually set the tax rate, if you use either of the other 2 constructors (which you do in your main code), you never set the tax rate. I think you know where I'm going now... so the tax rate defaults to 0 and that value is used in your net pay calculation. And when tax is 0 (I wish I lived in that world), gross pay is the same as net pay.

Also, touching on the change you made of getting rid of the calcGrossPay method... the way you had it separated was a good practice. Having one method do one thing is good design and provides you flexibility. Any example I could give would probably not drive home the point for you at this point.. just suffice to say, one method should carry out one function.

So a way to keep that good separation but accomplish the same thing you accomplished is that you could make your GetGrossPay method have 2 lines. The first line would call the method calcGrossPay and then the second line would return grossPay.

That ensures that gross pay is updated based on the most recent information before you return grossPay and it keeps the object oriented intent of a method doing one thing.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.