The Savings Calculator application calculates the amount that the user will have on deposit after one year. The application gets the initial amount on deposit from the user, and assumes that the user will add $100 to the account every month for the entire year. No interest is added to the account. While testing the appli- cation, you noticed that the amount calculated by the application was incorrect. Use the debugger to locate and correct any logic error(s). Figure 10.31 displays the correct output for this application.



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

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

c) Running the application. Run the Savings Calculator application by typing java SavingsCalculator. Enter 100 as the starting amount and click the Calculate JButton. Notice that the amount after one year is 1200, whereas the correct output would be 1300 (Fig. 10.31).

d) Compiling with the -g option. Close your application (but leave the Command Prompt open), and compile the application by typing javac -g SavingsCalcula- tor.java.

e) Starting the debugger. Start the debugger by typing jdb.

f) Finding and correcting the error(s). Use the debugging skills learned in previous tutorials to determine where the application’s logic errors exist. Modify the application so that it displays the correct

The for header should be for ( int counter = 1; counter <= 12; counter++ ). The specific error is pointed out in the code below
```
1 // SavingsCalculator.java
2 // Application that calculates the amount of money the user will have
3 // after one year of saving $100 a month, plus an initial deposit.
4 import java.awt.*;
5 import java.awt.event.*;
6 import javax.swing.*;
7
8 public class SavingsCalculator extends JFrame
9 {
10 // JLabel and JTextField for starting amount
11 private JLabel startingJLabel;
12 private JTextField startingJTextField;
13
14 // JLabel and JTextField for amount after one year
15 private JLabel yearJLabel;
16 private JTextField yearJTextField;
17
18 // JButton calculates amounts after one, two and three years
19 private JButton calculateJButton;
20
21 // no-argument constructor
22 public SavingsCalculator()
23 {
24 createUserInterface();
25 }
26
27 // create and position GUI components; register event handlers
28 private void createUserInterface()
29 {
30 // get content pane for attaching GUI components
31 Container contentPane = getContentPane();
32
33 // enable explicit positioning of GUI components
34 contentPane.setLayout( null );
35
36 // set up startingJLabel
37 startingJLabel = new JLabel();
38 startingJLabel.setBounds( 15, 15, 120, 20 );
39 startingJLabel.setText( "Starting amount:" );
40 contentPane.add( startingJLabel );
41
42 // set up startingJTextField
43 startingJTextField = new JTextField();
44 startingJTextField.setBounds( 155, 15, 90, 20 );
45 startingJTextField.setHorizontalAlignment( JTextField.RIGHT );
46 contentPane.add( startingJTextField );
47
48 // set up yearJLabel
49 yearJLabel = new JLabel();
50 yearJLabel.setBounds( 15, 50, 160, 20 );
51 yearJLabel.setText( "Amount after one year:" );
52 contentPane.add( yearJLabel );
53
54 // set up yearJTextField
55 yearJTextField = new JTextField();
56 yearJTextField.setBounds( 155, 50, 90, 20 );
57 yearJTextField.setText( "0" );
58 yearJTextField.setHorizontalAlignment( JTextField.RIGHT );
59 contentPane.add( yearJTextField );
60
61 // set up calculateJButton
62 calculateJButton = new JButton();
63 calculateJButton.setBounds( 155, 85, 90, 20 );
64 calculateJButton.setText( "Calculate" );
65 contentPane.add( calculateJButton );
66 calculateJButton.addActionListener(
67
68 new ActionListener() // anonymous inner class
69 {
70 // event handler called when calculateJButton is pressed
71 public void actionPerformed( ActionEvent event )
72 {
73 calculateJButtonActionPerformed( event );
74 }
75
76 } // end anonymous inner class
77
78 ); // end call to addActionListener
79
80 // set properties of application’s window
81 setTitle( "Savings Calculator" ); // set title bar text
82 setSize( 270, 150 ); // set window size
83 setVisible( true ); // display window
84
85 } // end method createUserInterface
86
87 // calculate and display amount
88 private void calculateJButtonActionPerformed( ActionEvent event )
89 {
90 int deposit = Integer.parseInt( startingJTextField.getText() );
91
92 // for loop increments deposit
93 for ( int counter = 1; counter <= 12; counter++ )
94 {
95 deposit += 100;
96 }
97
98 // display deposit in yearJTextField
99 yearJTextField.setText( String.valueOf( deposit ) );
100
101 } // end method calculateJButtonActionPerformed
102
103 // main method
104 public static void main( String[] args )
105 {
106 SavingsCalculator application = new SavingsCalculator();
107 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
108
109 } // end method main
110
111 } // end class SavingsCalculator
```

Computer Science & Information Technology

You might also like to view...

Which of the following is a method that optical drive manufacturers have used to increase transfer rates?

A) Increasing RAM on video cards B) Adding buffer memory within the drive C) Minimizing hard drive caching because the CD-ROM is faster D) Reducing allowable colors and resolution for video E) None of these are methods used for this purpose.

Computer Science & Information Technology

Ruby's problem in the early days was the lack of a killer application that demonstrated is capabilities.

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

Computer Science & Information Technology