(Expanded Wage Calculator that Performs Tax Calculations) Develop an application that calculates an employee’s wages as shown in Fig. 6.32. The user enters the hourly wage and number of hours worked per week. When the Calculate JButton is clicked, the gross wages of the user should display in the Gross wages: JTextField. The Federal taxes: JTextField should display the amount deducted for Federal taxes, and the Net wages: JTextField should display the difference between the gross wages and the Federal tax amount. Assume overtime wages are 1.5 times the hourly wage and Federal withholding taxes are 15% of gross earnings.





a) Copying the template to your working directory. Copy the C:Examples Tutorial06ExercisesExpandedWageCalculator directory to your C:Simply- Java directory

b) Opening the template file. Open the WageCalculator.java file in your text editor.

c) Modifying the Calculate JButton’s ActionPerformed event handler. Add the code for Steps d and e to calculateJButtonActionPerformed (lines 190–229).

d) Calculating and displaying the Federal taxes deducted. After line 228, insert a state- ment that declares a constant double variable—TAX_RATE—and assigns it 0.15, which represents 15%. On the next line, insert a statement that declares double vari- able federalTaxes and assigns it the product of wages and TAX_RATE. The result is the amount that will be deducted for Federal taxes from the gross wages. Insert a statement that displays this value in federalTaxesJTextField, using method for- mat of DecimalFormat dollars that you created earlier (see Fig. 6.20 for the basic syntax of th


```
1 // WageCalculator.java
2 // This application inputs the hourly wage and number of hours
3 // worked for an employee, then calculates the employee's gross
4 // wages, federal taxes and net wages.
5 import java.awt.*;
6 import java.awt.event.*;
7 import javax.swing.*;
8 import java.text.*;
9
10 public class WageCalculator extends JFrame
11 {
12 // JLabel and JTextField for wage per hour
13 private JLabel hourlyWageJLabel;
14 private JTextField hourlyWageJTextField;
15
16 // JLabel and JTextField for hours worked in a week
17 private JLabel hoursWorkedJLabel;
18 private JTextField hoursWorkedJTextField;
19
20 // JLabel and JTextField for gross wages
21 private JLabel grossWagesJLabel;
22 private JTextField grossWagesJTextField;
23
24 // JLabel and JTextField for federal taxes
25 private JLabel federalTaxesJLabel;
26 private JTextField federalTaxesJTextField;
27
28 // JLabel and JTextField for net wages
29 private JLabel netWagesJLabel;
30 private JTextField netWagesJTextField;
31
32 // JButton to initiate wage calculation
33 private JButton calculateJButton;
34
35 // no-argument constructor
36 public WageCalculator()
37 {
38 createUserInterface();
39 }
40
41 // create and position GUI components; register event handlers
42 public void createUserInterface()
43 {
44 // get content pane for attaching GUI components
45 Container contentPane = getContentPane();
46
47 // enable explicit positioning of GUI components
48 contentPane.setLayout( null );
49
50 // set up hourlyWageJLabel
51 hourlyWageJLabel = new JLabel();
52 hourlyWageJLabel.setBounds( 16, 16, 90, 21 );
53 hourlyWageJLabel.setText( "Hourly wage:" );
54 contentPane.add( hourlyWageJLabel );
55
56 // set up hourlyWageJTextField
57 hourlyWageJTextField = new JTextField();
58 hourlyWageJTextField.setBounds( 120, 16, 90, 21 );
59 hourlyWageJTextField.setHorizontalAlignment(
60 JTextField.RIGHT );
61 contentPane.add( hourlyWageJTextField );
62 hourlyWageJTextField.addKeyListener(
63
64 new KeyAdapter() // anonymous inner class
65 {
66 // event handler called when user types
67 // in hourlyWageJTextField
68 public void keyPressed( KeyEvent event )
69 {
70 hourlyWageJTextFieldKeyPressed( event );
71 }
72
73 } // end anonymous inner class
74
75 ); // end call to addKeyListener
76
77 // set up hoursWorkedJLabel
78 hoursWorkedJLabel = new JLabel();
79 hoursWorkedJLabel.setBounds( 16, 56, 90, 21 );
80 hoursWorkedJLabel.setText( "Hours worked:" );
81 contentPane.add( hoursWorkedJLabel );
82
83 // set up hoursWorkedJTextField
84 hoursWorkedJTextField = new JTextField();
85 hoursWorkedJTextField.setBounds( 120, 56, 90, 21 );
86 hoursWorkedJTextField.setHorizontalAlignment(
87 JTextField.RIGHT );
88 contentPane.add( hoursWorkedJTextField );
89 hoursWorkedJTextField.addKeyListener(
90
91 new KeyAdapter() // anonymous inner class
92 {
93 // event handler called when user types
94 // in hoursWorkedJTextField
95 public void keyPressed( KeyEvent event )
96 {
97 hoursWorkedJTextFieldKeyPressed( event );
98 }
99
100 } // end anonymous inner class
101
102 ); // end call to addKeyListener
103
104 // set up grossWagesJLabel
105 grossWagesJLabel = new JLabel();
106 grossWagesJLabel.setBounds( 16, 96, 90, 21 );
107 grossWagesJLabel.setText( "Gross wages:" );
108 contentPane.add( grossWagesJLabel );
109
110 // set up grossWagesJTextField
111 grossWagesJTextField = new JTextField();
112 grossWagesJTextField.setBounds( 120, 96, 90, 21 );
113 grossWagesJTextField.setHorizontalAlignment(
114 JTextField.RIGHT );
115 grossWagesJTextField.setEditable( false );
116 contentPane.add( grossWagesJTextField );
117
118 // set up federalTaxesJLabel
119 federalTaxesJLabel = new JLabel();
120 federalTaxesJLabel.setBounds( 16, 136, 120, 21 );
121 federalTaxesJLabel.setText( "Federal taxes:" );
122 contentPane.add( federalTaxesJLabel );
123
124 // set up federalTaxesJTextField
125 federalTaxesJTextField = new JTextField();
126 federalTaxesJTextField.setBounds( 120, 136, 90, 21 );
127 federalTaxesJTextField.setHorizontalAlignment(
128 JTextField.RIGHT );
129 federalTaxesJTextField.setEditable( false );
130 contentPane.add( federalTaxesJTextField );
131
132 // set up netWagesJLabel
133 netWagesJLabel = new JLabel();
134 netWagesJLabel.setBounds( 16, 176, 90, 21 );
135 netWagesJLabel.setText( "Net wages:" );
136 contentPane.add( netWagesJLabel );
137
138 // set up netWagesJTextField
139 netWagesJTextField = new JTextField();
140 netWagesJTextField.setBounds( 120, 176, 90, 21 );
141 netWagesJTextField.setHorizontalAlignment( JTextField.RIGHT );
142 netWagesJTextField.setEditable( false );
143 contentPane.add( netWagesJTextField );
144
145 // set up calculateJButton
146 calculateJButton = new JButton();
147 calculateJButton.setBounds( 120, 216, 90, 24 );
148 calculateJButton.setText( "Calculate" );
149 contentPane.add( calculateJButton );
150 calculateJButton.addActionListener(
151
152 new ActionListener() // anonymous inner class
153 {
154 // method called when user clicks calculateJButton
155 public void actionPerformed ( ActionEvent event )
156 {
157 calculateJButtonActionPerformed( event );
158 }
159
160 } // end anonymous inner class
161
162 ); // end call to addActionListener
163
164 // set properties of application’s window
165 setTitle( "Wage Calculator" ); // set title bar text
166 setSize( 230, 280 ); // set window size
167 setVisible( true ); // display window
168
169 } // end method createUserInterface
170
171 // method called when uses types in hourlyWageJTextField
172 private void hourlyWageJTextFieldKeyPressed( KeyEvent event )
173 {
174 // clear output JTextFields when key pressed
175 grossWagesJTextField.setText( "" );
176 federalTaxesJTextField.setText( "" );
177 netWagesJTextField.setText( "" );
178
179 } // end method hourlyWageJTextFieldKeyPressed
180
181 // method called when uses types in hoursWorkedJTextField
182 private void hoursWorkedJTextFieldKeyPressed( KeyEvent event )
183 {
184 // clear output JTextFields when key pressed
185 grossWagesJTextField.setText( "" );
186 federalTaxesJTextField.setText( "" );
187 netWagesJTextField.setText( "" );
188
189 } // end method hoursWorkedJTextFieldKeyPressed
190
191 // method called when user presses calculateJButton
192 private void calculateJButtonActionPerformed( ActionEvent event )
193 {
194 // get hourly wage
195 double hourlyWage =
196 Double.parseDouble( hourlyWageJTextField.getText() );
197
198 // get number of hours worked this week
199 double hoursWorked =
200 Double.parseDouble( hoursWorkedJTextField.getText() );
201
202 // constant for maximum hours employee can
203 // work before being paid for overtime
204 final double HOUR_LIMIT = 40.0;
205
206 // gross wages for week; calculated in if...else statement
207 double wages;
208
209 // determine gross wages
210 if ( hoursWorked <= HOUR_LIMIT )
211 {
212 // regular wages for HOUR_LIMIT (40) hours or less
213 wages = ( hoursWorked * hourlyWage );
214 }
215 else // worked more than HOUR_LIMIT (40) hours
216 {
217 // wage for first HOUR_LIMIT (40) hours
218 wages = HOUR_LIMIT * hourlyWage;
219
220 // add time-and-a-half for hours above HOUR_LIMIT (40)
221 wages +=
222 ( hoursWorked - HOUR_LIMIT ) * ( 1.5 * hourlyWage );
223 }
224
225 // specify output format
226 DecimalFormat dollars = new DecimalFormat( "$0.00" );
227
228 // display gross wages
229 grossWagesJTextField.setText( dollars.format( wages ) );
230
231 // calculate federal taxes
232 final double TAX_RATE = 0.15; // 15% tax rate double federalTaxes = wages * TAX_RATE;
233
234 // display federal taxes federalTaxesJTextField.setText(
235 dollars.format( federalTaxes ) );
236
237 // calculate and display net wages netWagesJTextField.setText(
238 dollars.format( wages - federalTaxes ) );
239
240 // calculate and display net wages
241 netWagesJTextField.setText(
242 dollars.format( wages - federalTaxes ) );
243 } // end method calculateJButtonActionPerformed
244
245 // main method
246 public static void main( String args[] )
247 {
248 WageCalculator application = new WageCalculator();
249 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
250
251 } // end method main
252
253 } // end class WageCalculator
```

Computer Science & Information Technology

You might also like to view...

Regardless of the number of fields used for row headings, at least ________ fields must remain available to complete the crosstab query

Fill in the blank(s) with correct word

Computer Science & Information Technology

Case-Based Critical Thinking Question ? Tyler has been helping his friend Wes learn the website design business. Wes is concerned that the article of the history of his business will extend beyond its container. Tyler tells him about _____ , which Wes can use to give the appearance that the article fits in its container.

A. display: all B. overflow: auto C. clear: bottom D. height: auto

Computer Science & Information Technology

The ____ format is a compressed graphic format designed to minimize file size and electronic transfer time.

A. TIFF B. EPS C. GIF D. HTML

Computer Science & Information Technology

 ____ is the default browser for the iOS platform.

A. Safari B. Internet Explorer C. Chrome D. Navigator

Computer Science & Information Technology