This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Program that calculates and prints the monthly paycheck for an employee Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Program that calculates and prints the monthly paycheck for an employee" Watch "Program that calculates and prints the monthly paycheck for an employee" New topic
Author

Program that calculates and prints the monthly paycheck for an employee

Tyler Wam
Greenhorn

Joined: Feb 05, 2012
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

Joined: Sep 20, 2010
Posts: 3573
    
  14

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

Joined: Feb 05, 2012
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

Joined: Sep 20, 2010
Posts: 3573
    
  14

Well, show us exactly how you are calculating the final result, and what input values you're using.
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1012
    
    5

Well, I keep coming out with a negative number after the deductions


Are you working out my paycheck Tyler? :-)
Tyler Wam
Greenhorn

Joined: Feb 05, 2012
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

Joined: Feb 05, 2012
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

Joined: Sep 20, 2010
Posts: 3573
    
  14

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

Joined: Feb 05, 2012
Posts: 5

Would it have been better to just use those to define the variables and just less code in general?
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1012
    
    5

Also, I would always use BigDecimal instead of doubles/floats when currency is involved.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37879
    
  22
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

Joined: Oct 21, 2008
Posts: 1107

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

Joined: Jan 03, 2004
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

Joined: Oct 13, 2005
Posts: 37879
    
  22
Wendy Gibbons wrote:. . . ID_SERIAL_135 = 135
What’s wrong with that?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7484
    
  18

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

Erm....carpel-tunnel syndrome?

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Wendy Gibbons
Bartender

Joined: Oct 21, 2008
Posts: 1107

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

the actual code went

Pete Nelson
Ranch Hand

Joined: Aug 30, 2010
Posts: 147

Wendy Gibbons wrote:


It's like poetry in praise of Enums!


OCPJP
In preparing for battle I have always found that plans are useless, but planning is indispensable. -- Dwight D. Eisenhower
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37879
    
  22
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Program that calculates and prints the monthly paycheck for an employee
 
Similar Threads
problem with StringBuffer and Double
health care debate
Java Program....Coding Help....Please!
Arrays
Aligning text in JOptionPane