Develop an application that computes the amount of money an employee makes each year over a user-specified number of years. Assume the employee receives a weekly wage of $500 and a pay raise once every year. The user specifies in the application the amount of the raise (in percent per year) and the number of years for which the amounts earned will be calculated. The application should run as shown in Fig. 10.32.
a) Copying the template to your working directory. Copy the C:Exam- plesTutorial10ExercisesPayRaise directory to your C:SimplyJava direc- tory.
b) Opening the template file. Open the PayRaise.java file in your text editor.
c) Customizing the Amount of raise (in %): JSpinner. You must customize this JSpin- ner to display the pay raise percentage. The name of this JSpinner is raiseJSpin- ner. The user should only be able to specify percentages in the range of 3%–8%. Modify line 53 so that raiseJSpinner’s initial value is 3, its minimum value is 3, its maximum value is 8 and its step size is 1. In line 54, insert code to set the bounds property to 170, 25, 70, 22.
d) Customizing the Years: JSpinner. You must customize this JSpinner to display the number of years in the range 1–50. The name of this JSpinner is yearsJSpinner. Modify line 65 so that yearsJSpinner’s initial value is 1, its minimum value is 1, its maximum value is 50 and its step size is 1. In l
```
1 // PayRaise.java
2 // Application that calculates how much money should be invested in
3 // order to achieve a certain financial goal.
4 import java.awt.*;
5 import java.awt.event.*;
6 import javax.swing.*;
7 import java.text.*;
8
9 public class PayRaise extends JFrame
10 {
11 // JLabel and JSpinner for yearly raise
12 private JLabel raiseJLabel;
13 private JSpinner raiseJSpinner;
14
15 // JLabel and JSpinner for number of years
16 private JLabel yearsJLabel;
17 private JSpinner yearsJSpinner;
18
19 // JLabel and JTextArea for amount earned
20 // after a certain number of years
21 private JLabel amountEarnedJLabel;
22 private JTextArea amountEarnedJTextArea;
23
24 // JScrollPane adds scrollbars to amountEarnedJTextArea
25 private JScrollPane amountEarnedJScrollPane;
26
27 // JButton calculates amount earned
28 private JButton calculateJButton;
29
30 // no-argument constructor
31 public PayRaise()
32 {
33 createUserInterface();
34 }
35
36 // create and position GUI components; register event handlers
37 private void createUserInterface()
38 {
39 // get content pane for attaching GUI components
40 Container contentPane = getContentPane();
41
42 // enable explicit positioning of GUI components
43 contentPane.setLayout( null );
44
45 // set up raiseJLabel
46 raiseJLabel = new JLabel();
47 raiseJLabel.setBounds( 20, 25, 150, 20 );
48 raiseJLabel.setText( "Amount of raise (in %):" );
49 contentPane.add( raiseJLabel );
50
51 // set up raiseJSpinner
52 raiseJSpinner = new JSpinner(
53 new SpinnerNumberModel( 3, 3, 8, 1 ) );
54 raiseJSpinner.setBounds( 170, 25, 70, 22 );
55 contentPane.add( raiseJSpinner );
56
57 // set up yearsJLabel
58 yearsJLabel = new JLabel();
59 yearsJLabel.setBounds( 20, 60, 80, 20 );
60 yearsJLabel.setText( "Years:" );
61 contentPane.add( yearsJLabel );
62
63 // set up yearsJSpinner
64 yearsJSpinner = new JSpinner(
65 new SpinnerNumberModel( 1, 1, 50, 1 ) );
66 yearsJSpinner.setBounds( 170, 60, 70, 22 );
67 contentPane.add( yearsJSpinner );
68
69 // set up amountEarnedJLabel
70 amountEarnedJLabel = new JLabel();
71 amountEarnedJLabel.setBounds( 20, 95, 150, 20 );
72 amountEarnedJLabel.setText( "Yearly amount earned:" );
73 contentPane.add( amountEarnedJLabel );
74
75 // set up amountEarnedJTextArea
76 amountEarnedJTextArea = new JTextArea();
77 amountEarnedJTextArea.setEditable( false );
78
79 // set up amountEarnedJScrollPane
80 amountEarnedJScrollPane = new JScrollPane(
81 amountEarnedJTextArea );
82 amountEarnedJScrollPane.setBounds( 20, 120, 330, 115 );
83 contentPane.add( amountEarnedJScrollPane );
84
85 // set up calculateJButton
86 calculateJButton = new JButton();
87 calculateJButton.setBounds( 260, 25, 90, 20 );
88 calculateJButton.setText( "Calculate" );
89 contentPane.add( calculateJButton );
90 calculateJButton.addActionListener(
91
92 new ActionListener() // anonymous inner class
93 {
94 // event handler called when calculateJButton is pressed
95 public void actionPerformed( ActionEvent event )
96 {
97 calculateJButtonActionPerformed( event );
98 }
99
100 } // end anonymous inner class
101
102 ); // end call to addActionListener
103
104 // set properties of application’s window
105 setTitle( "Pay Raise Calculator" ); // set title bar text
106 setSize( 380, 280 ); // set window size
107 setVisible( true ); // display window
108
109 } // end method createUserInterface
110
111 // calculate and display amounts
112 private void calculateJButtonActionPerformed( ActionEvent event )
113 {
114 // store weekly starting wage
115 double wage = 500;
116
117 Integer integerRaiseObject =
118 ( Integer ) raiseJSpinner.getValue();
119 int rate = integerRaiseObject.intValue();
120
121 Integer integerYearsObject =
122 ( Integer ) yearsJSpinner.getValue();
123 int years = integerYearsObject.intValue();
124
125 amountEarnedJTextArea.setText( "Year\tAmount" );
126 DecimalFormat dollars = new DecimalFormat( "$0.00" );
127
128 // for loop calculates total
129 for ( int counter = 1; counter <= years; counter++ )
130 {
131 double amount = wage * 52;
132 amountEarnedJTextArea.append(
133 "\n" + counter + "\t" + dollars.format( amount ) );
134
135 wage *= ( 1 + ( ( double ) rate / 100 ) );
136
137 } // end for
138
139 } // end method calculateJButtonActionPerformed
140
141 // main method
142 public static void main( String[] args )
143 {
144 PayRaise application = new PayRaise();
145 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
146
147 } // end method main
148
149 } // end class PayRaise
```
You might also like to view...
A comparison operator compares two values and returns either TRUE or FALSE
Indicate whether the statement is true or false
Common uses of ActiveX controls are ________
A) Starting your computer B) Disabling content C) Testing a procedure D) Command buttons, lists, and dialog boxes