In this exercise, you will create a Salary Sur- vey application that is similar to the one you created in Exercise 16.12. This application includes an ArrayList that has already been added for you, to contain the salary ranges for use by an Iterator. Note that the salary ranges are for whole dollar amounts—the amount of cents in the user’s salary is truncated before the salary is placed in a specific range. You will use the Iterator with a while statement to replace the for statement that is used in Tutorial 16 (Fig. 19.56).
a) Copying the template to your working directory. Copy the C:Examples Tutorial19ExercisesModifiedSalarySurvey directory to your C:SimplyJava directory.
b) Opening the template file. Open the SalarySurvey.java file in your text editor.
c) Clearing the JTextArea from previous output. Find the showTotalsJButtonAc- tionPerformed method, which begins at line 174. Inside the showTotalsJBut- tonActionPerformed method, clear the Survey results: JTextArea’s text.
d) Adding a header to the output. The first portion of your application’s output is the header. Use method append to add the header as a String to the Survey results: JTextArea (surveyResultsJTextArea).
e) Creating a counter variable. ArrayList rangesArrayList contains each range as a String. In the next step, you will create an Iterator to cycle through each element in this ArrayList. However, you will still need to use a counter to access the number of results for the current range. Declare counter variable i and
```
1 // SalarySurvey.java
2 // Application that takes information about employee salaries and
3 // uses an array to keep track of the number of employees in each
4 // salary range.
5 import java.awt.*;
6 import java.awt.event.*;
7 import java.text.DecimalFormat;
8 import java.util.*;
9 import javax.swing.*;
10
11 public class SalarySurvey extends JFrame
12 {
13 // JLabel and JTextField for sales input
14 private JLabel enterSalesJLabel;
15 private JTextField enterSalesJTextField;
16
17 // JButton to initiate calculation of salary
18 private JButton calculateJButton;
19
20 // JLabel and JTextField for salary output
21 private JLabel totalSalaryJLabel;
22 private JTextField totalSalaryJTextField;
23
24 // JButton to display salaries and totals
25 private JButton showTotalsJButton;
26
27 // JLabel and JList for salaries and totals
28 private JLabel surveyResultsJLabel;
29 private JTextArea surveyResultsJTextArea;
30
31 // String[] and ArrayList to hold salaries
32 private String[] salaryRangesArray = { "$200 - $299",
33 "$300 - $399", "$400 - $499", "$500 - $599",
34 "$600 - $699", "$700 - $799", "$800 - $899",
35 "$900 - $999", "$1000 +" };
36 private ArrayList rangesArrayList = new ArrayList();
37
38 // int array to hold totals
39 int[] resultArray = new int[ 11 ];
40
41 // DecimalFormat to format output
42 DecimalFormat dollars = new DecimalFormat( "$0.00" );
43
44 // no-argument constructor
45 public SalarySurvey()
46 {
47 createUserInterface();
48 }
49
50 // create and position GUI components; register event handlers
51 private void createUserInterface()
52 {
53 // get content pane for attaching GUI components
54 Container contentPane = getContentPane();
55
56 // enable explicit positioning of GUI components
57 contentPane.setLayout( null );
58
59 // set up enterSalesJLabel
60 enterSalesJLabel = new JLabel();
61 enterSalesJLabel.setBounds( 20, 20, 80, 20);
62 enterSalesJLabel.setText( "Enter sales:" );
63 contentPane.add( enterSalesJLabel );
64
65 // set up enterSalesJTextField
66 enterSalesJTextField = new JTextField();
67 enterSalesJTextField.setBounds( 120, 20, 70, 20 );
68 enterSalesJTextField.setHorizontalAlignment(
69 JTextField.RIGHT );
70 contentPane.add( enterSalesJTextField );
71
72 // set up calculateJButton
73 calculateJButton = new JButton();
74 calculateJButton.setBounds( 55, 60, 110, 20 );
75 calculateJButton.setText( "Calculate" );
76 contentPane.add( calculateJButton );
77 calculateJButton.addActionListener(
78
79 new ActionListener() // anonymous inner class
80 {
81 // event handler called when calculateJButton is pressed
82 public void actionPerformed( ActionEvent event )
83 {
84 calculateJButtonActionPerformed( event );
85 }
86
87 } // end anonymous inner class
88
89 ); // end call to addActionListener
90
91 // set up totalSalaryJLabel
92 totalSalaryJLabel = new JLabel();
93 totalSalaryJLabel.setBounds( 20, 100, 80, 20 );
94 totalSalaryJLabel.setText( "Total salary:" );
95 contentPane.add( totalSalaryJLabel );
96
97 // set up totalSalaryJTextField
98 totalSalaryJTextField = new JTextField();
99 totalSalaryJTextField.setBounds( 120, 100, 70, 20 );
100 totalSalaryJTextField.setHorizontalAlignment(
101 JTextField.CENTER );
102 totalSalaryJTextField.setEditable( false );
103 contentPane.add( totalSalaryJTextField );
104
105 // set up showTotalsJButton
106 showTotalsJButton = new JButton();
107 showTotalsJButton.setBounds( 55, 140, 110, 20 );
108 showTotalsJButton.setText( "Show Totals" );
109 contentPane.add( showTotalsJButton );
110 showTotalsJButton.addActionListener(
111
112 new ActionListener() // anonymous inner class
113 {
114 // event handler called when showTotalsJButton is pressed
115 public void actionPerformed( ActionEvent event )
116 {
117 showTotalsJButtonActionPerformed( event );
118 }
119
120 } // end anonymous inner class
121
122 ); // end call to addActionListener
123
124 // set up surveyResultsJLabel
125 surveyResultsJLabel = new JLabel();
126 surveyResultsJLabel.setBounds( 20, 180, 100, 20 );
127 surveyResultsJLabel.setText( "Survey results:" );
128 contentPane.add( surveyResultsJLabel );
129
130 // set up surveyResultsJTextArea
131 surveyResultsJTextArea = new JTextArea();
132 surveyResultsJTextArea.setBounds( 20, 210, 170, 180 );
133 contentPane.add( surveyResultsJTextArea );
134
135 // add all salary ranges to rangesArrayList
136 for ( int counter = 0; counter < 9; counter++ )
137 {
138 rangesArrayList.add( salaryRangesArray[ counter ] );
139 }
140
141 // set properties of application's window
142 setTitle( "Salary Survey" ); // set title bar string
143 setSize( 220, 450 ); // set window size
144 setVisible( true ); // display window
145
146 } // end method createUserInterface
147
148 // calculate the total salary based on user input
149 private void calculateJButtonActionPerformed( ActionEvent event )
150 {
151 // get user input and calculate salary
152 int sales = Integer.parseInt( enterSalesJTextField.getText() );
153 double salary = 200 + .09 * sales;
154 int index = ( int ) salary / 100;
155
156 // increment salary counters
157 if ( index >= 10 )
158 {
159 // increment counters
160 resultArray[ 10 ]++;
161 }
162 else
163 {
164 // increment counter
165 resultArray[ index ]++;
166 }
167
168 // display salary
169 totalSalaryJTextField.setText( dollars.format( salary ) );
170
171 } // end calculateJButtonActionPerformed
172
173 // display range of salaries
174 private void showTotalsJButtonActionPerformed(
175 ActionEvent event )
176 {
177 // clear surveyResultsJTextArea
178 surveyResultsJTextArea.setText( "" );
179
180 // add header to surveyResultsJTextAr
181 surveyResultsJTextArea.append( "Salary Range: \tTotal:\n")
182
183 // counter variable
184 int i = 2;
185
186 // set iterator
187 Iterator rangeIterator = rangesArrayL
188
189 // for each salary, add output to Lis
190 while ( rangeIterator.hasNext() )
191 {
192 // set reference variable
193 String range = ( String ) rangeIte
194
195 // add output information to surveyResultsJTextArea
196 surveyResultsJTextArea.append( range + '\t'
197 + resultArray[ i ] + "\n" );
198 i++; // increment counter
199
200 } // end while loop
201
202 } // end showTotalsJButtonActionPerformed
203
204 //
205 public static void main( String args[] )
206 {
207 SalarySurvey application = new SalarySurvey();
208 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
209
210 } // end method main
211
212 } // end class SalarySurvey
```
You might also like to view...
________ functions enable linking or joining of functions or formulas
Fill in the blank(s) with correct word
Which technology must be used by a mechanic who wants to use a smartphone hands-free while driving?
A) Device pairing B) KVM switch C) Tethering D) NFC configuration