Testing and Debugging Example2

advertisement
Testing and Debugging Example:
Consider the following plan for the given question.
For an employee, read in the name, the rate of pay per hour and the number of hours for
each day that the employee has worked for a 5 day working week. For the employee
determine the gross pay, tax to be paid at a rate of 12% on gross pay if the gross pay is less
than $300 and 15% otherwise, and the resulting net pay for the week. Print out the payslip
details with name, gross pay, tax paid and net pay.
b) Modify the program to enter the data for any number of employees. Data is terminated
with name of “quit”. Also determine the average number of hours worked per day.
c) Modify the program further to pay the employee a bonus if they have worked more than
35 hours for the week and have worked every day. Bonus is calculated at one and half times
the hourly rate for every hour over 35 hours for the week.
Input
variableName
type
where
name
string
User – screen
rateOfPay
float
User – screen
numberHours
float
User – screen - loop
Constants
lowTaxRate  12% (or 0.12)
highTaxRate  15% (or 0.15)
taxLimit  300
Output
variableName
grossPay
taxPaid
netPay
format
currency 2dp
currency 2dp
currency 2dp
Calculations and Processing
totalHours  Sum code for hours over the week (loop)
grossPay  rateOfPay * totalHours
if grossPay < taxLimit
taxPaid  grossPay * lowTaxRate
else
taxPaid  grossPay * highTaxRate
netPay  grossPay - taxPaid
where
Sprite – on screen
Sprite – on screen
Sprite – on screen
Processing # how to scaffold your solution with versions of algorithm
Sprites
Input Sprite
Costume
person
Name
employee
Thread1
Event signal
Event handling
Green flag clicked
Version1
#Set up loop to input the data from the user
Input name, rateOfPay
For counter from 1 to 5
Ask user for hours
#end code
Version 2
#Set up loop to input the data from the user
#add code for sum of hours
totalHours  0
Input name, rateOfPay
For counter from 1 to 5
Ask user for hours
Add hours to TotalHours
Print TotalHours (for checking if correct)
#end code
Version 3
#Set up loop to input the data from the user
#add code for sum of hours
#add code for calculations
totalHours  0
Input name, rateOfPay
For counter from 1 to 5
Ask user for hours
Add hours to TotalHours
Print TotalHours (for checking if correct)
grossPay  rateOfPay * totalHours
if grossPay < taxLimit
taxPaid  grossPay * lowTaxRate
else
taxPaid  grossPay * highTaxRate
netPay  grossPay - taxPaid
display name, grossPay, taxPaid, netPay
#end code
Version 4
#Set up loop to input the data from the user
#add code for sum of hours
#add code for calculations
#add code for (b) part of question for MERIT
Enter name
While name <> “quit”
totalHours  0
Input name, rateOfPay
For counter from 1 to 5
Ask user for hours
Add hours to totalHours
Print TotalHours (for checking if correct)
Calculate averageHours
grossPay  rateOfPay * totalHours
if grossPay < taxLimit
taxPaid  grossPay * lowTaxRate
else
taxPaid  grossPay * highTaxRate
netPay  grossPay - taxPaid
display name, grossPay, taxPaid, netPay,
display averageHours
enter name
#end code
Version 5
#Set up loop to input the data from the user
#add code for sum of hours
#add code for calculations
#add code for (b) part of question for MERIT
#add code for (c) part of question for EXCELLENCE
Enter name
While name <> “quit”
#set flag to check for absence
Flag  0
totalHours  0
Input name, rateOfPay
For counter from 1 to 5
Ask user for hours
Add hours to TotalHours
If hours = 0
Flag  1
Print TotalHours (for checking if correct)
Calculate averageHours
grossPay  rateOfPay * totalHours
if grossPay < taxLimit
taxPaid  grossPay * lowTaxRate
else
taxPaid  grossPay * highTaxRate
netPay  grossPay - taxPaid
display name, grossPay, taxPaid, netPay
display averageHours
check if eligible for bonus
calculate and display bonus
enter name
#end code
Step 1
Determine test data of Expected inputs and calculated outputs. Ensure that both lowTaxRate and
highTaxRate are applied
TestData
name
rateOfpay
Hours over five
grossPay
taxPaid
netPay
days
Adam Smith
18.75
6, 5.5, 8, 6.75, 7
….
Step 2
Code version 1. Ensure you have adequately commented your code.
Test and debug with expected data.
Screen dump of code with output showing correct program.
If there is an error with code – make screen dump of incorrect code, explain why incorrect, and then
screen dump of corrected code.
Step 3
Code version 2. Ensure you have adequately commented your code.
Test and debug with expected data.
Screen dump of code with output showing correct program.
If there is an error with code – make screen dump of incorrect code, explain why incorrect, and then
screen dump of corrected code.
Step 4
Code version 3. Ensure you have adequately commented your code.
Test and debug with expected data.
Screen dump of code with output showing correct program.
If there is an error with code – make screen dump of incorrect code, explain why incorrect, and then
screen dump of corrected code.
Confirm program is correct on expected testdata
UP TO HERE IS ACHIEVED.
Step 5
Code version 4. Ensure you have adequately commented your code.
Test and debug with expected data. You may have to add to your test data table
Screen dump of code with output showing correct program.
If there is an error with code – make screen dump of incorrect code, explain why incorrect, and then
screen dump of corrected code.
Step 6
Determine some test data for exceptional (“not normal”) data, some boundary values, some out of
range values
Start a testing table (see hints document)
Test Data
quit
What is being
tested
Output if no data
entered
grossPay equal to
300
Negative number
entered for
rateOfpay
Hours greater
than 24
Test type
Expected result
Actual result
Exceptional (“not
normal” data
Boundary values
Out of range data
Out of range data
Test and debug with data as in above table.
Screen dump of code with output showing actual result.
Complete the above table to compare expected results with actual results
Conclusion that program correct, or determine how to correct error if one occurs
If there is an error with code – make screen dump of incorrect code, explain why incorrect, and then
screen dump of corrected code.
UP TO HERE FOR MERIT
Step 7
Code version 5. Ensure you have adequately commented your code.
Test and debug with expected data. You may have to add to your test data table to ensure all code
for expected data is correct.
Screen dump of code with output showing correct program.
If there is an error with code – make screen dump of incorrect code, explain why incorrect, and then
screen dump of corrected code.
Continue testing table for some invalid data, as well as additional boundary and out of range data
Note: in Scratch, you can confirm if a data entry is a number as opposed to a string by checking if
data divided by 1 gives back the same answer. Place this test condition inside a repeat loop that asks
user to enter data until valid data is entered.
Try and think of all possible ways data entry could crash your program, and add code to ensure that
it is robust enough to prevent this.
Test Data
What is being
Test type
Expected result
Actual result
tested
quit
Output if no data Exceptional (“not
entered
normal” data
grossPay equal to Boundary values
300
Negative number Out of range data
entered for
rateOfpay
Hours greater
Out of range data
than 24
Hours equal to 35 Boundary values
rateOfPay equal
Exceptional (“not
to zero
normal” data,
boundary
Hours values on
Boundary values
either side of zero (borderline)
Ten not accepted Erroneous, Data
Data rejected
as valid number
validation
Test and debug with data as in above table.
Screen dump of code with output showing actual result, especially invalid data entry.
Complete the above table to compare expected results with actual results
Conclusion that program correct, or determine how to correct error if one occurs
If there is an error with code – make screen dump of incorrect code, explain why incorrect, and then
screen dump of corrected code.
UP TO HERE FOR EXCELLENCE
Download