The Factorial application calculates the factorial of an integer input by the user. The factorial of an integer is the product of each integer from one to that integer. For example, the factorial of 3—represented in mathematics as 3!— is 6 (1 2 3). While testing the application, you notice that it does not execute correctly. Use the debugger to find and correct the logic error(s) in the application. Figure 9.26 displays the correct out- put for the Factorial application.
5
a) Copying the template to your working directory. Copy the C:Examples Tutorial09ExercisesDebuggerFactorial directory 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:SimplyJavaDebug- gerFactorial.
c) Running the application. Run the Factorial application by typing java Factorial.
Enter the value 3 into the Enter number: JTextField and press the Calculate JBut- ton. Notice that the result displayed in the Factorial: JTextField (0) is not the cor- rect value (6).
d) Closing the application. Close your running application by clicking its close button. e) Compiling with the -g option. For debugging, compile the application by typing
javac -g Factorial.java.
f) Starting the debugger. Start the debugger by typing jdb.
g) Opening the template file. Open the Factorial.
The error in the Factorial application is that the result is always 0. The incorrect code lies in the do…while loop where the result is calculated. In the template application, the loop condition is counter >= 0. The value of counter will eventually equal 0, which when multiplied by factorial results in a value of 0. To fix this bug, change the loop condition to counter >= 1, or counter > 0.
```
1 // Factorial.java
2 // This application accepts an integer and finds its factorial
3 import java.awt.*;
4 import java.awt.event.*;
5 import javax.swing.*;
6
7 public class Factorial extends JFrame
8 {
9 // JLabel and JTextField for input
10 private JLabel enterNumberJLabel;
11 private JTextField enterNumberJTextField;
12
13 // JLabel and JTextField for displaying factorial
14 private JLabel factorialJLabel;
15 private JTextField factorialJTextField;
16
17 // JButton initiates calculation of factorial
18 private JButton calculateJButton;
19
20 // no-argument constructor
21 public Factorial()
22 {
23 createUserInterface();
24 }
25
26 // create and position GUI components; register event handlers
27 private void createUserInterface()
28 {
29 // get content pane for attaching GUI components
30 Container contentPane = getContentPane();
31
32 // enable explicit positioning of GUI components
33 contentPane.setLayout( null );
34
35 // set up enterNumberJLabel
36 enterNumberJLabel = new JLabel();
37 enterNumberJLabel.setBounds( 16, 16, 80, 23 );
38 enterNumberJLabel.setText( "Enter number:" );
39 contentPane.add( enterNumberJLabel );
40
41 // set up factorialJLabel
42 factorialJLabel = new JLabel();
43 factorialJLabel.setBounds( 16, 56, 72, 23 );
44 factorialJLabel.setText( "Factorial:" );
45 contentPane.add( factorialJLabel );
46
47 // set up factorialJTextField
48 factorialJTextField = new JTextField();
49 factorialJTextField.setBounds( 112, 56, 104, 23 );
50 factorialJTextField.setEditable( false );
51 contentPane.add( factorialJTextField );
52
53 // set up enterNumberJTextField
54 enterNumberJTextField = new JTextField();
55 enterNumberJTextField.setBounds( 112, 16, 104, 23 );
56 contentPane.add( enterNumberJTextField );
57
58 // set up calculateJButton
59 calculateJButton = new JButton();
60 calculateJButton.setBounds( 112, 96, 104, 26 );
61 calculateJButton.setText( "Calculate" );
62 contentPane.add( calculateJButton );
63 calculateJButton.addActionListener(
64
65 new ActionListener() // anonymous inner class
66 {
67 // event handler called when calculateJButton is pressed
68 public void actionPerformed( ActionEvent event )
69 {
70 calculateJButtonActionPerformed( event );
71 }
72
73 } // end anonymous inner class
74
75 ); // end call to addActionListener
76
77 // set properties of application’s window
78 setTitle( "Factorial" ); // set title bar text
79 setSize( 240, 160 ); // set window size
19
20 // no-argument constructor
21 public Factorial()
22 {
23 createUserInterface();
24 }
25
26 // create and position GUI components; register event handlers
27 private void createUserInterface()
28 {
29 // get content pane for attaching GUI components
30 Container contentPane = getContentPane();
31
32 // enable explicit positioning of GUI components
33 contentPane.setLayout( null );
34
35 // set up enterNumberJLabel
36 enterNumberJLabel = new JLabel();
37 enterNumberJLabel.setBounds( 16, 16, 80, 23 );
38 enterNumberJLabel.setText( "Enter number:" );
39 contentPane.add( enterNumberJLabel );
40
41 // set up factorialJLabel
42 factorialJLabel = new JLabel();
43 factorialJLabel.setBounds( 16, 56, 72, 23 );
44 factorialJLabel.setText( "Factorial:" );
45 contentPane.add( factorialJLabel );
46
47 // set up factorialJTextField
48 factorialJTextField = new JTextField();
49 factorialJTextField.setBounds( 112, 56, 104, 23 );
50 factorialJTextField.setEditable( false );
51 contentPane.add( factorialJTextField );
52
53 // set up enterNumberJTextField
54 enterNumberJTextField = new JTextField();
55 enterNumberJTextField.setBounds( 112, 16, 104, 23 );
56 contentPane.add( enterNumberJTextField );
57
58 // set up calculateJButton
59 calculateJButton = new JButton();
60 calculateJButton.setBounds( 112, 96, 104, 26 );
61 calculateJButton.setText( "Calculate" );
62 contentPane.add( calculateJButton );
63 calculateJButton.addActionListener(
64
65 new ActionListener() // anonymous inner class
66 {
67 // event handler called when calculateJButton is pressed
68 public void actionPerformed( ActionEvent event )
69 {
70 calculateJButtonActionPerformed( event );
71 }
72
73 } // end anonymous inner class
74
75 ); // end call to addActionListener
76
77 // set properties of application’s window
78 setTitle( "Factorial" ); // set title bar text
79 setSize( 240, 160 ); // set window size
80 setVisible( true ); // display window
81
82 } // end method createUserInterface
83
84 // method to calculate factorial based on input
85 private void calculateJButtonActionPerformed( ActionEvent event )
86 {
87 int counter = 1;
88 int factorial = 1;
89
90 // retrieve user input, use as counter
91 counter = Integer.parseInt( enterNumberJTextField.getText() );
92
93 do // find factorial
94 {
95 factorial *= counter;
96 counter--; // decrease counter by 1 with each iteration
97
98 }
99 while( counter >= 1 );
100
101 // display value in factorialJTextField
102 factorialJTextField.setText( String.valueOf( factorial ) );
103
104 } // end method calculateJButtonActionPerformed
105
106 // main method
107 public static void main( String[] args )
108 {
109 Factorial application = new Factorial();
110 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
111
112 } // end method main
113
114 } // end class Factorial
```
You might also like to view...
Why is the maximum value of any color component (red, green, or blue) 255?
What will be an ideal response?
Which logical unit of the computer sends information that has already been processed by the computer to various devices so that the information may be used outside the computer? _______ .
Fill in the blank(s) with the appropriate word(s).
The two types of errors in VBA are handled and unhandled
Indicate whether the statement is true or false
The clns commands are used when dealing with the IS-IS protocol at the Layer 2 level. True or false?
a. True b. False