Program DesignCP1020 -Week 12
Last
One
Aims:
- Demonstrate a simple development method
- Develop a partial solution to a larger problem
- Introduce an incremental program coding and testing strategy
Problem Specification
ACME Widgets plc require a program to calculate and display the pay details for hourly paid workers.
Gross pay is calculated by multiplying the hours worked by the hourly rate, but any hours worked over 40 are treated as overtime and paid at "time and a half” rate.
...continued
ACME Widgets PLC
Tax deductions must also be calculated.
The first £100 each week is tax free, the next £100 is taxed at 25% and any pay over £200 is taxed at 50%.
Net pay is gross pay minus all deductions.
Tackling the problem.
1. Read the problem
2. Read it again!
3. Work through problem on paper; prepare test data & results
4. Design solution
5. Code solution
6. Test solution
7. Document
Steps 1 and 2
Read the spec. carefully.
Underline the important bits
Check out any queries you have
Make sure you’ve got all the facts
Step 3. Prepare test data
e.g. Employee No.1 works 20 hours at £3.00 per hour.
Gross pay = 20 x £3.00 = £60
No tax payable (first £100 tax free) therefore deductions = 0
Net Pay = Gross Pay - deductions = £60
(Total wage bill so far = £60 )
More Test Data
e.g. Employee No. 2 works 40 hours at £4.00 per hour
Gross pay = 40 x £4.00 = £160.00
Tax payable on £60 at 25% = £15.00
Net Pay = £160 - £15 = £145.00
Yet more test data
e.g. Employee No. 3 works 50 hours at £5.00 per hour. (N.B. 10 hours overtime!)
Gross pay = 40 hours at £5.00 = £200.00
10 hours at £7.50 = £ 75.00
Total = £275.00
Further calculations
Tax = £100 at 25% = £25
+ £75 at 50% = £37.50
total = £62.50
Net Pay =£275 - £62.50 = £212.50
Step 4. Design a Solution
Questions:
a. WHAT is the program to do?
b. WHAT are the inputs to the program?
c. WHAT are the outputs from the program?
Design - solution
Answers:
a. Calculate and display employee's pay
b. Hours worked and hourly rate
c. Gross pay(?), Tax, Net pay.
Data Dictionary
List all items you think you’ll need input variables output variables interim variables
Give each a Name and a typeNote what you plan to use each one for
Splitting the Program into tasks
1. Read in the employee's hours and rate.
2. Calculate Gross pay
3. Calculate Tax payable
4. Calculate Net pay
5. Print out pay details.
Design again1. Read in the employee's hours and rate.
1.1 get hours worked and hourly rate
2. Calculate Gross pay2.1 if hours worked > 40 then
gross pay = 40 X hourly rate
+ (hours worked -40) X (hourly rate * 1.5)
else
gross pay = hours worked X hourly rate
3. Calculate Tax payable3.1 If gross pay > £200 then
tax = (gross pay - 200) * Tax High rate
+ (100 * Tax low rate)
else if gross pay > £100 then
tax = (gross pay - 100) * Tax low rate
else
tax = 0
More design
4. Calculate Net pay4.1 Net pay = gross pay - tax
5. Print out pay details.5.1 print gross pay
5.2 print tax
5.3 print net pay
Data Dictionary
NAME TYPE DESCRIPTION
fHours single No of hours worked
fPayRate single Rate per hour
fGrossPay single Total pay before tax
fTax single Tax paidfTaxLowRate single Lower tax rate 25p
fTaxHighRate single Higher tax rate 50p
fNetPay single Net Pay
5. Code the solution
Use your design and data dictionary to develop the code
Remember to use sensible names put in comments/remarks use indentation
You could code it in small blocks and test by adding a few dummy print statements
The code
REM payroll program
REM written by I Coulson
REM ////declare variables/////
DIM fHours, fGrossPay, fTax AS SINGLE
DIM fNetPay, fPayRate AS SINGLE
DIM fTaxLowRate, fTaxHighRate AS SINGLE
fTaxLowRate = .25
fTaxHighRate = .50
CLS
PRINT " ACME WIDGETS PLC Payroll”
REM Read in the employee data
INPUT ”How many hours did this employee work ? "; fHours
INPUT "And the hourly rate for this employee "; fPayRate
REM Calculate gross pay and overtime
if fHours > 40 then
fGrossPay = 40 * fPayRate
+ ( fHours - 40) * ( fPayRate * 1.5 )
else
fGrossPay = fPayRate * fHours
REM Calculate Tax payable
IF fGrossPay > 200 THEN
fTax=(100 * fTaxLowRate)+
((fGrossPay-200) * fTaxHighRate)
ELSEIF fGrossPay > 100 THEN
fTax = (fGrossPay - 100) * fTaxLowRate
ELSE
fTax = 0 REM No Tax to pay
END IF
REM Calculate Net pay
fNetPay = fGrossPay - fTax
REM Display Employees Pay Details
PRINT "gross pay is "; fGrossPay
PRINT "Pay after tax of "; fTax; " is "; NetPay
PRINT "*******************************************"
6. Test the solution
Use your test cases and run the programCheck a few other possible valuesIf there are problems
rework the design THEN rewrite the offending code
7. Documentation
Others need to know about the program other programmers the users the management!
Programmers guide
design data dictionary Algorithm
testingcode
comments layout variable names
The User Guide
Those who use the program need to know: how to get it started the data required from them the screens they might see how to quit the program