Enhance the Interest Calculator application you built in this tutorial with error checking. Test whether the user has entered valid values for the principal and interest rate. If the user enters an invalid value, dis- play a message in a message dialog. Figure 10.30 demonstrates the application handling invalid input.
a) Copying the template to your working directory. Copy the C:Exam- plesTutorial10ExercisesInterestCalculatorEnhanced directory to your C:SimplyJava directory.
b) Opening the template file. Open the InterestCalculator.java file in your text editor.
c) Customizing the calculateJButtonActionPerformed method to handle invalid input. In line 143, enter a condition into the if statement that returns true when the principal or rate are negative, or when the rate is over 10.
d) Displaying the error message. In lines 145–147, display the message dialog shown in
Fig. 10.30. Use three lines for clarity.
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 C:SimplyJavaInterest- CalculatorEnhanced.
g) Compiling the application. Compile your
```
1 // InterestCalculator.java
2 // Calculate the total value of an investment.
3 import java.awt.*;
4 import java.awt.event.*;
5 import javax.swing.*;
6 import java.text.*;
7
8 public class InterestCalculator extends JFrame
9 {
10 // JLabel and JTextField for principal invested
11 private JLabel principalJLabel;
12 private JTextField principalJTextField;
13
14 // JLabel and JTextField for interest rate
15 private JLabel interestRateJLabel;
16 private JTextField interestRateJTextField;
17
18 // JLabel and JTextField for the number of years
19 private JLabel yearsJLabel;
20 private JSpinner yearsJSpinner;
21
22 // JLabel and JTextArea display amount on deposit at
23 // the end of each year up to number of years entered
24 private JLabel yearlyBalanceJLabel;
25 private JTextArea yearlyBalanceJTextArea;
26
27 // JScrollPane adds scrollbars to JTextArea for lengthy output
28 private JScrollPane yearlyBalanceJScrollPane;
29
30 // JButton calculates amount on deposit at the
31 // end of each year up to number of years entered
32 private JButton calculateJButton;
33
34 // no-argument constructor
35 public InterestCalculator()
36 {
37 createUserInterface();
38 }
39
40 // create and position GUI components; register event handlers
41 private void createUserInterface()
42 {
43 // get content pane for attaching GUI components
44 Container contentPane = getContentPane();
45
46 // enable explicit positioning of GUI components
47 contentPane.setLayout( null );
48
49 // set up principalJLabel
50 principalJLabel = new JLabel();
51 principalJLabel.setBounds( 16, 16, 56, 24 );
52 principalJLabel.setText( "Principal:" );
53 contentPane.add( principalJLabel );
54
55 // set up principalJTextField
56 principalJTextField = new JTextField();
57 principalJTextField.setBounds( 100, 16, 100, 24 );
58 principalJTextField.setHorizontalAlignment(
59 JTextField.RIGHT );
60 contentPane.add( principalJTextField );
61
62 // set up interestRateJLabel
63 interestRateJLabel = new JLabel();
64 interestRateJLabel.setBounds( 16, 56, 80, 24 );
65 interestRateJLabel.setText( "Interest rate:" );
66 contentPane.add( interestRateJLabel );
67
68 // set up interestRateJTextField
69 interestRateJTextField = new JTextField();
70 interestRateJTextField.setBounds( 100, 56, 100, 24 );
71 interestRateJTextField.setHorizontalAlignment(
72 JTextField.RIGHT );
73 contentPane.add( interestRateJTextField );
74
75 // set up yearsJLabel
76 yearsJLabel = new JLabel();
77 yearsJLabel.setBounds( 16, 96, 48, 24 );
78 yearsJLabel.setText( "Years:" );
79 contentPane.add( yearsJLabel );
80
81 // set up yearsJSpinner
82 yearsJSpinner = new JSpinner(
83 new SpinnerNumberModel( 1, 1, 10, 1 ) );
84 yearsJSpinner.setBounds( 100, 96, 100, 24 );
85 contentPane.add( yearsJSpinner );
86
87 // set up yearlyBalanceJLabel
88 yearlyBalanceJLabel = new JLabel();
89 yearlyBalanceJLabel.setBounds( 16, 136, 150, 24 );
90 yearlyBalanceJLabel.setText( "Yearly account balance:" );
91 contentPane.add( yearlyBalanceJLabel );
92
93 // set up yearlyBalanceJTextArea
94 yearlyBalanceJTextArea = new JTextArea();
95 yearlyBalanceJTextArea.setEditable( false );
96
97 // set up yearlyBalanceJScrollPane
98 yearlyBalanceJScrollPane = new JScrollPane(
99 yearlyBalanceJTextArea );
100 yearlyBalanceJScrollPane.setBounds( 16, 160, 300, 92 );
101 contentPane.add( yearlyBalanceJScrollPane );
102
103 // set up calculateJButton
104 calculateJButton = new JButton();
105 calculateJButton.setBounds( 216, 16, 100, 24 );
106 calculateJButton.setText( "Calculate" );
107 contentPane.add( calculateJButton );
108 calculateJButton.addActionListener(
109
110 new ActionListener() // anonymous inner class
111 {
112 // event handler called when calculateJButton is clicked
113 public void actionPerformed( ActionEvent event )
114 {
115 calculateJButtonActionPerformed( event );
116 }
117
118 } // end anonymous inner class
119
120 ); // end call to addActionListener
121
122
123 // set properties of application’s window
124 setTitle( "Interest Calculator" ); // set title bar text
125 setSize( 340, 296 ); // set window size
126 setVisible( true ); // display window
127
128 } // end method createUserInterface
129
130 // calculate and display amounts
131 private void calculateJButtonActionPerformed( ActionEvent event )
132 {
133 // declare variables to store user input
134 double principal = Double.parseDouble(
135 principalJTextField.getText() );
136 double rate = Double.parseDouble(
137 interestRateJTextField.getText() );
138
139 Integer integerObject = ( Integer ) yearsJSpinner.getValue();
140 int year = integerObject.intValue();
141
142 // display error message
143 if ( ( principal < 0 ) || ( rate < 0 ) || ( rate > 10 ) )
144 {
145 JOptionPane.showMessageDialog( null, "The information " +
146 "input was not within the correct range of values.",
147 "Invalid Input", JOptionPane.ERROR_MESSAGE );
148 }
149 else
150 {
151 yearlyBalanceJTextArea.setText( "Year\tAmount on Deposit" );
152 DecimalFormat dollars = new DecimalFormat( "$0.00" );
153
154 // calculate the total value for each year
155 for ( int count = 1; count <= year; count++ )
156 {
157 double amount =
158 principal * Math.pow( ( 1 + rate / 100 ), count );
159 yearlyBalanceJTextArea.append( "\n" + count + "\t" +
160 dollars.format( amount ) );
161
162 } // end for
163
164 } // end else statement
165
166 } // end method calculateJButtonActionPerformed
167
168 // main method
169 public static void main( String[] args )
170 {
171 InterestCalculator application = new InterestCalculator();
172 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
173
174 } // end method main
175
176 } // end class InterestCalculator
```
You might also like to view...
os.popen takes two arguments—________and_________ .
a) the shell command to execute, the function to call. b) the shell command to execute, the mode to use when calling the function. c) the function to call, the shell to use. d) None of the above.
In the figure above, which of the following is not an option if you click item C?
A. On Click B. On Roll Over C. On Roll Off D. On Drag