The Gas Pump application (Fig. 12.31) calculates the cost of gas at a local gas station. This gas station charges $1.61 per gallon for Regular grade gas, $1.67 per gallon for Special grade gas and $1.77 per gallon for Super+ grade gas. The user enters the number of gallons to purchase and clicks the desired grade. The application calls a method to compute the total cost from the number of gallons entered and the selected grade. The application provided contains a logic error. In this exercise, you will find and fix the error



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

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

c) Compiling the application for debugging. Compile the application with the -g com- mand-line option by typing javac -g GasPump.java.

d) Running the application. Run the Gas Pump application by typing java GasPump.

Enter 14 as the starting amount and click the Special JButton. Notice that the out- put is incorrect (the correct output is displayed in Fig. 12.31).

e) Starting the debugger. Close your application (but leave the Command Prompt

open) and start the debugger by typing jdb.

f) Finding and correcting the error(s). Add a breakpoint at the beginning of method specialJButtonActionPerformed. Use the step, step up and next commands as necessary to walk through the different statements of this method and any methods calle


The code given used a price of $1.57 on line 203. This causes incorrect results when the Special JButton is clicked.
```
1 // GasPump.java
2 // Calculates gas prices based on the number of gallons and grade.
3 import java.awt.*;
4 import java.awt.event.*;
5 import java.text.*;
6 import javax.swing.*;
7
8 public class GasPump extends JFrame
9 {
10 // JLabel and JTextField for number of gallons
11 private JLabel numberGallonsJLabel;
12 private JTextField numberGallonsJTextField;
13
14 // JLabel and JTextField for total price
15 private JLabel totalJLabel;
16 private JTextField totalJTextField;
17
18 // JLabel and JButton for regular grade gas
19 private JLabel regularJLabel;
20 private JButton regularJButton;
21
22 // JLabel and JButton for special grade gas
23 private JLabel specialJLabel;
24 private JButton specialJButton;
25
26 // JLabel and JButton for super+ grade gas
27 private JLabel superJLabel;
28 private JButton superJButton;
29
30 private double gallons = 0.0; // number of gallons
31
32 // no-argument constructor
33 public GasPump()
34 {
35 createUserInterface();
36 }
37
38 // create and position GUI components; register event handlers
39 private void createUserInterface()
40 {
41 // get content pane for attaching GUI components
42 Container contentPane = getContentPane();
43
44 // enable explicit positioning of GUI components
45 contentPane.setLayout( null );
46
47 // set up numberGallonsJLabel
48 numberGallonsJLabel = new JLabel();
49 numberGallonsJLabel.setBounds( 16, 16, 114, 21 );
50 numberGallonsJLabel.setText( "Number of gallons:" );
51 contentPane.add( numberGallonsJLabel );
52
53 // set up numberGallonsJTextField
54 numberGallonsJTextField = new JTextField();
55 numberGallonsJTextField.setBounds( 138, 16, 86, 21 );
56 numberGallonsJTextField.setHorizontalAlignment(
57 JTextField.RIGHT );
58 contentPane.add( numberGallonsJTextField );
59
60 // set up totalJLabel
61 totalJLabel = new JLabel();
62 totalJLabel.setBounds( 240, 16, 40, 21 );
63 totalJLabel.setText( "Total:" );
64 contentPane.add( totalJLabel );
65
66 // set up totalJTextField
67 totalJTextField = new JTextField();
68 totalJTextField.setBounds( 291, 16, 61, 21 );
69 totalJTextField.setHorizontalAlignment(
70 JTextField.CENTER );
71 totalJTextField.setEditable( false );
72 contentPane.add( totalJTextField );
73
74 // set up regularJLabel
75 regularJLabel = new JLabel();
76 regularJLabel.setBounds( 16, 56, 85, 21 );
77 regularJLabel.setText( "Regular: $1.61" );
78 contentPane.add( regularJLabel );
79
80 // set up regularJButton
81 regularJButton = new JButton();
82 regularJButton.setBounds( 16, 77, 85, 85 );
83 regularJButton.setText( "Regular" );
84 contentPane.add( regularJButton );
85 regularJButton.addActionListener(
86
87 new ActionListener() // anonymous inner class
88 {
89 // event handler called when regularJButton is pressed
90 public void actionPerformed( ActionEvent event )
91 {
92 regularJButtonActionPerformed( event );
93 }
94
95 } // end anonymous inner class
96
97 ); // end call to addActionListener
98
99 // set up specialJLabel
100 specialJLabel = new JLabel();
101 specialJLabel.setBounds( 144, 56, 85, 21 );
102 specialJLabel.setText( "Special: $1.67" );
103 contentPane.add( specialJLabel );
104
105 // set up specialJButton
106 specialJButton = new JButton();
107 specialJButton.setBounds( 144, 77, 85, 85 );
108 specialJButton.setText( "Special" );
109 contentPane.add( specialJButton );
110 specialJButton.addActionListener(
111
112 new ActionListener() // anonymous inner class
113 {
114 // event handler called when specialJButton is pressed
115 public void actionPerformed( ActionEvent event )
116 {
117 specialJButtonActionPerformed( event );
118 }
119
120 } // end anonymous inner class
121
122 ); // end call to addActionListener
123
124 // set up superJLabel
125 superJLabel = new JLabel();
126 superJLabel.setBounds( 272, 56, 85, 21 );
127 superJLabel.setText( "Super+: $1.77" );
128 contentPane.add( superJLabel );
129
130 // set up superJButton
131 superJButton = new JButton();
132 superJButton.setBounds( 272, 77, 85, 85 );
133 superJButton.setText( "Super+" );
134 contentPane.add( superJButton );
135 superJButton.addActionListener(
136
137 new ActionListener() // anonymous inner class
138 {
139 // event handler called when superJButton is clicked
140 public void actionPerformed( ActionEvent event )
141 {
142 superJButtonActionPerformed( event );
143 }
144
145 } // end anonymous inner class
146
147 ); // end call to addActionListener
148
149 // set properties of application's window
150 setTitle( "Gas Pump" ); // set title bar string
151 setSize( 380, 202 ); // set window size
152 setVisible( true ); // display window
153
154 } // end method createUserInterface
155
156 // calculate total for regular gas grade
157 private void regularJButtonActionPerformed( ActionEvent event )
158 {
159 gallons = Double.parseDouble(
160 numberGallonsJTextField.getText() );
161
162 // call method to determine total
163 total( 1, gallons );
164
165 } // end method regularJButtonActionPerformed
166
167 // calculates total for special gas grade
168 private void specialJButtonActionPerformed( ActionEvent event )
169 {
170 gallons = Double.parseDouble(
171 numberGallonsJTextField.getText() );
172
173 // call method to determine total
174 total( 2, gallons );
175
176 } // end method specialJButtonActionPerformed
177
178 // calculates total for super gas grade
179 private void superJButtonActionPerformed( ActionEvent event )
180 {
181 gallons = Double.parseDouble(
182 numberGallonsJTextField.getText() );
183
184 // call method to determine total
185 total( 3, gallons );
186
187 } // end method superJButtonActionPerformed
188
189 // calculate and display total price based on gas grade
190 private void total( int grade, double gallons )
191 {
192 DecimalFormat dollars = new DecimalFormat( "$0.00" );
193
194 // determine grade selected and output total
195 switch ( grade )
196 {
197 case 1:
198 totalJTextField.setText(
199 dollars.format( 1.61 * gallons ) );
200 break;
201 case 2:
202 totalJTextField.setText(
203 dollars.format( 1.67 * gallons ) );
204 break;
205 case 3:
206 totalJTextField.setText(
207 dollars.format( 1.77 * gallons ) );
208 break;
209
210 } // end switch
211
212 } // end method total
213
214 // main method
215 public static void main( String[] args )
216 {
217 GasPump application = new GasPump();
218 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
219
220 } // end method main
221
222 } // end class GasPump
```

