The program in Listing 3.4 uses if statements to check the filing status and computes the tax based on the filing status. This example uses methods to simplify Listing 3.4.
Each filing status has six brackets. The code for computing taxes is nearly the same regardless of filing status except that each filing status has different bracket ranges. For example, the brackets of the single- filer status are [0, 6000], (6000, 27950], (27950, 67700], (67700, 141250], (141250, 307050], (307050, ), and the six brackets of the married “file jointly status are [0, 12000], (12000, 46700], (46700, 112850], (112850, 171950], (171950, 307050], (307050 ). The first bracket of each filing status is taxed at 10%, the second at 15%, the third at 27%, the fourth at 30%, the fifth at 35%, and the sixth at 38.6%. So you can write a method with the brackets as arguments to compute the tax for the filing status. The header of the method is:
For example, you can invoke computeTax(400000, 6000, 27950, 67700, 141250, 307050) to compute the tax for single filers with $400,000 of taxable income.
Listing 5.5 gives the solution to the problem. The output of the program is similar to Figure 3.3.
1 import javax.swing.JOptionPane; 2 3 public class ComputeTaxWithMethod { 4 public static void main(String[] args) { 5 // Prompt the user to enter filing status 6 String statusString = JOptionPane.showInputDialog( 7 "Enter the filing status:" ); 8 int status = Integer.parseInt(statusString); 9 10 // Prompt the user to enter taxable income 11 String incomeString = JOptionPane.showInputDialog( 12 "Enter the taxable income:" ); 13 double income = Double.parseDouble(incomeString); 14 15 // Display the result 16 JOptionPane.showMessageDialog( null , "Tax is " + 17 ( int )( computeTax(status, income ) * 100 ) / 100.0 ); 18 } 19 20 public static double computeTax( double income, 21 int r1, int r2, int r3, int r4, int r5) { 22 double tax = ; 23 24 if (income <= r1) 25 tax = income * 0.10 ; 26 else if (income <= r2) 27 tax = r1 * 0.10 + (income - r1) * 0.15 ; 28 else if (income <= r3) 29 tax = r1 * 0.10 + (r2 - r1) * 0.15 + (income - r2) * 0.27 ; 30 else if (income <= r4) 31 tax = r1 * 0.10 + (r2 - r1) * 0.15 + 32 (r3 - r2) * 0.27 + (income - r3) * 0.30 ; 33 else if (income <= r5) 34 tax = r1 * 0.10 + (r2 - r1) * 0.15 + (r3 - r2) * 0.27 + 35 (r4 - r3) * 0.30 + (income - r4) * 0.35 ; 36 else 37 tax = r1 * 0.10 + (r2 - r1) * 0.15 + (r3 - r2) * 0.27 + 38 (r4 - r3) * 0.30 + (r5 - r4) * 0.35 + (income - r5) * 0.386 ; 39 40 return tax; 41 } 42 43 public static double computeTax(int status, double income) { 44 switch (status) { 45 case 0: return 46 computeTax(income, 6000 , 27950 , 67700 , 141250 , 307050 ); 47 case 1: return 48 computeTax(income, 12000 , 46700 , 112850 , 171950 , 307050 ); 49 case 2: return 50 computeTax(income, 6000 , 23350 , 56425 , 85975 , 153525 ); 51 case 3: return 52 computeTax(income, 10000 , 37450 , 96700 , 156600 , 307050 ); 53 default: return 0 ; 54 } 55 } 56 } |
This program does the same thing as the one in Listing 3.4. Instead of writing the same code for computing taxes for different filing statuses, the new program uses a method for computing taxes. Using the method not only shortens the program, it also makes the program simpler, easy to read, and easy to maintain.
The program uses two overloaded computeTax methods (lines 20, 43). The first computeTax method in line 20 computes the tax for the specified brackets and taxable income. The second computeTax method in line 43 computes the tax for the specified status and taxable income.