13 Replies - 1639 Views - Last Post: 09 December 2012 - 02:51 PM Rate Topic: -----

#1 mwalsh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 06-December 12

java program that calculates Tax Witholding issue. Always returns zero

Posted 06 December 2012 - 09:20 PM

Ok I have to write a program that calculates an employees gross pay, tax witholding and net pay. My class must have a constructor and methods. I also need a test class. I have most but the Tax Witholding needs to be computed based on
Gross Pay <= 300.00 = 10%
Gross Pay <= 400.00 = 12%
Gross Pay <= 500.00 = 15%
Gross Pay . 500.01 = 20%
Here is what I have
[import java.util.Scanner;	//Needed for scanner class.

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 GrossPay;
	private double Tax;
	/**
		Constructor
		@param Name The name to store in EmployeeName.
		@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){
			return HourlyPayRate * HoursWorked;
		}
		else{
			double regPay =REG_HOURS * HourlyPayRate;
			double otPay = (HoursWorked - REG_HOURS) * OT_RATE * HourlyPayRate;
			return regPay + otPay;
		}
	}
	
	public double getTaxWitholding()
	{
		if (GrossPay <= 300.00)
				Tax = .10 * GrossPay;
		else if (GrossPay <= 400.00)
				Tax = .12 * GrossPay;
		else if (GrossPay <= 500.00)
			Tax = .15 * GrossPay;
		else if (GrossPay > 500.01)
			Tax = .20 * GrossPay;
		return Tax;
	}

}
and my test
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;
		double TaxWitholding = 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());
		System.out.println("The Tax Withholding for " + IDnumber + " is: " + pay.getTaxWitholding());
	}
}


*** Edit ***
Please use code tags when posting code
:code:

This post has been edited by GunnerInc: 06 December 2012 - 09:27 PM
Reason for edit:: Added elusive code tags


Is This A Good Question/Topic? 0
  • +

Replies To: java program that calculates Tax Witholding issue. Always returns zero

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2674
  • View blog
  • Posts: 11,293
  • Joined: 20-September 08

Re: java program that calculates Tax Witholding issue. Always returns zero

Posted 07 December 2012 - 03:28 AM

Make sure GrossPay has been set
Was This Post Helpful? 0
  • +
  • -

#3 mwalsh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 06-December 12

Re: java program that calculates Tax Witholding issue. Always returns zero

Posted 07 December 2012 - 08:59 AM

Ok so I need to 'set' the Gross Pay. Do I set it before the get TaxWitholding() method?
 public double getGrossPay() 
	    { 
	        if (HoursWorked <=REG_HOURS){ 
	            return HourlyPayRate * HoursWorked; 
	        } 
	        else{ 
	            double regPay =REG_HOURS * HourlyPayRate; 
	            double otPay = (HoursWorked - REG_HOURS) * OT_RATE * HourlyPayRate; 
	            return regPay + otPay; 
	        } 
	    } 
	      
	    public double getTaxWitholding() 
	    { 
	        if (GrossPay <= 300.00) 
	                Tax = .10 * GrossPay; 
	        else if (GrossPay <= 400.00) 
	                Tax = .12 * GrossPay; 
	        else if (GrossPay <= 500.00) 
	            Tax = .15 * GrossPay; 
	        else if (GrossPay > 500.01) 
	            Tax = .20 * GrossPay; 
	        return Tax; 
	    } 
	  
	} 




Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2674
  • View blog
  • Posts: 11,293
  • Joined: 20-September 08

Re: java program that calculates Tax Witholding issue. Always returns zero

Posted 07 December 2012 - 09:11 AM

Quote

Do I set it before the get TaxWitholding() method?
yes
Was This Post Helpful? 0
  • +
  • -

#5 mwalsh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 06-December 12

Re: java program that calculates Tax Witholding issue. Always returns zero

Posted 07 December 2012 - 09:42 AM

Something like this
}
	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;


Was This Post Helpful? 0
  • +
  • -

#6 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2674
  • View blog
  • Posts: 11,293
  • Joined: 20-September 08

Re: java program that calculates Tax Witholding issue. Always returns zero

Posted 07 December 2012 - 09:56 AM

Well you'll know when you compile it. I was talking about calling it though, not defining it
Was This Post Helpful? 0
  • +
  • -

#7 mwalsh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 06-December 12

Re: java program that calculates Tax Witholding issue. Always returns zero

Posted 08 December 2012 - 10:08 AM

I am still having trouble with my Witholding tax retuning zero.
I have 1 class with the Methods and a Test class calling the Methods.
The Gross Pay (and overtime calculated works but Witholding tax returns zero no matter what pay is calculated. Here is the code where I am off.
Method
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;
}
}


My Test Class calls:
//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();

		     }  

		 } 


I will continue to work on this but any suggestions would be great.
Was This Post Helpful? 0
  • +
  • -

#8 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2674
  • View blog
  • Posts: 11,293
  • Joined: 20-September 08

Re: java program that calculates Tax Witholding issue. Always returns zero

Posted 08 December 2012 - 10:09 AM

Please post full runnable code
Was This Post Helpful? 0
  • +
  • -

#9 mwalsh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 06-December 12

Re: java program that calculates Tax Witholding issue. Always returns zero

Posted 08 December 2012 - 10:14 AM

Ok Here is the Methods...
import java.util.Scanner;	//Needed for scanner class.

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;
	/**
		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;
}
}



Here is my 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();

		     }  

		 } 


Was This Post Helpful? 0
  • +
  • -

#10 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2674
  • View blog
  • Posts: 11,293
  • Joined: 20-September 08

Re: java program that calculates Tax Witholding issue. Always returns zero

Posted 08 December 2012 - 10:53 AM

I've already advised on the reason for your problem HERE

You still don't set your instance variable 'GrossPay' (which should incidentally begin lower case
Was This Post Helpful? 0
  • +
  • -

#11 mwalsh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 06-December 12

Re: java program that calculates Tax Witholding issue. Always returns zero

Posted 08 December 2012 - 12:15 PM

Is there a chance you can just show me the code. I learn better by examples...
Was This Post Helpful? 0
  • +
  • -

#12 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2674
  • View blog
  • Posts: 11,293
  • Joined: 20-September 08

Re: java program that calculates Tax Witholding issue. Always returns zero

Posted 08 December 2012 - 12:23 PM

Quote

private double GrossPay;
]Change to
private double grossPay

Quote

double grossPay
Get rid of 'double' from that throughout
Was This Post Helpful? 0
  • +
  • -

#13 mwalsh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 06-December 12

Re: java program that calculates Tax Witholding issue. Always returns zero

Posted 08 December 2012 - 02:03 PM

Thanks. I finally got it to work. I will post my source for others later. Off to 2nd job. thanks again
Was This Post Helpful? 0
  • +
  • -

#14 mwalsh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 06-December 12

Re: java program that calculates Tax Witholding issue. Always returns zero

Posted 09 December 2012 - 02:51 PM

Methods
public class NetPayProgram {
	
		//constants used to get overtime hours
		private static final double REG_HOURS = 40;
		private static final double OT_RATE = 1.5;
		//Variables declared
		private int employeeId;
		private double payRate;
		private double HoursWorked;
		private double taxWitholding;
		private double grossPay;
		
		/**
			Constructor			
			@param ID store ID in EmployeeId.
		*/
		public NetPayProgram( int ID)
		{
			employeeId = ID;
		}
		// returns ID
		public int getIDnumber()
		{
			return employeeId;
		}
		/**
		 * adds in the hourly rate, sets it and gets it.
		 * @param HourlyRate
		 */
		public void setHourlyPayRate(double HourlyRate)
		{
			payRate = HourlyRate;
		}
		public double getHourlyPayRate()
		{
			return payRate;
		}
		/**
		 * adds in the hours worked, sets it and gets it.
		 * @param hoursWorked
		 */
		public void setHoursWorked(double hoursWorked)
		{
			HoursWorked = hoursWorked;
		}
		public double getHoursWorked()
		{
			return HoursWorked;
		}
		/**
		 * Calculates the Gross Pay  
		 * if over 40 hours calculates overtime
		 * @return Gross Pay
		 */
		public double getGrossPay()
		{
			if (HoursWorked <=REG_HOURS){
				 grossPay =payRate * HoursWorked;
				return grossPay;
			}
			else{
				double regPay =REG_HOURS * payRate;
				double otPay = (HoursWorked - REG_HOURS) * OT_RATE * payRate;
				 grossPay = (regPay + otPay);
				return grossPay;
			}
		}
		//uses the GrossPay Method and sets a value 
		public void setGrossPay(double grossPay)
		{
		}
	  	/**
	  	 * Calculates Tax. Uses Gross Pay and 
	  	 * uses table to calc tax.
	  	 * @return tax withholding
	  	 */
		
			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;
			}	
			/**
			 * Calculates Net Pay.
			 * Gross Pay -Tax
			 * @return Net Pay
			 */
			public double getNet()
		{
			double Net =(grossPay - taxWitholding);
			return Net;
	}
	}


