Modify the Miles Per Gallon application so that it includes exception handling to handle the NumberFormatExcep- tion when converting the strings in the JTextFields to doubles (Fig. 24.15). The original application allowed the user to input the number of miles driven and the number of gallons used for a tank of gas, in order to determine the number of miles the user was able to drive on one gallon of gas.
a) Copying the template to your working directory. Copy the C:Examples Tutorial24ExercisesEnhancedMilesPerGallon directory to your C:Simply- Java directory.
b) Opening the template file. Open the MilesPerGallon.java file in your text editor. c) Adding a try block. Find the calculateMPGJButtonActionPerformed method,
which immediately follows createUserInterface. Enclose lines 113–120 in a try
block.
d) Adding a catch block. Immediately following the try block you added in Step c, add a catch block to catch a NumberFormatException. Inside the catch block, add code to display an error message dialog at the center of the running application.
e) Saving the application. Save your modified source code file.
f) 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, EnhancedMilesPerGallon, by typing cd C:SimplyJavaEnhancedMilesPerGallon.
```
1 // MilesPerGallon.java
2 // Calculates miles per gallon based on user input.
3 import java.awt.*;
4 import java.awt.event.*;
5 import javax.swing.*;
6 import java.text.*;
7
8 public class MilesPerGallon extends JFrame
9 {
10 // JLabel and JTextField to enter the miles driven
11 private JLabel milesDrivenJLabel;
12 private JTextField milesDrivenJTextField;
14 // JLabel and JTextField to enter the number of gallons used
15 private JLabel gallonsUsedJLabel;
16 private JTextField gallonsUsedJTextField;
17
18 // JLabel and JTextField to display the miles per gallon
19 private JLabel milesPerGallonJLabel;
20 private JTextField milesPerGallonJTextField;
21
22 // JButton for calculating the miles per gallon
23 private JButton calculateMPGJButton;
24
25 // no-argument constructor
26 public MilesPerGallon()
27 {
28 createUserInterface();
29 }
30
31 // create and position GUI components; register event handlers
32 private void createUserInterface()
33 {
34 // get content pane for attaching GUI components
35 Container contentPane = getContentPane();
36
37 // enable explicit positioning of GUI components
38 contentPane.setLayout( null );
39
40 // set up milesDrivenJLabel
41 milesDrivenJLabel = new JLabel();
42 milesDrivenJLabel.setBounds( 16, 16, 81, 21 );
43 milesDrivenJLabel.setText( "Miles driven:" );
44 contentPane.add( milesDrivenJLabel );
45
46 // set up milesDrivenJTextField
47 milesDrivenJTextField = new JTextField();
48 milesDrivenJTextField.setBounds( 128, 16, 64, 21 );
49 milesDrivenJTextField.setHorizontalAlignment(
50 JTextField.RIGHT );
51 contentPane.add( milesDrivenJTextField );
52
53 // set up gallonsUsedJLabel
54 gallonsUsedJLabel = new JLabel();
55 gallonsUsedJLabel.setBounds( 16, 56, 81, 21 );
56 gallonsUsedJLabel.setText( "Gallons used:" );
57 contentPane.add( gallonsUsedJLabel );
58
59 // set up gallonsUsedJTextField
60 gallonsUsedJTextField = new JTextField();
61 gallonsUsedJTextField.setBounds( 128, 56, 64, 21 );
62 gallonsUsedJTextField.setHorizontalAlignment(
63 JTextField.RIGHT );
64 contentPane.add( gallonsUsedJTextField );
65
66 // set up MilesPerGallonJLabel
67 milesPerGallonJLabel = new JLabel();
68 milesPerGallonJLabel.setBounds( 16, 104, 96, 21 );
69 milesPerGallonJLabel.setText( "Miles per gallon:" );
70 contentPane.add( milesPerGallonJLabel );
71
72 // set up milesPerGallonJTextField
73 milesPerGallonJTextField = new JTextField();
74 milesPerGallonJTextField.setBounds( 128, 104, 64, 21 );
14 // JLabel and JTextField to enter the number of gallons used
15 private JLabel gallonsUsedJLabel;
16 private JTextField gallonsUsedJTextField;
17
18 // JLabel and JTextField to display the miles per gallon
19 private JLabel milesPerGallonJLabel;
20 private JTextField milesPerGallonJTextField;
21
22 // JButton for calculating the miles per gallon
23 private JButton calculateMPGJButton;
24
25 // no-argument constructor
26 public MilesPerGallon()
27 {
28 createUserInterface();
29 }
30
31 // create and position GUI components; register event handlers
32 private void createUserInterface()
33 {
34 // get content pane for attaching GUI components
35 Container contentPane = getContentPane();
36
37 // enable explicit positioning of GUI components
38 contentPane.setLayout( null );
39
40 // set up milesDrivenJLabel
41 milesDrivenJLabel = new JLabel();
42 milesDrivenJLabel.setBounds( 16, 16, 81, 21 );
43 milesDrivenJLabel.setText( "Miles driven:" );
44 contentPane.add( milesDrivenJLabel );
45
46 // set up milesDrivenJTextField
47 milesDrivenJTextField = new JTextField();
48 milesDrivenJTextField.setBounds( 128, 16, 64, 21 );
49 milesDrivenJTextField.setHorizontalAlignment(
50 JTextField.RIGHT );
51 contentPane.add( milesDrivenJTextField );
52
53 // set up gallonsUsedJLabel
54 gallonsUsedJLabel = new JLabel();
55 gallonsUsedJLabel.setBounds( 16, 56, 81, 21 );
56 gallonsUsedJLabel.setText( "Gallons used:" );
57 contentPane.add( gallonsUsedJLabel );
58
59 // set up gallonsUsedJTextField
60 gallonsUsedJTextField = new JTextField();
61 gallonsUsedJTextField.setBounds( 128, 56, 64, 21 );
62 gallonsUsedJTextField.setHorizontalAlignment(
63 JTextField.RIGHT );
64 contentPane.add( gallonsUsedJTextField );
65
66 // set up MilesPerGallonJLabel
67 milesPerGallonJLabel = new JLabel();
68 milesPerGallonJLabel.setBounds( 16, 104, 96, 21 );
69 milesPerGallonJLabel.setText( "Miles per gallon:" );
70 contentPane.add( milesPerGallonJLabel );
71
72 // set up milesPerGallonJTextField
73 milesPerGallonJTextField = new JTextField();
74 milesPerGallonJTextField.setBounds( 128, 104, 64, 21 );
75 milesPerGallonJTextField.setHorizontalAlignment(
76 JTextField.CENTER );
77 milesPerGallonJTextField.setEditable( false );
78 contentPane.add( milesPerGallonJTextField );
79
80 // set up calculateMPGJButton
81 calculateMPGJButton = new JButton();
82 calculateMPGJButton.setBounds( 76, 144, 116, 23 );
83 calculateMPGJButton.setText( "Calculate MPG" );
84 contentPane.add( calculateMPGJButton );
85 calculateMPGJButton.addActionListener(
86
87 new ActionListener() // anonymous inner class
88 {
89 // event handler called when calculateMPGJButton
90 // is pressed
91 public void actionPerformed( ActionEvent event )
92 {
93 calculateMPGJButtonActionPerformed( event );
94 }
95
96 } // end anonymous inner class
97
98 ); // end call to addActionListener
99
100 // set properties of application's window
101 setTitle( "Miles Per Gallon" ); // set title-bar String
102 setSize( 224, 208 ); // set window size
103 setVisible( true ); // display window
104
105 } // end method createUserInterface
106
107 // get user input, calculate miles per gallon, and display results
108 private void calculateMPGJButtonActionPerformed(
109 ActionEvent event )
110 {
111 DecimalFormat dollars = new DecimalFormat( "0.00" );
112
113
114
115
// try block to retrieve user input try
{
116 double milesDriven = Double.parseDouble(
117 milesDrivenJTextField.getText() );
118 double gallonsUsed = Double.parseDouble(
119 gallonsUsedJTextField.getText() );
120
121 // display miles per gallon
122 milesPerGallonJTextField.setText( dollars.format(
123 milesPerGallon( milesDriven, gallonsUsed ) ) );
124 }
125 catch ( NumberFormatException exception )
126 {
127 JOptionPane.showMessageDialog( this, "Please enter " +
128 "decimal numbers for the miles driven and gallons",
129 "Number Format Error", JOptionPane.ERROR_MESSAGE );
130 }
131
132 } // end method calculateMPGJButtonActionPerformed
133
134 // calculate and return miles per gallon
135 private double milesPerGallon( double milesDriven,
136 double gallonsUsed )
137 {
138 return milesDriven / gallonsUsed;
139
140 } // end method milesPerGallon
141
142 // main method
143 public static void main( String[] args )
144 {
145 MilesPerGallon application = new MilesPerGallon();
146 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
147
148 } // end method main
149
150 } // end class MilesPerGallon
```
You might also like to view...
You would most likely find a P2P network in a(n) ________
A) corporation B) university C) home D) library
The _____ includes provisions to combat cyberterrorism, including protecting ISPs against lawsuits from customers for revealing private information to law enforcement agencies.
A. ?Computer Fraud and Abuse Act B. ?U.S. SAFE WEB Act C. ?Homeland Security Act D. ?American Recovery and Reinvestment Act
In table relationships, the foreign key of one table cannot be joined to the ________ key of another table
Fill in the blank(s) with correct word
In the formula, 3*[BaseSalary], the 3 is considered a(n) ________.
A. variable B. field C. argument D. constant