• Introduction

• We study an advanced example of the if statement by constructing a program to compute the tax owed.

• Tax owed

• Decision diagram to compute taxes owed:

• Example 1: someone single earning \$40,000 pays:

• 15% of \$21,450, plus
• 28% of (\$40,000 - \$21,450)

• Example 2: someone single earning \$60,000 pays:

• 15% of \$21,450, plus
• 28% of (\$51,900 - \$21,450), plus
• 31% of (\$60,000 - \$51,900)

• Inputs needed:

• Marital status (status)
• Income level (income)

• Tax computation algorithm:

 ```22: if (status == SINGLE) 23: { // ***** single 24: if (income <= SINGLE_BRACKET1) 25: tax = RATE1 * income; 26: else if (income <= SINGLE_BRACKET2) 27: tax = RATE1 * SINGLE_BRACKET1 28: + RATE2 * (income - SINGLE_BRACKET1); 29: else 30: tax = RATE1 * SINGLE_BRACKET1 31: + RATE2 * (SINGLE_BRACKET2 - SINGLE_BRACKET1) 32: + RATE3 * (income - SINGLE_BRACKET2); 33: } 34: else 35: { // ***** married 36: if (income <= MARRIED_BRACKET1) 37: tax = RATE1 * income; 38: else if (income <= MARRIED_BRACKET2) 39: tax = RATE1 * MARRIED_BRACKET1 40: + RATE2 * (income - MARRIED_BRACKET1); 41: else 42: tax = RATE1 * MARRIED_BRACKET1 43: + RATE2 * (MARRIED_BRACKET2 - MARRIED_BRACKET1) 44: + RATE3 * (income - MARRIED_BRACKET2); 45: } ```

• Constants used:

• RATE1 = 0.15
• RATE2 = 0.28
• RATE3 = 0.31

• SINGLE_BRACKET1 = 21450
• SINGLE_BRACKET2 = 51900

• MARRIED_BRACKET1 = 35800
• MARRIED_BRACKET2 = 86500

• Here is the TaxReturn class that compute the tax owed (from book)

• Here is a program that uses the TaxReturn class.

It constructs a TaxReturn object using a status and a income.

• Example Program: (Demo above code)