TestProgram
import java.util.Scanner;	//Needed for Scanner class.
/** This is the program that tests the Methods in 
 * the NetPayProgram.
 * 
 */
public class TestNetPay {

	public static void main(String[] args)
	{
		//initialize variables
		int employeeId = 0;
		double HoursWorked=0;
		double payRate=0;
		
		//Create a Scanner object for keyboard input.
		Scanner in = new Scanner(System.in);
		//Create a NetPay object, employeeId
		// starts the constructor.
		NetPayProgram pay = new NetPayProgram(employeeId);
		
		//Get the employee's ID.
		System.out.println("Enter the employee's ID " );
		employeeId = in.nextInt();
		
		//Get the number of hours worked by the employee and tests.
		//If less than zero returns message to re-enter.
		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 and tests the value.
		//If less than zero returns message to re-enter.
		System.out.println("Enter the hourly pay rate for this employee: ");
		payRate = in.nextDouble();
		pay.setHourlyPayRate(payRate);
		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 and print.
		System.out.println("The gross pay for " + employeeId + " is: " + pay.getGrossPay());
		//Get the Gross Pay of the employee and print.  
		System.out.println("The Withholding Tax for " + employeeId + " is: " + pay.getTaxWitholding());
		//Get the Net Pay of employee and print.
		System.out.println("The Net Pay for " + employeeId + " is: " + pay.getNet());
				     }  

		 } 


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1