Computer Science & Information Technology

You might also like to view...

How does the computer keep track of all the calls to a recursive function?

What will be an ideal response?

Computer Science & Information Technology

The best-case time complexity for this function would be _________, where n is the number of elements.

``` 1 bool search( Node ptr, Bird & bird, Bird dove ) 2 { 3 if ( ptr == NULL ) 4 return false; 5 if ( ptr->info == dove ) { 6 bird = ptr->info; 7 return true; 8 } 9 return search( ptr->next, bird, dove ); 10 } ``` which is called for a linked list (where start points to the first node) using the lines of code: ``` if ( search( start, bird, dove ) ) cout << “search successful” << endl; ``` A. O( lg n ) B. O( 1 ) C. O( n2 ) D. O( n )

Computer Science & Information Technology

A network technician has detected duplicate IP addresses on the network. After testing the behavior of rogue DHCP servers, the technician believes that the issue is related to an unauthorized home router. Which of the following should the technician do NEXT in the troubleshooting methodology?

A. Document the findings and action taken. B. Establish a plan to locate the rogue DHCP server. C. Remove the rogue DHCP server from the network. D. Identify the root cause of the problem.

Computer Science & Information Technology

Which of these is not one of the technology evolution phases?

A. Innovation B. Empiricism C. Automation  D. Breakthrough

Computer Science & Information Technology