Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!

Program that calculates and prints the monthly paycheck for an employee

Tyler Wam
Greenhorn
Posts: 5
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.

Stephan van Hulst
Bartender
Posts: 5813
61
Welcome to CodeRanch!

This sounds more like a math problem than a programming problem. What exactly are you having a hard time with?

Tyler Wam
Greenhorn
Posts: 5
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

Stephan van Hulst
Bartender
Posts: 5813
61
Well, show us exactly how you are calculating the final result, and what input values you're using.

James Boswell
Bartender
Posts: 1051
5
• 1
Well, I keep coming out with a negative number after the deductions

Are you working out my paycheck Tyler? :-)

Tyler Wam
Greenhorn
Posts: 5
James Boswell wrote:
Well, I keep coming out with a negative number after the deductions

Are you working out my paycheck Tyler? :-)

Haha, would actually be mine most of the time.

Tyler Wam
Greenhorn
Posts: 5
Stephan van Hulst wrote:Well, show us exactly how you are calculating the final result, and what input values you're using.

I hope this will give you what you're asking for? I hope I didn't put too much up here to go through. Sorry still pretty new to this.

Stephan van Hulst
Bartender
Posts: 5813
61
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?

Tyler Wam
Greenhorn
Posts: 5
Would it have been better to just use those to define the variables and just less code in general?

James Boswell
Bartender
Posts: 1051
5
Also, I would always use BigDecimal instead of doubles/floats when currency is involved.

Campbell Ritchie
Sheriff
Posts: 48981
60
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 BigDecimal here, 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.

Wendy Gibbons
Bartender
Posts: 1111
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 BigDecimal here, 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
Posts: 6109
6
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.

Campbell Ritchie
Sheriff
Posts: 48981
60
Wendy Gibbons wrote:. . . ID_SERIAL_135 = 135
What’s wrong with that?

Winston Gutkowski
Bartender
Posts: 10417
63
Campbell Ritchie wrote:
Wendy Gibbons wrote:. . . ID_SERIAL_135 = 135
What’s wrong with that?

Erm....carpel-tunnel syndrome?

Winston

Wendy Gibbons
Bartender
Posts: 1111
Campbell Ritchie wrote:
Wendy Gibbons wrote:. . . ID_SERIAL_135 = 135
What’s wrong with that?

the actual code went

Pete Nelson
Ranch Hand
Posts: 147
Wendy Gibbons wrote:

It's like poetry in praise of Enums!

Campbell Ritchie
Sheriff
Posts: 48981
60
Wendy Gibbons wrote: . . . the actual code went . . .
Oh, that’s even better! You have some brilliant people there who ought to supply material to Roedy Green.