Hi All.
Im in the process of writing a basic payroll program in Java. It is for some school work and I am really struggling with it. I have been working with it over the last 3 weeks and have not accomplished much. Below is what I have done so far. Part of the test requires a second contrusctor. I appreciate any help given, I can also send you the actual test document to further understand it if need be.

import javax.swing.*;
import java.util.Scanner;

public class EmployeePayroll {
   
   public double hours, payrate, grosspay, netpay, taxrate;
   String firstname, lastname, id;
   
    public EmployeePayroll() {
      
	  firstname = "";
	  lastname = "";
	  id = "";
      hours = 0;
      payrate = 0;
	  grosspay = 0;
	  netpay = 0;
  	}  
	 
	public EmployeePayroll(double _hours, double _payrate, double _grosspay, double _netpay) {
	   hours = _hours;
	   payrate = _payrate;
	   grosspay = _grosspay;
	   netpay = _netpay;
    
	
	public double get_hours() {
	  double _hours;
	  
	  return _hours;
	}

    public double get_payrate() {
      double _payrate;
      
	  return _payrate;
	}
    
    public double get_grosspay() {
      double _grosspay;
      _grosspay = _hours * _payrate;
      return _grosspay;
    }
  
    public double get_netpay() {
      double _netpay;
      _netpay = _grosspay - (_grosspay * taxrate);
      return _netpay;
    }	  
  } 
}

1. get_hours() method should not declare local variable _hours; should return member variable hours instead. Same for get_payrate() method.
2. You need to ask a question to get further help. What is it you don't know how to do?

In your get_hours and get_payrate , you are creating a new variable and sending it back. First, as kremerd said, you don't need to do that, since your intention is to return the global variable _hours / _payrate that you have already declared and initialized. What happens is called Shadowing Declarations, you can read about it more here, and a shorter version here.

Actually, the "global" variables are called hours and payrate, not _hours and _payrate. So there is no shadowing going on.

Ok, I am sorry, early in the morning - my mistake. Thanks for the correction :)

Basically I need it to store first name, last name, ID, hours, payrate and work out grosspay and netpay and display it using the scanner. Im just really stuck on where to go from here.

Hmm. Scanner is used to read data into your program. I don't know how to use a scanner to display anything, sorry. But I'm guessing that's not what you meant.

It looks to me like you are correctly storing first name, last name, ID, hours and payrate. Your grosspay method looks fine. In your netpay method, you are referring to a variable _grosspay that doesn't exist in that scope, so that method won't work. Is that what you're asking for help with? If so, you should post a message stating that you are having trouble compiling your code. Show the error message you are getting and ask for help with that.

Yes I am getting errors when compiling. These are the errors.
EmployeePayroll.java:41: cannot find symbol
symbol : variable _hours
location: class EmployeePayroll
_grosspay = _hours * _payrate;
^
EmployeePayroll.java:41: cannot find symbol
symbol : variable _payrate
location: class EmployeePayroll
_grosspay = _hours * _payrate;
^
EmployeePayroll.java:47: cannot find symbol
symbol : variable _grosspay
location: class EmployeePayroll
_netpay = _grosspay - (_grosspay * taxrate);
^
EmployeePayroll.java:47: cannot find symbol
symbol : variable _grosspay
location: class EmployeePayroll
_netpay = _grosspay - (_grosspay * taxrate);

OK. Change get_grosspay to use your member variables hours & payrate, and store the result in your member variable grosspay so you can use it later for netpay like this:

public double get_grosspay() {
      grosspay = hours * payrate;   // get rid of those underscores
      return grosspay;
    }

Then, when you calculate netpay, you can use the variable grosspay like this:

public double get_netpay() {
      netpay = grosspay - (grosspay * taxrate);  // again, get rid of the underscores
      return netpay;
    }

The problem with this approach is that the get_netpay() method requires that the get_grosspay() method is called first. If someone calls get_netpay() and hasn't yet called get_grosspay(), they will get an incorrect result (get_netpay() will return zero).

A better approach would be to calculate both netpay and grosspay in the constructor, and then in get_netpay() method just return netpay and in get_grosspay() method just return grosspay. That way you control which gets calculated first and you don't rely on the user of your class to do things in the right order. Does that make sense?

Yes that does make sense. I made the changes suggested but now my hours and payrate variables are not initializing. It also returns this if the variable is underscored.

EmployeePayroll.java:30: variable hours might not have been initialized
return hours;
^
EmployeePayroll.java:36: variable payrate might not have been initialized
return payrate;

Do you still have hours = 0; and payrate = 0; in your first constructor, and hours = _hours and payrate = _payrate; in your secnond constructor like you posted in your original code? If not, those should still be there. If so, please post your code again because I don't know what's going on now.

import javax.swing.*;
import java.util.Scanner;

public class EmployeePayroll {
   
   public double hours, payrate, grosspay, netpay, taxrate;
   String firstname, lastname, id;
   
    public EmployeePayroll() {
      
	  firstname = "";
	  lastname = "";
	  id = "";
      hours = 0;
      payrate = 0;
	  grosspay = 0;
	  netpay = 0;
  	}  
	 
	public EmployeePayroll(double _hours, double _payrate, double _grosspay, double _netpay) {
	   hours = _hours;
	   payrate = _payrate;
	   grosspay = _grosspay;
	   netpay = _netpay;
    }
	
	public double hours() {
	  double hours;
	  
	  return hours;
	}

    public double payrate() {
      double payrate;
      
	  return payrate;
	}
    
    public double get_grosspay() {
      double _grosspay;
      grosspay = hours * payrate;
      return grosspay;
    }
  
    public double get_netpay() {
      double _netpay;
      netpay = grosspay - (grosspay * taxrate);
      return netpay;
    }	  
   
}

Remove lines 28, 34, 40 and 46.

Bonesawed, let's take a look at a part of your code:

public class EmployeePayroll 
{
 
   public double hours, payrate, grosspay, netpay, taxrate;
   String firstname, lastname, id;
   
   // ...
   
   public double payrate() 
   {
      double payrate;
      return payrate;
   }
   
   //...

} // end of class

Line 11 declares a new variable called payrate. The next line returns it back to the user. This means that every time the method is called a new variable will be sent back to the user. You wanted something else though - the method needs to return the payrate variable declared on line 4. As you probably know, global variables (the one that you have declared in the class outside of the methods, in the beginning) are visible to all the methods. This means that the method payrate() can see the payrate variable declared on line 4, and therefore can return it to the caller of the payrate() method:

public double payrate() 
{
   return payrate; //the method knows the payrate variable and can send it back to the user.
}

Can you see other places in your code with the same problem?

Thanks it compiles fine now. I also need to implement setter methods for firstname, lastname, ID, hours and payrate. I thought setter methods were only for strings ?

Setter and Getter methods are for all types. Just like a getter method for variable x of type y will be

public y getX()
{
   return x;
}

A setter method for the x will be:

public void setX(y newX)
{
   x = newX;
}
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.