|
| 1 | +/** |
| 2 | + * (Financial application: print a tax table) Listing 3.5 gives a program to compute |
| 3 | + * tax. Write a method for computing tax using the following header: |
| 4 | + * public static double computeTax(int status, double taxableIncome) |
| 5 | + * Use this method to write a program that prints a tax table for taxable income from |
| 6 | + * $50,000 to $70,000 with intervals of $100 for all the following statuses: |
| 7 | + * Taxable Single Married Joint Married Head of |
| 8 | + * Income or Qualifying Separate a House |
| 9 | + * Widow(er) |
| 10 | + * 50000 8688 6665 8688 7353 |
| 11 | + * 50100 8713 6680 8713 7378 |
| 12 | + * ... |
| 13 | + * 69900 13663 9850 12328 9840 |
| 14 | + * 70000 13688 9875 12353 9853 |
| 15 | + * Hint: round the tax into integers using Math.round (i.e., Math |
| 16 | + * .round(computeTax(status, taxableIncome))). |
| 17 | + * |
| 18 | + * Created by Sven on 07/07/19. |
| 19 | + */ |
| 20 | +package Chapter06; |
| 21 | + |
| 22 | +public class Exercise0615_Financial_application_print_a_tax_table { |
| 23 | + public static void main(String[] args) { |
| 24 | + System.out.println("Taxable Income Single Married Joint or Qualifying Widow(er) Married Separate Head of a house"); |
| 25 | + System.out.println("--------------------------------------------------------------------------------------------------------"); |
| 26 | + for (int i = 50000; i <= 70000; i += 100) { |
| 27 | + System.out.printf("%-18d%-10d%-41d%-20d%d\n", |
| 28 | + i, |
| 29 | + Math.round(computeTax(0, i)), |
| 30 | + Math.round(computeTax(1, i)), |
| 31 | + Math.round(computeTax(2, i)), |
| 32 | + Math.round(computeTax(3, i))); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + public static double computeTax(int status, double taxableIncome) { |
| 37 | + double tax = 0; |
| 38 | + |
| 39 | + if (status == 0) { // Compute tax for single filers |
| 40 | + if (taxableIncome <= 8350) |
| 41 | + tax = taxableIncome * 0.10; |
| 42 | + else if (taxableIncome <= 33950) |
| 43 | + tax = 8350 * 0.10 + (taxableIncome - 8350) * 0.15; |
| 44 | + else if (taxableIncome <= 82250) |
| 45 | + tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (taxableIncome - 33950) * 0.25; |
| 46 | + else if (taxableIncome <= 171550) |
| 47 | + tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (82250 - 33950) * 0.25 + (taxableIncome - 82250) * 0.28; |
| 48 | + else if (taxableIncome <= 372950) |
| 49 | + tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (82250 - 33950) * 0.25 + (171550 - 82250) * 0.28 + (taxableIncome - 171550) * 0.33; |
| 50 | + else |
| 51 | + tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (82250 - 33950) * 0.25 + (171550 - 82250) * 0.28 + (372950 - 171550) * 0.33 + (taxableIncome - 372950) * 0.35; |
| 52 | + } else if (status == 1) { // Compute tax for married file jointly |
| 53 | + if (taxableIncome <= 16700) |
| 54 | + tax = taxableIncome * 0.10; |
| 55 | + else if (taxableIncome <= 67900) |
| 56 | + tax = 16700 * 0.10 + (taxableIncome - 16700) * 0.15; |
| 57 | + else if (taxableIncome <= 137050) |
| 58 | + tax = 16700 * 0.10 + (67900 - 16700) * 0.15 + (taxableIncome - 67900) * 0.25; |
| 59 | + else if (taxableIncome <= 208850) |
| 60 | + tax = 16700 * 0.10 + (67900 - 16700) * 0.15 + (137050 - 67900) * 0.25 + (taxableIncome - 137050) * 0.28; |
| 61 | + else if (taxableIncome <= 372950) |
| 62 | + tax = 16700 * 0.10 + (67900 - 16700) * 0.15 + (137050 - 67900) * 0.25 + (208850 - 137050) * 0.28 + (taxableIncome - 208850) * 0.33; |
| 63 | + else |
| 64 | + tax = 16700 * 0.10 + (67900 - 16700) * 0.15 + (137050 - 67900) * 0.25 + (208850 - 137050) * 0.28 + (372950 - 208850) * 0.33 + (taxableIncome - 372950) * 0.35; |
| 65 | + } else if (status == 2) { // Compute tax for married separately |
| 66 | + if (taxableIncome <= 8350) |
| 67 | + tax = taxableIncome * 0.10; |
| 68 | + else if (taxableIncome <= 33950) |
| 69 | + tax = 8350 * 0.10 + (taxableIncome - 8350) * 0.15; |
| 70 | + else if (taxableIncome <= 68525) |
| 71 | + tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (taxableIncome - 33950) * 0.25; |
| 72 | + else if (taxableIncome <= 104425) |
| 73 | + tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (68525 - 33950) * 0.25 + (taxableIncome - 68525) * 0.28; |
| 74 | + else if (taxableIncome <= 186475) |
| 75 | + tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (68525 - 33950) * 0.25 + (104425 - 68525) * 0.28 + (taxableIncome - 104425) * 0.33; |
| 76 | + else |
| 77 | + tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (68525 - 33950) * 0.25 + (104425 - 68525) * 0.28 + (186475 - 104425) * 0.33 + (taxableIncome - 186475) * 0.35; |
| 78 | + } else if (status == 3) { // Compute tax for head of household |
| 79 | + if (taxableIncome <= 11950) |
| 80 | + tax = taxableIncome * 0.10; |
| 81 | + else if (taxableIncome <= 45500) |
| 82 | + tax = 11950 * 0.10 + (taxableIncome - 11950) * 0.15; |
| 83 | + else if (taxableIncome <= 117450) |
| 84 | + tax = 11950 * 0.10 + (45500 - 11950) * 0.15 + (taxableIncome - 45500) * 0.25; |
| 85 | + else if (taxableIncome <= 190200) |
| 86 | + tax = 11950 * 0.10 + (45500 - 11950) * 0.15 + (117450 - 45500) * 0.25 + (taxableIncome - 117450) * 0.28; |
| 87 | + else if (taxableIncome <= 372950) |
| 88 | + tax = 11950 * 0.10 + (45500 - 11950) * 0.15 + (117450 - 45500) * 0.25 + (190200 - 117450) * 0.28 + (taxableIncome - 190200) * 0.33; |
| 89 | + else |
| 90 | + tax = 11950 * 0.10 + (45500 - 11950) * 0.15 + (117450 - 45500) * 0.25 + (190200 - 117450) * 0.28 + (372950 - 190200) * 0.33 + (taxableIncome - 372950) * 0.35; |
| 91 | + } |
| 92 | + |
| 93 | + return tax; |
| 94 | + } |
| 95 | +} |
0 commit comments