home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Ask a Tech Support Question (free)!

silly java code problem

Reply
Get bargains at  »  Dealighted.com
 
Thread Tools Search this Thread
Currently Active Users: 1829
Discussions: 200,979, Posts: 2,379,832, Members: 246,341
Old February 28th, 2002, 09:23 PM   Digg it!   #1
Member
 
jgargac's Avatar
 
Join Date: Oct 2001
Location: St. Louis, MO
Posts: 463
silly java code problem

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
jgargac is offline   Reply With Quote
Old February 28th, 2002, 09:31 PM     #2
Real gangstas sip on Yacc
 
jkrohn's Avatar
 
Join Date: Oct 2001
Location: Suckas-ville
Posts: 4,552
Send a message via ICQ to jkrohn Send a message via AIM to jkrohn Send a message via Yahoo to jkrohn
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.
jkrohn is offline   Reply With Quote
Old February 28th, 2002, 09:37 PM     #3
Ultimate Member
 
AuraEdge's Avatar
 
Join Date: Oct 2001
Location: Clifton, NJ
Posts: 5,068
Send a message via ICQ to AuraEdge
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.
AuraEdge is offline   Reply With Quote
Old February 28th, 2002, 09:42 PM     #4
Real gangstas sip on Yacc
 
jkrohn's Avatar
 
Join Date: Oct 2001
Location: Suckas-ville
Posts: 4,552
Send a message via ICQ to jkrohn Send a message via AIM to jkrohn Send a message via Yahoo to jkrohn
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
jkrohn is offline   Reply With Quote
Old February 28th, 2002, 10:55 PM     #5
Ultimate Member
 
AuraEdge's Avatar
 
Join Date: Oct 2001
Location: Clifton, NJ
Posts: 5,068
Send a message via ICQ to 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).

Last edited by AuraEdge : February 28th, 2002 at 11:05 PM.
AuraEdge is offline   Reply With Quote
Old March 1st, 2002, 03:33 AM     #6
Member
 
jgargac's Avatar
 
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!
jgargac is offline   Reply With Quote
Old March 1st, 2002, 12:06 PM     #7
Ultimate Member
 
Banti's Avatar
 
Join Date: Oct 2001
Location: Alpharetta, GA
Posts: 1,166
Send a message via Yahoo to Banti
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
Banti is offline   Reply With Quote
Old March 1st, 2002, 12:51 PM     #8
The Mad Redhatter
 
storm2k's Avatar
 
Join Date: Oct 2001
Location: NJ
Posts: 3,552
Send a message via ICQ to storm2k Send a message via AIM to storm2k Send a message via MSN to storm2k Send a message via Yahoo to storm2k
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
storm2k is offline   Reply With Quote
Old March 1st, 2002, 02:18 PM     #9
Ultimate Member
 
Banti's Avatar
 
Join Date: Oct 2001
Location: Alpharetta, GA
Posts: 1,166
Send a message via Yahoo to Banti
storm, to whom are you saying no?


Banti
Banti is offline   Reply With Quote
Old March 1st, 2002, 08:58 PM     #10
Banned
 
qball's Avatar
 
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!
qball is offline   Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Most Active Discussions
Charges against non-tippers dropped.. (19)
Health Care Rationing (9)
Is It Just Me? (3062)
Delete an OS (16)
Nvidia GTX 260 problem (9)
Laptop with wireless problem. (12)
Wireless Televisions. (12)
CPU fan stops spinning randomly (11)
windows vista security holes (18)
Regular Build (11)
Point and Shoot Camera Suggestions. (7)
windows 7 problem (7)
Internet Lost (5)
Multiple Restarts Required at Boot (5)
Recent Discussions
Unallocated Space (0)
help me pls laptop just stopped worki.. (1)
cheap gaming laptop? (11)
C# + LINQ Help (7)
windows vista security holes (18)
[F@H SPAM 11/16/09] ! 1/2 months to r.. (38)
Nvidia GTX 260 problem (9)
Dynex DX E-402 (3)
EVGA 9800 gtx help with finding a goo.. (12)
Multiple Restarts Required at Boot (5)
Point and Shoot Camera Suggestions. (7)
Delete an OS (16)
cell phone won't work (0)
Is the PSU I received dead? (15)
Can't open Word (12)
Steam ID's, Gamertags etc... (4)
Games, Cables, PCI cards, and more fo.. (6)
Dept. of HS: NSA 'Helped' Develop Vis.. (17)
Linksys WMP54GS wireless card problem.. (5)
Help getting around port 80 for camer.. (5)
Skillsoft Network+ Study Software Que.. (10)
Browsers wont load websites (3)
Open With ..... Win7 (3)
Laptop with wireless problem. (12)
Internet Lost (5)


All times are GMT -4. The time now is 01:13 AM.
TechIMO Copyright 2009 All Enthusiast, Inc.