1 / 17

Programming Example: tax computation

Programming Example: tax computation. 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. Formula to compute the tax owed. Decision diagram to compute taxes owed:.

marcus
Download Presentation

Programming Example: tax computation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 bracketschanges, 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 constantsinside 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

More Related