silly java code problem
 |
|
|
February 28th, 2002, 09:23 PM
|
#1
|
|
Member
Join Date: Oct 2001
Location: St. Louis, MO
Posts: 463
|
I've written a program to calculate pay based on the hours worked and the pay scale entered. It's supposed to calculate the pay for 40 hours of work and then calculate time and a half for any hours over 40. Well, my code forgets to take out the original 40 hours that would be paid at regular time. Any ideas?
//calculate overtime
if ( hrs > 40 )
overtime = hrs - 40;
if ( hrs <= 40 )
overtime = 0;
//calculate pay
totalPay = ( hrs * rate ) + ( overtime * ( rate * 1.5 ));
Thanks,
Jeff
|
|
|
February 28th, 2002, 09:31 PM
|
#2
|
|
Real gangstas sip on Yacc
Join Date: Oct 2001
Location: Suckas-ville
Posts: 4,552
|
Well there looks to be nothing wrong with your code.
For starters make it
if( hrs > 40)
overtime = hrs - 40;
else
overtime = 0;
totalPay = (( hrs * rate ) + ( overtime * (rate * 1.5));
This saves an extra comparision.
One thing to check is that rate and hours are doubles and not ints.
Other than that the code *should* work fine. Try putting a few System.out.print() on your variables at various locations to see exactly what it does.
Jkrohn
__________________
Signatures blow hard
If your signature contains an ad of any kind, congratulations, you're on my ignore list.
|
|
|
February 28th, 2002, 09:37 PM
|
#3
|
|
Ultimate Member
Join Date: Oct 2001
Location: Clifton, NJ
Posts: 5,068
|
I know no programming, but from an algebra perspective, i see the problem. WHen paying OT, you are counting hours twice.
Once in Hrs*Rate, and 1.5 times with OT*1.5, totally 2.5 times.
If you make the last line rate*.5, then you have it.
the way you have it, your paying an EXTRA time and a half for OT. It's supposed to be a half more than normal rate, so just add the half more.
|
|
|
February 28th, 2002, 09:42 PM
|
#4
|
|
Real gangstas sip on Yacc
Join Date: Oct 2001
Location: Suckas-ville
Posts: 4,552
|
You nailed it *doh*
Say hours worked = 60.
you are paying 60*rate + 20*rate*1.5
change it to
if( hrs > 40)
{
overtime = hrs - 40;
hrs = 40;
}
else
overtime = 0;
totalPay = ((hrs * rate) + (overtime * (rate *1.5));
 Thanks Aura
Jkrohn
|
|
|
February 28th, 2002, 10:55 PM
|
#5
|
|
Ultimate Member
Join Date: Oct 2001
Location: Clifton, NJ
Posts: 5,068
|
Just curious, but If you wanted to output the hours on the statement too, doing it your way, wouldnt it show any hours over 40 to be 40, because you set "hrs" to 40 if any OT is paid?
You could do an (jargons gunna be all wrong here since I never took a programming course and I'm just basing this off what I see so far this thread)
(NH = Normal hours)
------
If (hrs < 40)
NH = hrs and OT = 0;
else
NH = 40 and OT = hrs - 40;
totalPay = (NH * rate) + (OT * (rate * 1.5))
-----
That way, if you needed to, your hrs, NH, and OT outputs would still be correct.
Edited for self stupidity. Re-edited to "fix my code" (I think i did anyways).
Last edited by AuraEdge : February 28th, 2002 at 11:05 PM.
|
|
|
March 1st, 2002, 03:33 AM
|
#6
|
|
Member
Join Date: Oct 2001
Location: St. Louis, MO
Posts: 463
|
Thanks Jkron & AuraEdge. You nailed my problem right on the head. It was counting my hours twice and I couldn't figure it out. I appreciate the help and will try your sample code tomorrow when i have a little more energy. Thanks again,
Jeff
PS. I did set these up as double floating point numbers as JBuilder gave me a fit b/c they were originally integers  It was going by what I coded, not what I meant. Imagine that!
|
|
|
March 1st, 2002, 12:06 PM
|
#7
|
|
Ultimate Member
Join Date: Oct 2001
Location: Alpharetta, GA
Posts: 1,166
|
Quote:
Originally posted by AuraEdge
Just curious, but If you wanted to output the hours on the statement too, doing it your way, wouldnt it show any hours over 40 to be 40, because you set "hrs" to 40 if any OT is paid?
You could do an (jargons gunna be all wrong here since I never took a programming course and I'm just basing this off what I see so far this thread)
(NH = Normal hours)
------
If (hrs < 40)
NH = hrs and OT = 0;
else
NH = 40 and OT = hrs - 40;
totalPay = (NH * rate) + (OT * (rate * 1.5))
-----
That way, if you needed to, your hrs, NH, and OT outputs would still be correct.
Edited for self stupidity. Re-edited to "fix my code" (I think i did anyways).
|
I think you could also just...
Code:
If (hrs < 40)
{
OT = 0;
}
else
{
OT = hrs - 40;
}
totalPay = (NH * rate) + (OT * (rate * 0.5))
Banti
|
|
|
March 1st, 2002, 12:51 PM
|
#8
|
|
The Mad Redhatter
Join Date: Oct 2001
Location: NJ
Posts: 3,552
|
no, hours would have to be set to 40 again, or you can't compute the total properly.
think in terms of the real world. you get paid for 40 hours at your normal rate, and then you get paid any remaining hours beyond 40. how I would do it is like this...
Code:
//establish a static variable that equals 40. this is your normal hours.
normalHours = 40;
//figure out how much overtime you have
if (hours > 40)
overtimeHours = hours - normalHours;
else
overtimeHours = 0;
//now calculate the pay
pay = (rate*normalHours) + ((1.5*rate)*overtimeHours);
//finally, pipe out the result
return pay;
by having that variable normalHours, you don't have to worry about resetting any varibales, and you can make sure you count only the number of hours you need.... if there's no overtime, the right side of that expression will equal zero, and you will only count 40 hours at the normal rate, without having to worry about the hours variable being wrong. just make sure that the values are all doubles or floats, not ints, or you won' be able to do out decimals...
hope this helps
|
|
|
March 1st, 2002, 02:18 PM
|
#9
|
|
Ultimate Member
Join Date: Oct 2001
Location: Alpharetta, GA
Posts: 1,166
|
storm, to whom are you saying no?
Banti
|
|
|
March 1st, 2002, 08:58 PM
|
#10
|
|
Banned
Join Date: Oct 2001
Posts: 447
|
just being silly but, this formula will work
payme = max(0, ((hrs-40)*1.5*rate) + min(hrs, 40)*rate;
so iffin hrs = 13
payme = max(0, ((-27)*1.5*rate) + min(13,40)*rate;
payme = 0 + 13*rate;
payme = 13*rate;
so iffin hours = 60 (my daily sleep)
payme = max(0,((20)*1.5*rate + min(60,40)*rate;
payme = 30*rate + 40*rate;
payme = 70*rate;
iffin my bidness was a sleepin, bidness be good!
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
|
Most Active Discussions
|
|
|
|
|
Recent Discussions 
|
|
|
|
|
|