# Maximum PC

 It is currently Fri Aug 29, 2014 2:50 am

 All times are UTC - 8 hours

 Page 1 of 1 [ 10 posts ]
 Print view Previous topic | Next topic
Author Message
 Post subject: Can someone look over this equation for me please?Posted: Tue Mar 11, 2008 7:42 pm
 Malware specialist

Joined: Sun Apr 03, 2005 12:49 pm
Posts: 11696
Location: Kansas City, KS
Hey all, I'm making a Java program for my programming class to calculate payroll.

For overtime (assuming overtime is 40 hours or greater), I'm using the following equation:

Code:
grossPay = (40 * rateOfPay) + ((workHours-40) * rateOfPay * 1.5);

grossPay is just the grossPay, before insurance is taken out
rateOfPay is the amount the person gets paid per hour
workHours is the amount of hours the person works

Did I do this overtime calculation correct? I can supply my full code if needed, I'm just making sure this formula doesn't have any logical errors in it.

Top

 Post subject: Posted: Tue Mar 11, 2008 8:02 pm
 7yrs+11,000 Posts

Joined: Tue Jul 27, 2004 5:44 pm
Posts: 11242
Location: The kitten above is not on fire.
It works, assuming the employee puts in at least 40 hours per week. Lets pretend our employee takes a day off, or is only part time. Using that formula for the calculation, he would still be paid for 40 hours, regardless of how much time he actually out in. . Something like this may do you better:
Code:
grossPay = (workhours * rateOfPay) + ((workHours-40) * 1.5);

You'll probably need to add some additional logic in there so you don't end up adding negative numbers to the gross pay.

Another issue you may want to think about is how you are awarding overtime. Correct me if I'm wrong, but dont most employers calculate overtime in daily increments?

Here is a scenario. Lets pretend our employee works mon-wensday normal hours. On Thursday he works 3 hours overtime, and takes off Friday. With your current pay stricture, he wouldn't be paid extra for that overtime he put in on Thursday. Of course, the method for awarding overtime is completely up to you.

Top

 Post subject: Posted: Tue Mar 11, 2008 10:20 pm
 Million Club - 5 Plus*

Joined: Sun Sep 12, 2004 6:37 pm
Posts: 4745
Location: In the monkey's litterbox
Hack -- your equation is fine, however gamer's has an error in it.

Top

 Post subject: Posted: Wed Mar 12, 2008 2:33 am
 Coppermine

Joined: Mon Feb 21, 2005 9:33 am
Posts: 692
Location: Grass Valley, CA
Hackman, your equation is fine, but you need to handle work weeks with less than 40 hours. I'm sure your instructor is sneaky enough to throw a short work week into the data. gamerfreak's equation is not correct, but that looks liek the kind of equation the IRS will use on me come April 15.

Top

 Post subject: Posted: Wed Mar 12, 2008 9:26 am
 7yrs+11,000 Posts

Joined: Tue Jul 27, 2004 5:44 pm
Posts: 11242
Location: The kitten above is not on fire.
smartcat99s wrote:
Hack -- your equation is fine, however gamer's has an error in it.

Blah, I see that. That was at 1:00 AM -- ignore the equation and read the rest. It gives him the right idea, I never meant it to be 100% functional.

Top

 Post subject: Posted: Thu Mar 13, 2008 4:00 am
 Java Junkie

Joined: Mon Jun 14, 2004 10:23 am
Posts: 24222
Location: Granite Heaven
So long as workHours >= 40, your equation works just fine.

If worksHours < 40, though, you will end up subtracting overtime pay from grossPay for each hour less than 40 .. not a good thing. So, you'll need a conditional statement to ensure correct input or a more robust equation to handle workHours < 40.

Top

 Post subject: Posted: Thu Mar 13, 2008 9:26 am
 Malware specialist

Joined: Sun Apr 03, 2005 12:49 pm
Posts: 11696
Location: Kansas City, KS
I probably should have mentioned that I'm using a loop here. (I messed up the spacing of the brackets so just ignore the spacing on them).

It would just normally be a 40 hour work week 5 days a week, 8 hours a day. But some people worked overtime and some worked less than 40.

Sorry if I wasn't more clear to begin with.

Code:
if (workHours < 40)
{
grossPay = (workHours * rateOfPay);
}

else if (workHours >= 40)
{
grossPay = (40*rateOfPay) + ((workHours-40) * rateOfPay * 1.5);
}

Top

 Post subject: Posted: Thu Mar 13, 2008 9:36 am
 Million Club - 5 Plus*

Joined: Sun Sep 12, 2004 6:37 pm
Posts: 4745
Location: In the monkey's litterbox
If you assume valid input (i.e. non-negative hours worked), then you could simplify it down to this:

Code:
if (workHours < 40) {
grossPay = (workHours * rateOfPay);
} else {
grossPay = (40*rateOfPay) + ((workHours-40) * rateOfPay * 1.5);
}

The second if would always return true (with valid input), and then it would save you a conditional check.

Top

 Post subject: Posted: Thu Mar 13, 2008 9:51 am
 Malware specialist

Joined: Sun Apr 03, 2005 12:49 pm
Posts: 11696
Location: Kansas City, KS
Will do. Thanks cat.

And thanks to everyone else for the help

Top

 Post subject: Posted: Thu Mar 13, 2008 10:27 pm
 Bitchin' Fast 3D Z8000*

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
Personally, I would recommend losing the if-else structure and replace the 40 with a variable... 40, after all, is a magic number. I believe the French start getting overtime after 20 hours of work, right? And French-Canadians probably after 22 hours.

Code:
int regularWorkHoursPerWeek = 40;
int overtimeHours = Math.max(0, (workHours - regularWorkHoursPerWeek));
grossPay = (workHours * rateOfPay) + (overtimeHours * rateOfPay * 1.5);

The 1.5 is obviously another magic number that should be replaced.

And before someone goes and does it, I didn't suggest removing the if-else block to 'improve performance'. If anybody starts that "It'll save 2 instructions per iteration" crap and I work in a (sur)real-time enviroment where we don't have a nano-second to spare, I'll throw a compiler book at 'em. Not the dragon book either -- one of the lame ones.

Top

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 1 [ 10 posts ]

 All times are UTC - 8 hours

#### Who is online

Users browsing this forum: No registered users and 2 guests

 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum

Search for:
 Jump to:  Select a forum ------------------ Maximum FAQs    Forum Rules, Posting Guidelines & Announcements    The Good, The Bad & The Banned    FAQs Help/Do It Yourself    PC Building Lab    The Help Desk    PC Modding    Education & Certification Hardware    Nuts & Bolts    Portable Talk    Appraisals, Deals & Bargains (oh my!) OS/Software/Programming    Windows Parlor    Alt.OS.Abode    Games Arena    Programmers' Paradise Networking/Internet    Internet Truckstop    Network Nook In/Out    Magazine and Book Feedback    Forum & Website Feedback    Dog Pound Team Maximum PC Folding at Home    Team Maximum PC - Folding at Home - FIND CURES TO DISEASES    Team MPC - Folding Gauntlets