I am having trouble getting my program to return Withholding Tax (keeps returing zero).
hi
I have to set an instance variable but just don't know where.Attached is my class with Methods and my Test Class . Any help would be great.
Methods:
Code Java:
public class Payroll
{
//constants
private static final double REG_HOURS = 40;
private static final double OT_RATE = 1.5;
//Variables
private int IDnumber;
private double HourlyPayRate;
private double HoursWorked;
private double TaxWitholding;
private double GrossPay;
private double grossPay;
/**
Constructor
@param ID The ID to store in Employee ID number.
*/
public Payroll( int ID)
{
IDnumber = ID;
}
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()
{
if (HoursWorked <=REG_HOURS){
double grossPay =HourlyPayRate * HoursWorked;
return grossPay;
}
else{
double regPay =REG_HOURS * HourlyPayRate;
double otPay = (HoursWorked - REG_HOURS) * OT_RATE * HourlyPayRate;
double grossPay = (regPay + otPay);
return grossPay;
}
}
public void setgrossPay(double grossPay)
{
GrossPay =grossPay;
}
public double getTaxWitholding()
{
if (GrossPay <= 300.00){
TaxWitholding = .10 * GrossPay;
}
else if (GrossPay <= 400.00){
TaxWitholding = .12 * GrossPay;}
else if (GrossPay <= 500.00){
TaxWitholding = .15 * GrossPay;}
else if (GrossPay > 500.01){
TaxWitholding = .20 * GrossPay;}
return TaxWitholding;
}
}
Test Class
Code Java:
import java.util.Scanner; //Needed for Scanner class.
public class PayrollTest
{
public static void main(String[] args)
{
int IDnumber = 0;
double HoursWorked=0;
double HourlyPayRate=0;
double GrossPay = 0;
//Create a Scanner object for keyboard input.
Scanner in = new Scanner(System.in);
//Create a payroll object, IDnumber
// as arguments to the constructor.
Payroll pay = new Payroll(IDnumber);
//Get the employee's ID.
System.out.println("Enter the employee's ID " );
IDnumber = in.nextInt();
//Get the number of hours worked by the employee.
System.out.println("Enter the number of hours worked: ");
HoursWorked= in.nextDouble();
pay.setHoursWorked(HoursWorked);
while(pay.getHoursWorked()<=0)
{
System.out.print("Please enter a number >0. Try Again.");
pay.setHoursWorked(in.nextDouble());
}
//Get the hourly pay rate of the employee.
System.out.println("Enter the hourly pay rate for this employee: ");
HourlyPayRate = in.nextDouble();
pay.setHourlyPayRate(HourlyPayRate);
while(pay.getHourlyPayRate()<=0)
{
System.out.print("Please enter a number >0. Try Again.");
pay.setHourlyPayRate(in.nextDouble());
}
//Get the Gross Pay of the employee.
System.out.println("The gross pay for " + IDnumber + " is: " + pay.getGrossPay());
//Get the Gross Pay of the employee.
System.out.println("The gross pay for " + IDnumber + " is: ");
pay.setgrossPay(GrossPay);
pay.getTaxWitholding();
}
}
Any help would be great
Re: I am having trouble getting my program to return Withholding Tax (keeps returing zero).
What line in the code is returning the 0? Is there a variable that is supposed to have a non-zero value?
Check the code that puts a value in the variable to see why it does not assign a valid value to teh variable.
If you can't see what the code is doing, add some println statements to print out the values of all the variables used in the computation so you can see what the program is doing.
Can you post the contents of the command prompt window that shows the problem and add comments to show what it should be?
When I execute the code with this input source:
Code :
Scanner in = new Scanner("123\n22\n20\n"); //System.in);
it prints out this:
The gross pay for 123 is: 440.0
The gross pay for 123 is:
Re: I am having trouble getting my program to return Withholding Tax (keeps returing zero).
Ok the gross pay works. It is the Withholding tax that returns zero.
Here is my output
Enter the employee's ID
123
Enter the number of hours worked:
22
Enter the hourly pay rate for this employee:
20
The gross pay for 123 is: 440.0
The Withholding Tax for 123 is:
Code Java:
\}
public double getGrossPay()
{
if (HoursWorked <=REG_HOURS){
double grossPay =HourlyPayRate * HoursWorked;
return grossPay;
}
else{
double regPay =REG_HOURS * HourlyPayRate;
double otPay = (HoursWorked - REG_HOURS) * OT_RATE * HourlyPayRate;
double grossPay = (regPay + otPay);
return grossPay;
}
}
public void setgrossPay(double grossPay)
{
GrossPay =grossPay;
}
public double getTaxWitholding()
{
if (GrossPay <= 300.00){
TaxWitholding = .10 * GrossPay;
}
else if (GrossPay <= 400.00){
TaxWitholding = .12 * GrossPay;}
else if (GrossPay <= 500.00){
TaxWitholding = .15 * GrossPay;}
else if (GrossPay > 500.01){
TaxWitholding = .20 * GrossPay;}
return TaxWitholding;
This call the Method
Code Java:
//Get the Gross Pay of the employee.
System.out.println("The gross pay for " + IDnumber + " is: " + pay.getGrossPay());
//Get the Withholding Tax of the employee.
System.out.println("The Withholding Tax for " + IDnumber + " is: ");
pay.setgrossPay(GrossPay);
pay.getTaxWitholding();
Re: I am having trouble getting my program to return Withholding Tax (keeps returing zero).
Where does the program print out the value of the withholding tax?
I don't see any calls to the println method with that value.
Re: I am having trouble getting my program to return Withholding Tax (keeps returing zero).
Don't I call it here
//Get the Withholding Tax of the employee.
System.out.println("The Withholding Tax for " + IDnumber + " is: ");
pay.setgrossPay(GrossPay);
pay.getTaxWitholding();
Re: I am having trouble getting my program to return Withholding Tax (keeps returing zero).
Where is there a println statement that prints out the value returned by the getTaxWitholding() method?
The program calls the method and ignores the value that it returns.
Look at this line:
Code :
System.out.println("The gross pay for " + IDnumber + " is: " + pay.getGrossPay());
it calls the getGrossPay() method AND prints what it returns.
Re: I am having trouble getting my program to return Withholding Tax (keeps returing zero).
ok..I am getting there. It at least returns 0.0 Now I just have to see why it's not getting the value
Here is my return
Enter the employee's ID
55
Enter the number of hours worked:
12
Enter the hourly pay rate for this employee:
10
The gross pay for 55 is: 120.0
The Withholding Tax for 55 is: 0.0
--- Update ---
So it has to be somewhere in here
Code Java:
public double getGrossPay()
{
if (HoursWorked <=REG_HOURS){
grossPay =HourlyPayRate * HoursWorked;
return grossPay;
}
else{
double regPay =REG_HOURS * HourlyPayRate;
double otPay = (HoursWorked - REG_HOURS) * OT_RATE * HourlyPayRate;
grossPay = (regPay + otPay);
return grossPay;
}
}
public void setGrossPay( double grossPay)
{
GrossPay =grossPay;
}
public double getTaxWitholding()
{
if (GrossPay <= 300.00){
TaxWitholding = .10 * GrossPay;
}
else if (GrossPay <= 400.00){
TaxWitholding = .12 * GrossPay;}
else if (GrossPay <= 500.00){
TaxWitholding = .15 * GrossPay;}
else if (GrossPay > 500.01){
TaxWitholding = .20 * GrossPay;}
return TaxWitholding;
}
Re: I am having trouble getting my program to return Withholding Tax (keeps returing zero).
Quote:
Now I just have to see why it's not getting the value
Try debugging the code by adding some printlns to print out the values of all the variables used in the computation. The printed out values will show you what the computer sees when it executes the code.
You should add an else statement at the end of the chain of if/else if statements to print out a message if the code does not find a match in the above if/else if statements. The message should include GrossPay.
Re: I am having trouble getting my program to return Withholding Tax (keeps returing zero).
Thank so kmuch. Finally got it.
Enter the employee's ID
555
Enter the number of hours worked:
40
Enter the hourly pay rate for this employee:
20
The gross pay for 555 is: 800.0
The Withholding Tax for 555 is: 160.0