Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: I am having trouble getting my program to return Withholding Tax (keeps returing zero).

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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:
    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
    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


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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:
    	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:
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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:



    \}
    	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
    //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();

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Dec 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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();

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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:
    System.out.println("The gross pay for " + IDnumber + " is: " + pay.getGrossPay());
    it calls the getGrossPay() method AND prints what it returns.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Dec 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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
    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;
    }

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: I am having trouble getting my program to return Withholding Tax (keeps returing zero).

    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.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Dec 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

Similar Threads

  1. Why wont my program calculate sales tax?
    By scholaryoshi in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 28th, 2012, 08:30 PM
  2. help with tax program???
    By JavaNewb3 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 31st, 2011, 12:34 PM
  3. First, simple, two-classed tax program
    By rousseau in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 24th, 2011, 02:14 PM
  4. Basic program which gets cost, adds tax, gets payment then calculates change.
    By bibboorton in forum What's Wrong With My Code?
    Replies: 6
    Last Post: August 25th, 2010, 10:31 AM
  5. tax return program..help finish please!!
    By dscrudato21xo in forum Loops & Control Statements
    Replies: 2
    Last Post: November 5th, 2009, 03:23 PM