(Bank Account Information Debugging Exercise) Copy the C:\Examples\ Tutorial04\Exercises\DebuggingExercise directory to the C:\SimplyJava directory. Compile and run the Bank Account Information application. Remove any syntax errors and logic errors, so that the application runs correctly. [Hint: All the syntax and logic errors appear in the event handler code (lines 146–156 of the application).]

What will be an ideal response?


The method name Integer.parseInt was spelled with incorrect capitalization on lines 150–151. Also, the multiplication operator in line 150 should be an addition operator.
```
1 // Exercise 4.16: AccountInformation.java
2 // This application inputs and outputs account information.
3 import java.awt.*;
4 import java.awt.event.*;
5 import javax.swing.*;
6 import javax.swing.border.*;
7
8 public class AccountInformation extends JFrame
9 {
10 // JPanel to group deposit components
11 private JPanel enterJPanel;
12
13 // JLabel and JTextField for deposits
14 private JLabel depositJLabel;
15 private JTextField depositJTextField;
16
17 // JButton to initiate balance calculation
18 private JButton enterJButton;
19
20 // JPanel to group account information components
21 private JPanel accountJPanel;
22
23 // JLabel and JTextField for account holder's name
24 private JLabel nameJLabel;
25 private JTextField nameJTextField;
26
27 // JLabel and JTextField for account number
28 private JLabel accountNumberJLabel;
29 private JTextField accountNumberJTextField;
30
31 // JLabel and JTextField for balance
32 private JLabel balanceJLabel;
33 private JTextField balanceJTextField;
34
35 // no-argument constructor
36 public AccountInformation()
37 {
38 createUserInterface();
39 }
40
41 // create and position GUI components; register event handlers
42 private void createUserInterface()
43 {
44 // get content pane and set layout to null
45 Container contentPane = getContentPane();
46 contentPane.setLayout( null );
47
48 // set up enterJPanel
49 enterJPanel = new JPanel();
50 enterJPanel.setLayout( null );
51 enterJPanel.setBounds( 16, 16, 152, 126 );
52 enterJPanel.setBorder(
53 new TitledBorder( "Enter information" ) );
54 contentPane.add( enterJPanel );
55
56 // set up depositJLabel
57 depositJLabel = new JLabel();
58 depositJLabel.setText( "Deposit amount:" );
59 depositJLabel.setBounds( 16, 40, 140, 16 );
60 enterJPanel.add( depositJLabel );
61
62 // set up depositJTextField
63 depositJTextField = new JTextField();
64 depositJTextField.setText( "0" );
65 depositJTextField.setBounds( 16, 56, 120, 21 );
66 depositJTextField.setHorizontalAlignment( JTextField.RIGHT );
67 enterJPanel.add( depositJTextField );
68
69 // set up enterJButton
70 enterJButton = new JButton();
71 enterJButton.setText( "Enter" );
72 enterJButton.setBounds( 16, 150, 152, 34 );
73 contentPane.add( enterJButton );
74 enterJButton.addActionListener(
75
76 new ActionListener() // anonymous inner class
77 {
78 // event handler called when enterJButton is pressed
79 public void actionPerformed( ActionEvent event )
80 {
81 enterJButtonActionPerformed( event );
82 }
83
84 } // end anonymous inner class
85
86 ); // end call to addActionListener
87
88 // set up accountJPanel
89 accountJPanel = new JPanel();
90 accountJPanel.setLayout( null );
91 accountJPanel.setBounds( 180, 16, 136, 170 );
92 accountJPanel.setBorder(
93 new TitledBorder( "Account information" ) );
94 contentPane.add( accountJPanel );
95
96 // set up nameJLabel
97 nameJLabel = new JLabel();
98 nameJLabel.setText( "Name:" );
99 nameJLabel.setBounds( 16, 24, 100, 16 );
100 accountJPanel.add( nameJLabel );
101
102 // set up nameJTextField
103 nameJTextField = new JTextField();
104 nameJTextField.setText( "Sue Purple" );
105 nameJTextField.setBounds( 16, 40, 104, 21 );
106 nameJTextField.setEditable( false );
107 accountJPanel.add( nameJTextField );
108
109 // set up accountNumberJLabel
110 accountNumberJLabel = new JLabel();
111 accountNumberJLabel.setText( "Account Number:" );
112 accountNumberJLabel.setBounds( 16, 70, 140, 16 );
113 accountJPanel.add( accountNumberJLabel );
114
115 // set up accountNumberJTextField
116 accountNumberJTextField = new JTextField();
117 accountNumberJTextField.setText( "12345" );
118 accountNumberJTextField.setBounds( 16, 86, 104, 21 );
119 accountNumberJTextField.setEditable( false );
120 accountNumberJTextField.setHorizontalAlignment(
121 JTextField.RIGHT );
122 accountJPanel.add( accountNumberJTextField );
123
124 // set up balanceJLabel
125 balanceJLabel = new JLabel();
126 balanceJLabel.setText( "Balance:" );
127 balanceJLabel.setBounds( 16, 116, 100, 16 );
128 accountJPanel.add( balanceJLabel );
129
130 // set up balanceJTextField
131 balanceJTextField = new JTextField();
132 balanceJTextField.setText( "0" );
133 balanceJTextField.setBounds( 16, 132, 104, 21 );
134 balanceJTextField.setEditable( false );
135 balanceJTextField.setHorizontalAlignment( JTextField.RIGHT );
136 accountJPanel.add( balanceJTextField );
137
138 // set properties of application’s window
139 setTitle( "Account Information" ); // set title bar text
140 setSize( 340, 225 ); // set window's size
141 setVisible( true ); // display window
142
143 } // end method createUserInterface
144
145 // update account balance based on user input
146 private void enterJButtonActionPerformed( ActionEvent event )
147 {
148 // display new balance
149 balanceJTextField.setText( String.valueOf(
150 Integer.parseInt( balanceJTextField.getText() ) +
151 Integer.parseInt( depositJTextField.getText() ) ) );
152
153 // clear depositJTextField
154 depositJTextField.setText( "0" );
155
156 } // end method enterJButtonActionPerformed
157
158 // main method
159 public static void main( String args[] )
160 {
161 AccountInformation application = new AccountInformation();
162 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
163
164 } // end method main
165
166 } // end class AccountInformation
```

Computer Science & Information Technology

You might also like to view...

What section for the main body of a form displays records and usually contains all the bound controls?

A. Form Header B. Page Header C. Page Footer D. Detail

Computer Science & Information Technology

Which of the following is a disadvantage of an embedded chart??

A. ?It cannot be repositioned from one worksheet to another. B. ?It cannot be resized or formatted once embedded. C. ?It might cover worksheet cells that hide data and formulas. D. ?It might restrict renaming some chart elements.

Computer Science & Information Technology

The filter applied to the Gantt chart in the accompanying figure is called_________________.

A. Complete B. New Tasks C. Highlight D. None of the above

Computer Science & Information Technology

To ________ one or more slices on a pie chart means to pull the slice(s) away from the rest of the chart

A) explode B) base C) tick D) bevel

Computer Science & Information Technology