Hello, I'm trying to finish a program here for an assignment. The prompt from the book: Write a program that calculates and prints the monthly paycheck for an employee. The net pay is calculated after taking the following deductions:
Federal Income Tax: 15%
State Tax: 3.5%
Social security Tax: 5.75%
Medicare/Medicaid Tax: 2.75%
Pension Plan: 5%
Health Insurance $75.00
It complies fine, I just need to figure out mainly how to get the right formula for health Insurance.
Here is what I have:
If you need anymore of the program I can post the rest, but I think this is where my main focus needs to be fixed since I think it's trying to calculate it as a percent instead of just plain $75
any help on this would be great, and thank you in advance.
Well, I keep coming out with a negative number after the deductions. Like everything other tax deduction works out fine. It only seems to be the Health Insurances that seems to be the problem that isn't wanting to calculate properly, and I was wondering if I should be using something else besides
The problem is that you're calculating health tax by deducting $75,- from the gross pay. When you later deduct this tax from the gross pay, it pretty much means you always get 75 bucks, before other deductions, regardless of how high your gross pay actually is.
The solution is to simply add 75 to the deductions, instead of healthtaxr. You can then remove healthtaxr completely.
PS. Why are you assigning input values to variables such as statetax, and then ignore them completely?
Would it have been better to just use those to define the variables and just less code in general?
James Boswell
Ranch Hand
Joined: Nov 09, 2011
Posts: 344
posted
0
Also, I would always use BigDecimal instead of doubles/floats when currency is involved.
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 26720
posted
0
I think you are trying to do too much at once. Get one bit working, then try to get the rest working. Don’lt attempt too much at once.
Also follow James Boswell’s advice: never never use doubles for money. Using floats is evern worse; both are unsuitable because of the restricted precision. I wrote an example of the use of BigDecimalhere, with some problems demonstrated.
You ought to avoid coding things like 0.035. You are better off creating constants to store those values inYes, you ought to write documentation comments even for private fields where you know they won’t be displayed anywhere (or at least, I think you ought to). Yes, you need the quote marks around 0.15; you want to use a String argument, not a double.
Once you have got that all working, I suggest you enhance the system to read the tax rates from a text file.
Campbell Ritchie wrote:I think you are trying to do too much at once. Get one bit working, then try to get the rest working. Don’lt attempt too much at once.
Also follow James Boswell’s advice: never never use doubles for money. Using floats is evern worse; both are unsuitable because of the restricted precision. I wrote an example of the use of BigDecimalhere, with some problems demonstrated.
You ought to avoid coding things like 0.035. You are better off creating constants to store those values inYes, you ought to write documentation comments even for private fields where you know they won’t be displayed anywhere (or at least, I think you ought to). Yes, you need the quote marks around 0.15; you want to use a String argument, not a double.
Once you have got that all working, I suggest you enhance the system to read the tax rates from a text file.
but please give them helpful names, not like the ones I found in code last week, ID_SERIAL_135 = 135
Jeff Verdegan
Bartender
Joined: Jan 03, 2004
Posts: 3144
posted
0
Campbell Ritchie wrote:Also follow James Boswell’s advice: never never use doubles for money.
While I would never use doubles for real accounting calculations, I think for beginners to the language, it's fine to use it for money in exercises, if the exercise includes the caveat that we don't do this for real, but are doing it here to keep things simple and avoid distracting from the core topic being taught.
More computing sins are committed in the name of efficiency (without necessarily achieving it)
than for any other single reason...including blind stupidity. — W.A. Wulf