Modify the application you devel- oped in Exercise 11.17 to calculate the yearly salary every time one of the JSpinner’s values is changed. The user specifies the amount of the annual raise (as a percentage) and the num- bers of years for which the raise applies in the application. The application should run as shown in Fig. 13.26.



a) Copying the template to your working directory. Copy the C:Examples Tutorial13ExercisesPayRaise2 directory to your C:SimplyJava directory.

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

c) Handling the stateChanged event for raiseJSpinner. Add code to the state- Changed event handler for raiseJSpinner to call method raiseJSpinnerState- Changed. This method must then be declared and it should call calculateWages.

d) Handling the stateChanged event for yearsJSpinner. Add code to the state- Changed event handler for yearsJSpinner to call method yearsJSpinnerState- Changed. This method must then be declared and it should call calculateWages.

e) Saving the application. Save your modified source code file.

f) Opening the Command Prompt window and changing directories. Open the Com- mand Prompt window by selecting Start > Programs > Accessories > Command Prompt. Change to your working directory by typing cd


```
1 // PayRaise.java
2 // Application that calculates yearly salaries given an annual
3 // raise percentage and number of years.
4 import java.text.*;
5 import java.awt.*;
6 import java.awt.event.*;
7 import javax.swing.*;
8 import javax.swing.event.*;
9
10 public class PayRaise extends JFrame
11 {
12 // JLabel and JSpinner for yearly raise
13 private JLabel raiseJLabel;
14 private JSpinner raiseJSpinner;
15
16 // JLabel and JSpinner for number of years
17 private JLabel yearsJLabel;
18 private JSpinner yearsJSpinner;
19
20 // JLabel and JTextArea for amount earned
21 // after a certain number of years
22 private JLabel amountEarnedJLabel;
23 private JTextArea amountEarnedJTextArea;
24
25 // JScrollPane add scrollbars to amountEarnedJTextArea
26 private JScrollPane amountEarnedJScrollPane;
27
28 // no-argument constructor
29 public PayRaise()
30 {
31 createUserInterface();
32
33 // display initial yearly wage in JTextArea
34 calculateWages();
35 }
36
37 // create and position GUI components; register event handlers
38 private void createUserInterface()
39 {
40 // get content pane for attaching GUI components
41 Container contentPane = getContentPane();
42
43 // enable explicit positioning of GUI components
44 contentPane.setLayout( null );
45
46 // set up raiseJLabel
47 raiseJLabel = new JLabel();
48 raiseJLabel.setBounds( 20, 25, 150, 20 );
49 raiseJLabel.setText( "Amount of raise (in %):" );
50 contentPane.add( raiseJLabel);
51
52 // set up raiseJSpinner
53 raiseJSpinner = new JSpinner(
54 new SpinnerNumberModel( 3, 3, 8, 1 ) );
55 raiseJSpinner.setBounds( 170, 25, 70, 22 );
56 contentPane.add( raiseJSpinner );
57 raiseJSpinner.addChangeListener(
58
59 new ChangeListener() // anonymous inner class
60 {
61 // event handler called when raiseJSpinner is changed
62 public void stateChanged( ChangeEvent event )
63 {
64
65 }
66 raiseJSpinnerStateChanged( event );
67 } // end anonymous inner class
68
69 ); // end call to addChangeListener
70
71 // set up yearsJLabel
72 yearsJLabel = new JLabel();
73 yearsJLabel.setBounds( 20, 60, 80, 20 );
74 yearsJLabel.setText( "Years:" );
75 contentPane.add( yearsJLabel );
76
77 // set up yearsJSpinner
78 yearsJSpinner = new JSpinner(
79 new SpinnerNumberModel( 1, 1, 50, 1 ) );
80 yearsJSpinner.setBounds( 170, 60, 70, 22 );
81 contentPane.add( yearsJSpinner );
82 yearsJSpinner.addChangeListener(
83
84 new ChangeListener() // anonymous inner class
85 {
86 // event handler called when yearsJSpinner is changed
87 public void stateChanged( ChangeEvent event )
88 {
89
90 }
91 yearsJSpinnerStateChanged( event );
92 } // end anonymous inner class
93
94 ); // end call to addChangeListener
95
96 // set up amountEarnedJLabel
97 amountEarnedJLabel = new JLabel();
98 amountEarnedJLabel.setBounds( 20, 95, 150, 20 );
99 amountEarnedJLabel.setText( "Yearly amount earned:" );
100 contentPane.add( amountEarnedJLabel );
101
102 // set up amountEarnedJTextArea
103 amountEarnedJTextArea = new JTextArea();
104 amountEarnedJTextArea.setEditable( false );
105
106 // set up amountEarnedJScrollPane
107 amountEarnedJScrollPane =
108 new JScrollPane( amountEarnedJTextArea );
109 amountEarnedJScrollPane.setBounds( 20, 120, 330, 115 );
110 contentPane.add( amountEarnedJScrollPane );
111
112 // set properties of application's window
113 setTitle( "Pay Raise Calculator" ); // set title-bar string
114 setSize( 380, 220 ); // set window size
115 setVisible( true ); // show window
116
117 } // end method createUserInterface
118
119 // calculate and display yearly wages
120 private void calculateWages()
121 {
122 // store weekly starting wage
123 double wage = 500;
124
125 Integer integerRaiseObject =
126 ( Integer ) raiseJSpinner.getValue();
127 int rate = integerRaiseObject.intValue();
128
129 Integer integerYearsObject =
130 ( Integer ) yearsJSpinner.getValue();
131 int years = integerYearsObject.intValue();
132
133 amountEarnedJTextArea.setText( "Year\tAmount" );
134 DecimalFormat dollars = new DecimalFormat( "$0.00" );
135
136 // for loop calculates total
137 for ( int counter = 1; counter <= years; counter++ )
138 {
139 double amount = wage * 52;
140 amountEarnedJTextArea.append(
141 "\n" + counter + "\t" + dollars.format( amount ) );
142
143 wage *= ( 1 + ( ( double ) rate / 100 ) );
144
145 } // end for
146
147 } // end method calculateWages
148
149 // update the JTextArea
150 private void raiseJSpinnerStateChanged( ChangeEvent event )
151 {
152 // call method calculateWages to perform calculations
153 calculateWages();
154
155 } // end method raiseJSpinnerStateChanged
156
157 // update the JTextArea
158 private void yearsJSpinnerStateChanged( ChangeEvent event )
159 {
160 // call method calculateWages to perform calculations
161 calculateWages();
162
163 } // end method yearsJSpinnerStateChanged
164
165 // main method
166 public static void main( String args[] )
167 {
168 PayRaise application = new PayRaise();
169 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
170
171 } // end method main
172
173 } // end class PayRaise
```

Computer Science & Information Technology

You might also like to view...

What history expansion expression (i.e., event designator) will you use to invoke the last command that you executed? What event designator will you use to invoke the last make command that you executed?

What will be an ideal response?

Computer Science & Information Technology

Both instructions and data in a digital computer are represented as binary digits.

Answer the following statement true (T) or false (F)

Computer Science & Information Technology

When using a selection tool, you can unintentionally miss some of the pixels; this can create a ____.

A. pixel ghost B. ghost shadow C. fringe pixel D. fringe ghost

Computer Science & Information Technology

The ____ option prevents painting tool modifications of a layer.

A. Lock image pixels B. Lock transparent pixels C. Lock position D. Lock all

Computer Science & Information Technology