(Odd Numbers Application) The Odd Numbers application should display all of the odd integers from one through the number input by the user. Figure 8.26 displays the correct output for the application. In this exercise, you will use the debugger to find and fix the error(s) in the application.
```
a) Copying the template to your working directory. Copy the directory C:Examples Tutorial08ExercisesDebuggerOddNumbers to your C:SimplyJava directory.
b) 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:SimplyJavaOddNumbers.
c) Running the application. Run the application by typing java OddNumbers. Enter a value into the Upper limit: JTextField, then click the View JButton. Notice that the JButton remains pressed and no output occurs. This is because the application con- tains an infinite loop in the viewJButtonActionPerformed event handler.
d) Closing the running application. Close the running application by clicking in the
Command Prompt window, holding the ctrl key and pressing C.
e) Compiling the application for debugging. Compile the application with the -g com- mand-line option by typing javac -g OddNumbers.j
```
1 // OddNumbers.java
2 // Displays a table of odd numbers up to a given limit.
3 import java.awt.*;
4 import java.awt.event.*;
5 import javax.swing.*;
6
7 public class OddNumbers extends JFrame
8 {
9 // JLabel and JTextField to input limit
10 private JLabel limitJLabel;
11 private JTextField limitJTextField;
12
13 // JButton to initiate display of odd integers
14 private JButton viewJButton;
15
16 // JTextArea to display results
17 private JTextArea outputJTextArea;
18
19 // no-argument constructor
20 public OddNumbers()
21 {
22 createUserInterface();
23 }
24
25 // create and position the GUI components; register event handlers
26 public void createUserInterface()
27 {
28 // get content pane for attaching GUI components
29 Container contentPane = getContentPane();
30
31 // enable explicit positioning of GUI components
32 contentPane.setLayout( null );
33
34 // set up limitJLabel
35 limitJLabel = new JLabel();
36 limitJLabel.setBounds( 16, 16, 64, 26 );
37 limitJLabel.setText( "Upper limit:" );
38 contentPane.add( limitJLabel );
39
40 // set up limitJTextField
41 limitJTextField = new JTextField();
42 limitJTextField.setBounds( 96, 16, 55, 26 );
43 limitJTextField.setHorizontalAlignment( JTextField.RIGHT );
44 contentPane.add( limitJTextField );
45
46 // set up viewJButton
47 viewJButton = new JButton();
48 viewJButton.setBounds( 168, 16, 90, 26 );
49 viewJButton.setText( "View" );
50 contentPane.add( viewJButton );
51 viewJButton.addActionListener(
52
53 new ActionListener() // anonymous inner class
54 {
55 // event handler called when user clicks viewJButton
56 public void actionPerformed( ActionEvent event )
57 {
58 viewJButtonActionPerformed( event );
59 }
60
61 } // end anonymous inner class
62
63 ); // end call to addActionListener
64
65 // set up outputJTextArea
66 outputJTextArea = new JTextArea();
67 outputJTextArea.setEditable( false );
68
69 // set up JScrollPane to allow outputJTextArea scrolling
70 JScrollPane scrollJScrollPane =
71 new JScrollPane( outputJTextArea );
72 scrollJScrollPane.setBounds( 16, 56, 242, 95 );
73 contentPane.add( scrollJScrollPane );
74
75 // set properties of application’s window
76 setTitle( "Odd Numbers" ); // set title bar text
77 setSize( 282, 192 ); // set windows's size
78 setVisible( true ); // display window
79
80 } // end method createUserInterface
81
82 // called when user clicks viewJButton
83 public void viewJButtonActionPerformed( ActionEvent event )
84 {
85 int counter = 1;
86 int limit = Integer.parseInt( limitJTextField.getText() );
87
88 // display header
89 outputJTextArea.setText( "Odd numbers:" );
90
91 // get odd numbers
92 while ( counter <= limit )
93 {
94 if ( ( counter % 2 ) != 0 )
95 {
96 outputJTextArea.append( "\n" + counter );
97 }
98
99 counter++; // increment counter
100
101 } // end while
102
103 } // end method viewJButtonActionPerformed
104
105 // main method
106 public static void main( String args[] )
107 {
108 OddNumbers application = new OddNumbers();
109 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
110
111 } // end method main
112
113 } // end class OddNumbers
```
You might also like to view...
A ________ is a simplified version of a function used to test the main program.
What will be an ideal response?
Sara installed Windows 10 for the first time on her computer. During the installation process, she noticed that a new system partition called ESP partition was created. Sara cannot access this new partition to see what it contains. Â What can you tell Sara about the ESP partition?
A. It was created by the MBR to store Windows boot and startup files. B. It was created by Windows to store the Windows OS files. C. It was created by GPT to store Windows OS files. D. It was created by GPT to store Windows boot and startup files.
A(n) ____ shows the relationship of each page to the other pages in the site.
A. flowchart B. index C. wireframe D. data dictionary
ENIAC's memory held programs but not data.
Answer the following statement true (T) or false (F)