Download presentation

Presentation is loading. Please wait.

Published byToni Purnell Modified over 3 years ago

1
Programming Example: tax computation

2
Introduction In this webpage, we will study a programming example using the conditional statements (if and if-else) to write a Java program to compute the tax owed.

3
Formula to compute the tax owed Decision diagram to compute taxes owed:

4
Formula to compute the tax owed (cont.) Example 1: someone single earning $40,000 pays: 15% of $21,450, plus 28% of ($40,000 − $21,450)

5
Formula to compute the tax owed (cont.) Graphically:

6
Formula to compute the tax owed (cont.) 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)

7
Formula to compute the tax owed (cont.) Graphically:

8
Formula to compute the tax owed (cont.) Algorithm: Define the following constants: S1 = 21450 S2 = 30450 M1 = 35800 M2 = 86500

9
Formula to compute the tax owed (cont.) Structure diagram:

10
Formula to compute the tax owed (cont.) Example input: status = 'S' and income = 40000

11
Formula to compute the tax owed (cont.) The tax computation algorithm in Java: (The whole program is too large to show here). if ( status == 'S' ) { // ***** single if ( income <= S1 ) tax = RATE1 * income; else if ( income <= S2 ) tax = RATE1 * S1 + RATE2 * (income - S1); else tax = RATE1 * S1 + RATE2 * (S2 - S1) + RATE3 * (income - S2); }

12
Formula to compute the tax owed (cont.) else { // ***** married if ( income <= M1 ) tax = RATE1 * income; else if ( income <= M2 ) tax = RATE1 * M1 + RATE2 * (income - M1); else tax = RATE1 * M1 + RATE2 * (M2 - M1) + RATE3 * (income - M2); }

13
Formula to compute the tax owed (cont.) Constants used in the program: RATE1 = 0.15 (Tax rate) RATE2 = 0.28 RATE3 = 0.31 S1 = 21450 (Income brackets) S2 = 51900 M1 = 35800 M2 = 86500

14
Formula to compute the tax owed (cont.) Java program: import java.util.Scanner; public class ComputeTax { public static void main(String[] args) { final double RATE1 = 0.15; // Tax rates final double RATE2 = 0.25; final double RATE3 = 0.31; final double S1 = 21450.0; // Tax brackets for single final double S2 = 51900.0; final double M1 = 35800.0; // Tax brackets for married final double M2 = 86500.0;

15
Formula to compute the tax owed (cont.) char status; double income = 0; double tax = 0; Scanner in = new Scanner(System.in); // Construct Scanner object System.out.print("Enter status (S or M) = "); status = in.next().charAt(0); // Read in status System.out.print("Enter income = "); income = in.nextDouble(); // Read in income >>>> Insert the Tax Computation Algorithm (see above) here <<<< System.out.println("Tax = " + tax); }

16
Formula to compute the tax owed (cont.) Programming advice: Using symbolic constants in computer program is highly recommended because when the tax rates or income brackets changes, we can easily accommodate the changes by changing the values of the constant definitions at the start of the program. We don't have to search for the constants inside the program !

17
Formula to compute the tax owed (cont.) Example Program: (The entire program is here) –Prog file: http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/06/Progs/ ComputeTax.java How to run the program: Right click on link and save in a scratch directory To compile: javac ComputeTax.java To run: java ComputeTax

Similar presentations

OK

Warm Up 1. 800 0.04 3 2. 1,700 2 0.25 3. 900 0.05 4 4. 1,200 6 0.35 96 850 180 Course 2 6-7 Simple Interest 2,520 Thursday February 2,

Warm Up 1. 800 0.04 3 2. 1,700 2 0.25 3. 900 0.05 4 4. 1,200 6 0.35 96 850 180 Course 2 6-7 Simple Interest 2,520 Thursday February 2,

© 2018 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google