Create a Cash Register application (Fig. 11.23) with a numeric keypad similar to that in the Security Panel application you built in this tutorial. In addition to numbers, the cash register includes a decimal point JButton. Also, there are Enter, Total, Delete and Clear JButtons. These JButtons add an amount to the subtotal, cal- culate the tax and total, delete the current amount in the $ JTextField (in case the user makes an error during input) and clear the current amounts displayed, respectively. You will implement the methods for the Enter and Total JButtons. Assume the user enters positive, non-zero amounts. Sales tax should be calculated on the amount purchased using a switch statement. Add the tax amount to the subtotal to calculate the total. Display the
Amounts under $100 = 5% (.05) sales tax
Amounts between $100 and $499 = 7.5% (.075) sales tax
Amounts $500 and over = 10% (.10) sales tax
a) Copying the template to your working directory. Copy the directory C:Examples Tutorial11ExercisesCashRegister to your C:SimplyJava directory.
b) Opening the template file. Open the CashRegister.java file in your text editor.
c) Adding code to the enterJButtonActionPerformed method. In the enter- JButtonActionPerformed method (lines 496–499 in the template code), insert a statement that converts the value in amountJTextField to type double and adds the value to instance variable subtotal. Using the DecimalFormat variable dollars (created in line 56), insert a statement that formats the new value of subtotal and
displays it in subtotalJTextField. Insert a statement that clears the amountJText- Field (so the user can enter the next amount).
d) Determining the tax rate. In the totalJButtonActionPerformed meth
```
1 // CashRegister.java
2 // Application simulates the behavior of a cash register, allowing
3 // the user to input prices, keep a running subtotal, and
4 // calculate the sales tax and total.
5 import java.awt.*;
6 import java.awt.event.*;
7 import javax.swing.*;
8 import java.text.*;
9
10 public class CashRegister extends JFrame
11 {
12 double subtotal = 0.0; // stores subtotal of items purchased
13
14 // JLabel and JTextField to enter amount
15 private JLabel amountJLabel;
16 private JTextField amountJTextField;
17
18 // JButtons to enter numbers in amountJTextField
19 private JButton oneJButton;
20 private JButton twoJButton;
21 private JButton threeJButton;
22 private JButton fourJButton;
23 private JButton fiveJButton;
24 private JButton sixJButton;
25 private JButton sevenJButton;
26 private JButton eightJButton;
27 private JButton nineJButton;
28 private JButton zeroJButton;
29 private JButton pointJButton;
30
31 // JButton to add value in amountJTextField to subtotal
32 private JButton enterJButton;
33
34 // JButton to determine tax and calculate final total
35 private JButton totalJButton;
36
37 // JButton to delete value displayed in amountJTextField
38 private JButton deleteJButton;
39
40 // JButton to clear results
41 private JButton clearJButton;
42
43 // JLabel and JTextField to display subtotal
44 private JLabel subtotalJLabel;
45 private JTextField subtotalJTextField;
46
47 // JLabel and JTextField to display tax
48 private JLabel taxJLabel;
49 private JTextField taxJTextField;
50
51 // JLabel and JTextField to display final total
52 private JLabel totalJLabel;
53 private JTextField totalJTextField;
54
55 // DecimalFormat to format dollar amounts
56 private DecimalFormat dollars = new DecimalFormat( "$0.00" );
57
58 // no-argument constructor
59 public CashRegister()
60 {
61 createUserInterface();
62 }
63
64 // create and position GUI components; register event handlers
65 private void createUserInterface()
66 {
67 // get content pane for attaching GUI components
68 Container contentPane = getContentPane();
69
70 // enable explicit positioning of GUI components
71 contentPane.setLayout( null );
72
73 // set up amountJLabel
74 amountJLabel = new JLabel();
75 amountJLabel.setBounds( 20, 20, 15, 20 );
76 amountJLabel.setText( "$" );
77 contentPane.add( amountJLabel );
78
79 // set up amountJTextField
80 amountJTextField = new JTextField();
81 amountJTextField.setBounds( 40, 20, 260, 20 );
82 contentPane.add( amountJTextField );
83
84 // set up oneJButton
85 oneJButton = new JButton();
86 oneJButton.setBounds( 55, 70, 45, 20 );
87 oneJButton.setText( "1" );
88 contentPane.add( oneJButton );
89 oneJButton.addActionListener(
90
91 new ActionListener() // anonymous inner class
92 {
93 // event handler called when oneJButton is pressed
94 public void actionPerformed( ActionEvent event )
95 {
96 oneJButtonActionPerformed( event );
97 }
98
99 } // end anonymous inner class
100
101 ); // end call to addActionListener
102
103 // set up twoJButton
104 twoJButton = new JButton();
105 twoJButton.setBounds( 100, 70, 45, 20 );
106 twoJButton.setText( "2" );
107 contentPane.add( twoJButton );
108 twoJButton.addActionListener(
109
110 new ActionListener() // anonymous inner class
111 {
112 // event handler called when twoJButton is pressed
113 public void actionPerformed( ActionEvent event )
114 {
115 twoJButtonActionPerformed( event );
116 }
117
118 } // end anonymous inner class
119
120 ); // end call to addActionListener
121
122 // set up threeJButton
123 threeJButton = new JButton();
124 threeJButton.setBounds( 145, 70, 45, 20 );
125 threeJButton.setText( "3" );
126 contentPane.add( threeJButton );
127 threeJButton.addActionListener(
128
129 new ActionListener() // anonymous inner class
130 {
131 // event handler called when threeJButton is pressed
132 public void actionPerformed( ActionEvent event )
133 {
134 threeJButtonActionPerformed( event );
135 }
136
137 } // end anonymous inner class
138
139 ); // end call to addActionListener
140
141 // set up fourJButton
142 fourJButton = new JButton();
143 fourJButton.setBounds( 55, 90, 45, 20 );
144 fourJButton.setText( "4" );
145 contentPane.add( fourJButton );
146 fourJButton.addActionListener(
147
148 new ActionListener() // anonymous inner class
149 {
150 // event handler called when fourJButton is pressed
151 public void actionPerformed( ActionEvent event )
152 {
153 fourJButtonActionPerformed( event );
154 }
155
156 } // end anonymous inner class
157
158 ); // end call to addActionListener
159
160 // set up fiveJButton
161 fiveJButton = new JButton();
162 fiveJButton.setBounds( 100, 90, 45, 20 );
163 fiveJButton.setText( "5" );
164 contentPane.add( fiveJButton );
165 fiveJButton.addActionListener(
166
167 new ActionListener() // anonymous inner class
168 {
169 // event handler called when fiveJButton is pressed
170 public void actionPerformed( ActionEvent event )
171 {
172 fiveJButtonActionPerformed( event );
173 }
174
175 } // end anonymous inner class
176
177 ); // end call to addActionListener
178
179 // set up sixJButton
180 sixJButton = new JButton();
181 sixJButton.setBounds( 145, 90, 45, 20 );
182 sixJButton.setText( "6" );
183 contentPane.add( sixJButton );
184 sixJButton.addActionListener(
185
186 new ActionListener() // anonymous inner class
187 {
188 // event handler called when sixJButton is pressed
189 public void actionPerformed( ActionEvent event )
190 {
191 sixJButtonActionPerformed( event );
192 }
193
194 } // end anonymous inner class
195
196 ); // end call to addActionListener
197
198 // set up sevenJButton
199 sevenJButton = new JButton();
200 sevenJButton.setBounds( 55, 110, 45, 20 );
201 sevenJButton.setText( "7" );
202 contentPane.add( sevenJButton );
203 sevenJButton.addActionListener(
204
205 new ActionListener() // anonymous inner class
206 {
207 // event handler called when sevenJButton is pressed
208 public void actionPerformed( ActionEvent event )
209 {
210 sevenJButtonActionPerformed( event );
211 }
212
213 } // end anonymous inner class
214
215 ); // end call to addActionListener
216
217 // set up eightJButton
218 eightJButton = new JButton();
219 eightJButton.setBounds( 100, 110, 45, 20 );
220 eightJButton.setText( "8" );
221 contentPane.add( eightJButton );
222 eightJButton.addActionListener(
223
224 new ActionListener() // anonymous inner class
225 {
226 // event handler called when eightJButton is pressed
227 public void actionPerformed( ActionEvent event )
228 {
229 eightJButtonActionPerformed( event );
230 }
231
232 } // end anonymous inner class
233
234 ); // end call to addActionListener
235
236 // set up nineJButton
237 nineJButton = new JButton();
238 nineJButton.setBounds( 145, 110, 45, 20 );
239 nineJButton.setText( "9" );
240 contentPane.add( nineJButton );
241 nineJButton.addActionListener(
242
243 new ActionListener() // anonymous inner class
244 {
245 // event handler called when nineJButton is pressed
246 public void actionPerformed( ActionEvent event )
247 {
248 nineJButtonActionPerformed( event );
249 }
250
251 } // end anonymous inner class
252
253 ); // end call to addActionListener
254
255 // set up zeroJButton
256 zeroJButton = new JButton();
257 zeroJButton.setBounds( 100, 130, 45, 20 );
258 zeroJButton.setText( "0" );
259 contentPane.add( zeroJButton );
260 zeroJButton.addActionListener(
261
262 new ActionListener() // anonymous inner class
263 {
264 // event handler called when zeroJButton is pressed
265 public vo
You might also like to view...
Events which can travel up or down the container hierarchy are called ______.
a) expandable events b) sourced events c) bubble events d) routed events
Repeat Problem 11 for the directed Laplacian based DSP G framework.
What will be an ideal response?
A ____ is a predefined procedure that performs a specific task and then returns a value after completing the task.
A. sub B. function C. method D. routine
A server that distributes traffic across two or more interfaces is utilizing which choice below?
a. true b. false