Wednesday, October 5, 2011

Notes for Assignment 2

Remember to break it into methods. Each method should do one thing. For example, one method can calculate the gross pay, another method can deduct social security, etc.

To calculate Overtime we need to us an if statement. We need to do one of two things depending on if the hours are greater than 40 or 40 or less. This can be set up as follows:

if(hours > 40)
{
     pay=rate * (40 + ((hours - 40) * 1.5);
}
else
{
    pay=rate * hours;
}

If the hours are greater than forty, we multiply the rate times forty (because we know we have over forty hours and forty of them are paid at the regular rate) and then add the hours over forty times rate time 1.5 to get time and a half. The parenthesis are necessary to control the order of operations. We want the subtraction to occur first, then the multiplication, then the addition. If, on the other hand, the hours are 40 or less, we just multiply hours times the rate of pay.

For the assignment, you don't have to display the overtime pay separate from the regular pay. You can just display the Gross (total) pay, the deductions and the Net pay (what's left over). Though, if you want to add a challenge you can separate the two in the display.

No comments:

Post a Comment