120 likes | 230 Views
Designing Programs with Branches. CSIS 1595: Fundamentals of Programming and P roblem Solving 1. Tax Example. The program must ask for income and number of dependents.
E N D
Designing Programs with Branches CSIS 1595: Fundamentals of Programming and Problem Solving 1
Tax Example • The program must ask for income and number of dependents. • Deductions are computed as a standard deduction of $5000 plus $2000 per dependent, which is subtracted from the income to get taxable income. • There are three tax brackets based on taxable income • No more than $20,000 tax rate = 10% of taxable income • More than $20,000 and up to $40,000 tax rate = $2000 + 15% of taxable income over $20,000 • Above $40,000 tax rate = $5000 + 20% of taxable income over $40,000 • The tax may not be less than $0 (if it is, than it should be set to $0). • Neither the income nor the number of dependents may be less than 0.
Analysis • What requires a branch? • What is it based on? • How many branches? • What to do down each branch? • What order must branches be in? • What must be decided first? • What other decisions will be based on that decision?
Analysis of Tax Brackets • “There are three tax brackets based on taxable income” • Based on income and deductions, so must get these values and compute taxable income first • Three branches with three ranges • <= 20000 • <= 40000 • Above 40000 • Can do with elif
Analysis of Tax Limit • “The tax may not be less than $0 (if it is, than it should be set to $0).” • If tax < 0, tax = 0 • Otherwise tax unchanged, so can be ifwith no else • Based on tax, so must be after tax bracket • Done for all tax brackets, so must be afterwards instead of nested in each branch
Analysis of Validation • “Neither the income nor the number of dependents may be less than 0.” • Three possibilities: • Income < 0 error message • Dependents < 0 error message • Both legal Do rest of calculations • Depends on income, dependents • Must be done before other branches • All other branches nested inside legal branch
Pseudocode • Prompt for income, dependents • Validate income and dependents If legal: • Compute deductions and taxable income • Branch on taxable income • Income <= 20000 • Income <= 40000 • Else Income > 40000 • Compute tax • If tax < 0, tax = 0 • Print tax Otherwise if dependents < 0, error message • If income < 0, error message • If dependents < 0, error message
Incremental Development • Can build and test program one branch at a time • Usually start with outerbranches first • Can print diagnostic message down each branch first for testing • Run all test cases for that branching • Build code for one branch at a time • …
Tax Program version 1 • Just branches for validation • Temporary message for legal branch • Test cases: income < 0, deductions < 0, legal values
Tax Program version 2 • Add branches for tax brackets, messages for each • Test cases: taxable income for each branch
Tax Program Version 3 • Add code for each tax bracket (can do one at a time) • Add code to compute and print taxes for test cases
Tax Program, version 4 • Add code to check for negative taxes • Test cases: Taxes > 0, taxes < 0