Drivers often want to know the miles per gallon their cars get so they can estimate gasoline costs. Develop an application that allows the user to input the number of miles driven and the number of gallons used for a tank of gas (Fig. 12.30).





a) Copying the template to your working directory. Copy the C:Examples Tutorial12ExercisesMilesPerGallon directory to your C:SimplyJava direc- tory.

b) Opening the template file. Open the MilesPerGallon.java file in your text editor. c) Adding a method to calculate miles per gallon. On line 124, add a comment indicat-

ing that the method will calculate the amount of miles per gallon. On lines 125–126,

add the method header for this method (use two lines for readability). The method will be called milesPerGallon. This method returns a value of type double and takes two arguments of type double. Name the first double parameter milesDriven and the second parameter gallonsUsed. On line 127, add a left brace to begin the body of the method. On line 128, add a return statement that performs the calcula- tion. To do this, follow the return keyword with the following expression:



milesDriven / gallonsUsed;



On line 130, add the right brace to end the body of the

```
1 // MilesPerGallon.java
2 // Calculates miles per gallon based on user input.
3 import java.awt.*;
4 import java.awt.event.*;
5 import java.text.*;
6 import javax.swing.*;
7
8 public class MilesPerGallon extends JFrame
9 {
10 // JLabel and JTextField for amount of miles driven
11 private JLabel milesDrivenJLabel;
12 private JTextField milesDrivenJTextField;
13
14 // JLabel and JTextField for amount of gallons used
15 private JLabel gallonsUsedJLabel;
16 private JTextField gallonsUsedJTextField;
17
18 // JLabel and JTextField for amount of miles per gallon
19 private JLabel milesPerGallonJLabel;
20 private JTextField milesPerGallonJTextField;
21
22 // JButton initiates calculation of 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
90 // calculateMPGJButton is clicked
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, call method milesPerGallon and display results
108 private void calculateMPGJButtonActionPerformed(
109 ActionEvent event )
110 {
111 DecimalFormat twoDigits = new DecimalFormat( "0.00" );
112
113 double miles = Double.parseDouble(
114 milesDrivenJTextField.getText() );
115 double gallons = Double.parseDouble(
116 gallonsUsedJTextField.getText() );
117
118 // display miles per gallon
119 milesPerGallonJTextField.setText( twoDigits.format(
120 milesPerGallon( miles, gallons ) ) );
121
122 } // end method calculateMPGJButtonActionPerformed
124 // calculate and return miles per gallon
125 private double milesPerGallon( double milesDriven,
126 double gallonsUsed )
127 {
128 return milesDriven / gallonsUsed;
129
130 } // end method milesPerGallon
131
132
133
134
135
132 // main method
133 public static void main( String[] args )
134 {
135 MilesPerGallon application = new MilesPerGallon();
136 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
137
138 } // end method main
139
140 } // end class MilesPerGallon
```

Computer Science & Information Technology

You might also like to view...

To create ________, mark the legal citations in a document

A) a table of authorities B) a table of contents C) a table of figures D) an index

Computer Science & Information Technology

What is the purpose of SIP in VoIP?

A) It carries voice and data. B) It connects the VoIP phone to an analog phone. C) It sets up the VoIP phone calls. D) It works with a PBX.

Computer Science & Information Technology