Create an application that computes the amount of Federal income tax that a person must pay, depending upon that person’s sal- ary. [Note: The actual U.S. Federal income tax rates vary based on many factors. For more information, see the information in IRS Form 1040-ES, which is located at www.irs.gov/ pub/irs-pdf/f1040e03.pdf.] Your application should operate as shown in Fig. 11.21. Assume all salaries are input as integers that are greater than or equal to 0. Use the following income ranges and corresponding tax rates:
Under $25,000 = 2% income tax
$25,000 – $49,999 = 5% income tax
$50,000 – $74,999 = 10% income tax
$75,000 – $99,999 = 15% income tax
$100,000 and over = 20% income tax
a) Copying the template to your working directory. Copy the directory C:Examples Tutorial11ExercisesIncomeTaxCalculator to your C:SimplyJava directory.
b) Opening the template file. Open the IncomeTaxCalculator.java file in your text editor.
c) Inserting a switch statement in the calculateJButtonActionPerformed method.
In the calculateJButtonActionPerformed method (lines 110–118 in the template),
insert a switch statement at line 114 to determine the tax rate and assign it to the double variable taxRate (declared on line 112). The int variable salary (line 113) contains the salary input by the user. Use the controlling expression salary / 25000 to determine the tax rate. If the salary is less than $25,000, the controlling expres- sion’s value will be 0. For s
```
1 // IncomeTaxCalculator.java
2 // Calculates a person's Federal income tax
3 // depending that person's salary.
4 import java.awt.*;
5 import java.awt.event.*;
6 import javax.swing.*;
7 import java.text.DecimalFormat;
8
9 public class IncomeTaxCalculator extends JFrame
10 {
11 // JLabel and JTextField for user to input yearly salary
12 private JLabel salaryJLabel;
13 private JTextField salaryJTextField;
14
15 // JLabel and JTextField to display income tax
16 private JLabel incomeTaxJLabel;
17 private JTextField incomeTaxJTextField;
18
19 // JButton to initiate tax calculation
20 private JButton calculateJButton;
21
22 // no-argument constructor
23 public IncomeTaxCalculator()
24 {
25 createUserInterface();
26 }
27
28 // create and position GUI components; register event handlers
29 private void createUserInterface()
30 {
31 // get content pane for attaching GUI components
32 Container contentPane = getContentPane();
33
34 // enable explicit positioning of GUI components
35 contentPane.setLayout( null );
36
37 // set up salaryJLabel
38 salaryJLabel = new JLabel();
39 salaryJLabel.setBounds( 20, 20, 100, 20 );
40 salaryJLabel.setText( "Yearly Salary:" );
41 contentPane.add( salaryJLabel );
42
43 // set up salaryJTextField
44 salaryJTextField = new JTextField();
45 salaryJTextField.setBounds( 140, 20, 90, 20 );
46 salaryJTextField.setHorizontalAlignment( JTextField.RIGHT );
47 contentPane.add( salaryJTextField );
48 salaryJTextField.addKeyListener(
49
50 new KeyAdapter() // anonymous inner class
51 {
52 // event handler called when key
53 // pressed in salaryJTextField
54 public void keyPressed( KeyEvent event )
55 {
56 salaryJTextFieldKeyPressed( event );
57 }
58
59 } // end anonymous inner class
60
61 ); // end call to addKeyListener
62
63 // set up incomeTaxJLabel
64 incomeTaxJLabel = new JLabel();
65 incomeTaxJLabel.setText( "Income Tax:" );
66 incomeTaxJLabel.setBounds( 20, 60, 100, 20 );
67 contentPane.add( incomeTaxJLabel );
68
69 // set up incomeTaxJTextField
70 incomeTaxJTextField = new JTextField();
71 incomeTaxJTextField.setBounds( 140, 60, 90, 20 );
72 incomeTaxJTextField.setHorizontalAlignment( JTextField.RIGHT );
73 incomeTaxJTextField.setEditable( false );
74 contentPane.add( incomeTaxJTextField );
75
76 // set up calculateJButton
77 calculateJButton = new JButton();
78 calculateJButton.setBounds( 140, 100, 90, 25 );
79 calculateJButton.setText( "Calculate" );
80 contentPane.add( calculateJButton );
81 calculateJButton.addActionListener(
82
83 new ActionListener() // anonymous inner class
84 {
85 // event handler called when calculateJButton is pressed
86 public void actionPerformed( ActionEvent event )
87 {
88 calculateJButtonActionPerformed( event );
89 }
90
91 } // end anonymous inner class
92
93 ); // end call to addActionListener
94
95 // set properties of application's window
96 setTitle( "Income Tax Calculator" ); // set window title
97 setSize( 255, 170 ); // set window size
98 setVisible( true ); // display window
99
100 } // end method createUserInterface
101
102 // clear incomeTaxJTextField when user types in salaryJTextField
103 private void salaryJTextFieldKeyPressed( KeyEvent event )
104 {
105 incomeTaxJTextField.setText( "" );
106
107 } // end method incomeTaxJTextFieldKeyPressed
108
109 // calculate income tax based on user input
110 private void calculateJButtonActionPerformed( ActionEvent event )
111 {
112 double taxRate; // stores the tax rate
113 int salary = Integer.parseInt( salaryJTextField.getText() );
114
115 switch ( salary / 25000 )
116 {
117 case 0: // yearly salary under $25,000
118 taxRate = 0.02; // 2% tax rate
119 break;
120
121 case 1: // yearly salary in range $25,000-49,999
122 taxRate = 0.05; // 5% tax rate
123 break;
124
125 case 2: // yearly salary in range $50,000-74,999
126 6taxRate = 0.1; // 10% tax rate
127 break;
128
129 case 3: // yearly salary in range $75,000-99,999
130 taxRate = 0.15; // 15% tax rate
131 break;
132
133 default: // yearly salary $100,000 or higher
134 taxRate = 0.2; // 20% tax rate
135
136 } // end switch statement
137
138 double incomeTax = salary * taxRate ; // calculate taxes
139
140 // display income tax
141 DecimalFormat dollars = new DecimalFormat( "$0.00" );
142 incomeTaxJTextField.setText( dollars.format( incomeTax ) );
143
144 } // end method calculateJButtonActionPerformed
145
146 // main method
147 public static void main( String[] args )
148 {
149 IncomeTaxCalculator application = new IncomeTaxCalculator();
150 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
151
152 } // end method main
153
154 } // end class IncomeTaxCalculator
```
You might also like to view...
The Magic Wand options bar has a(n) ____ box that allows you to enter a value that determines the similarity or difference in the color of the selected pixels.
a. Tolerance b. Resolution c. Density d. Indexing
Do you need to be in the Media view to add keyword tags to an image?
What will be an ideal response?
You have decided to use SNAT and PAT on your small office network. At minimum, how many IP addresses must you obtain from your ISP for all five clients in your office to be able to access servers on the Internet?
What will be an ideal response?
A(n) ____ is a method variable that stores an argument, so that the receiver of the message can access that argument.
A. message B. attribute C. parameter D. property