• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Wages not calculating correctly

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

My program is not calculating the wages correctly. Can somebody please tell me why???



[Added code tags and formatted - see UseCodeTags]
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jason. Welcome to the Ranch!

Can you tell us what your inputs were and what your outputs were (and what you expected them to be)?
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your math is wrong buddy.

it should be:

if (hours > max_hours)
{
wages = ((max_hours * payRate) + (((hours - max_hours) * payRate) * overtime))

your way works out the standard hours

(max_hours * payRate)

adds it to

(hours - max_hours)

then times it all by the pay rate

then times it all again by the overtime


my way works out the standard max hours wage, then works out the overtime, then adds them together
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, it doesn't. Multiplication has higher precedence than addition. The addition of those brackets doesn't have an effect.
 
H Biz
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote:No, it doesn't. Multiplication has higher precedence than addition. The addition of those brackets doesn't have an effect.



Ahh I forgot about bodmas, true. Well is there a chance that his payrate is being multiplied by the overtime before it has a chance to multiply the overtime hours?
 
Jason Smit
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote:Hi Jason. Welcome to the Ranch!

Can you tell us what your inputs were and what your outputs were (and what you expected them to be)?



Enter Employee Number: 6541
Enter Pay Rate: 10
Enter Hours Worked: 4
Employee Number: 6541
Pay rate: 10.0
Hours worked: 4.0
Wages: 400.0

This is what the program calculates.It gave me 400. Instead, it should return 40.

 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

H Biz wrote:
it should be:



Please DontBeACodeMill.(⇐click)
 
Marshal
Posts: 28220
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jason Smit wrote:Pay rate: 10.0
Hours worked: 4.0
Wages: 400.0

This is what the program calculates.It gave me 400. Instead, it should return 40.



And that was the result of this line of code, right?



So most likely there's something wrong with that line. Have a closer look at it.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should learn to use System.out.println very liberally. I drop them in all over my code to see what it is really doing. it took me about 8 seconds to figure this out, once i started printing what was going on...



you entered 4 hours. So, "hours > max_hours" is false. We drop to the "else". You should focus on what is happening there, in the "else".
 
Marshal
Posts: 79233
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Apart from the minor error which people have picked up, I think you have got too much in that main method. You are creating an object of the payCheckMethods class and never using it. You ought only to use the main method to start the app, then move all the code out into other methods (preferably in objects).

And welcome again
 
Jason Smit
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Apart from the minor error which people have picked up, I think you have got too much in that main method. You are creating an object of the payCheckMethods class and never using it. You ought only to use the main method to start the app, then move all the code out into other methods (preferably in objects).

And welcome again



Howdy Sheriff. Just in town to get me a jar of whiskey ;). hehe...this is a forum with a sense of humour. I like it

I had my program broken into three java files. There was 3 classes and a main one. Once I figure out the wage issue, I will attempt to do that.
 
Jason Smit
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

fred rosenberger wrote:You should learn to use System.out.println very liberally. I drop them in all over my code to see what it is really doing. it took me about 8 seconds to figure this out, once i started printing what was going on...



you entered 4 hours. So, "hours > max_hours" is false. We drop to the "else". You should focus on what is happening there, in the "else".



The equation is so simple. How can it "mess" up? Is it the Type that is causing the problems? Is it the value assigned to max_hours?
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jason Smit wrote:The equation is so simple. How can it "mess" up? Is it the Type that is causing the problems? Is it the value assigned to max_hours?


What does the max_hours variable represent ?
What does the hours variable represent ?
Which one are you using to calculate the salary ?
 
Jason Smit
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joanne Neal wrote:

Jason Smit wrote:The equation is so simple. How can it "mess" up? Is it the Type that is causing the problems? Is it the value assigned to max_hours?


What does the max_hours variable represent ?
What does the hours variable represent ?
Which one are you using to calculate the salary ?



max_hours is a variable delared with type double. It represents the maximum amount of hours worked for an employee for a week.
hours is a variable that is used to store the value (i.e. number of hours worked) on the system when the user captures when it gets prompt/asked to.

The if statement has two calculations. It all depends on whether the the variables meet the condition inside the bracket. If the the hours is bigger than the max_hours, then it enters the curly braces, and the computer calculates the formula based on the calculation. ELSE if the hours is not bigger than max_hours, then it goes into the curly braces below the "else".

Somehow the figures look correct if it meets the condition (i.e. hours > max_hours). Just the calculation inside else is not working correctly as it should.




 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If somebody has worked 1 hour, you're paying them max_hours * payRate. hours doesn't appear in your formula in the second case.
 
Jason Smit
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote:If somebody has worked 1 hour, you're paying them max_hours * payRate. hours doesn't appear in your formula in the second case.



HAHAHA

Shees...How could I have missed that lol...must be the whiskey...

Variable should be hours in that equation then. Thanks guys
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic