Write an application that performs two types of temperature conversions: degrees Fahrenheit to degrees Celsius and degrees Celsius to degrees Fahrenheit. Your output should look like Fig. 12.28.
a) Copying the template to your working directory. Copy the C:Examples Tutorial12ExercisesTemperatureConverter directory to your C:SimplyJava directory.
b) Opening the template file. Open the TemperatureConverter.java file in your text editor.
c) Adding a method to your application to convert from Celsius to Fahrenheit. On line
147, add a comment indicating that the method will convert the temperature from Celsius to Fahrenheit. On line 148, add the method header for this method. The method will be called convertToFahrenheit. This method returns a value of type double and takes an argument of type double. Name the double parameter degree. On line 149, add a left brace to begin the body of the method. On line 150, add a return statement that performs the conversion calculation. To do this, follow the return keyword with the following expression:
( 9.0 / 5.0 ) * degree + 32.0;
On line 152, add the right brace to end the body of the method. Follow
```
1 // TemperatureConverter.java
2 // Converts a degree amount from Celsius to Fahrenheit,
3 // or from Fahrenheit to Celsius.
4 import java.awt.*;
5 import java.awt.event.*;
6 import javax.swing.*;
7 import java.text.*;
8
9 public class TemperatureConverter extends JFrame
10 {
11 // JLabel and JTextField for degrees value, entered by user
12 private JLabel degreesJLabel;
13 private JTextField degreesJTextField;
14
15 // JTextField displays degrees converted to Fahrenheit or Celsius
16 private JTextField outputJTextField;
17
18 // JButtons initiate conversion to Fahrenheit or Celsius
19 private JButton convertFahrenheitJButton;
20 private JButton convertCelsiusJButton;
21
22 // no-argument constructor
23 public TemperatureConverter()
24 {
25 createUserInterface();
26 }
27
28 // create and position GUI components; register event handlers
29 private void createUserInterface()
30 {
31 // get content pane for attaching GUI components
32 Container contentPane = getContentPane();
33
34 // enable explicit positioning of GUI components
35 contentPane.setLayout( null );
36
37 // set up degreesJLabel
38 degreesJLabel = new JLabel();
39 degreesJLabel.setBounds( 16, 16, 56, 21 );
40 degreesJLabel.setText( "Degrees:" );
41 contentPane.add( degreesJLabel );
42
43 // set up degreesJTextField
44 degreesJTextField = new JTextField();
45 degreesJTextField.setBounds( 76, 16, 64, 21 );
46 degreesJTextField.setHorizontalAlignment( JTextField.RIGHT );
47 contentPane.add( degreesJTextField );
48
49 // set up outputJTextField
50 outputJTextField = new JTextField();
51 outputJTextField.setBounds( 16, 56, 354, 21 );
52 outputJTextField.setHorizontalAlignment( JTextField.CENTER );
53 outputJTextField.setEditable( false );
54 contentPane.add( outputJTextField );
55
56 // set up convertFahrenheitJButton
57 convertFahrenheitJButton = new JButton();
58 convertFahrenheitJButton.setBounds( 16, 104, 162, 40 );
59 convertFahrenheitJButton.setText( "Convert To Fahrenheit" );
60 contentPane.add( convertFahrenheitJButton );
61 convertFahrenheitJButton.addActionListener(
62
63 new ActionListener() // anonymous inner class
64 {
65 // event handler called when
66 // convertFahrenheitJButton is clicked
67 public void actionPerformed( ActionEvent event )
68 {
69 convertFahrenheitJButtonActionPerformed( event );
70 }
71
72 } // end anonymous inner class
73
74 ); // end call to addActionListener
75
76 // set up convertCelsiusJButton
77 convertCelsiusJButton = new JButton();
78 convertCelsiusJButton.setBounds( 208, 104, 162, 40 );
79 convertCelsiusJButton.setText( "Convert To Celsius" );
80 contentPane.add( convertCelsiusJButton );
81 convertCelsiusJButton.addActionListener(
82
83 new ActionListener() // anonymous inner class
84 {
85 // event handler called when
86 // convertCelsiusJButton is clicked
87 public void actionPerformed( ActionEvent event )
88 {
89 convertCelsiusJButtonActionPerformed( event );
90 }
91
92 } // end anonymous inner class
93
94 ); // end call to addActionListener
95
96 // set properties of application's window
97 setTitle( "Temperature Converter" ); // set title bar string
98 setSize( 396, 190 ); // set window size
99 setVisible( true ); // display window
100
101 } // end method createUserInterface
102
103 // converts degrees to Fahrenheit
104 private void convertFahrenheitJButtonActionPerformed(
105 ActionEvent event )
106 {
107 // format temperature value
108 DecimalFormat temperature = new DecimalFormat( "0.00" );
109
110 // retrieve input from user
111 double degreeCelsius = Double.parseDouble(
112 degreesJTextField.getText() );
113
114 // convert from Celsius to Fahrenheit
115 double convertedDegree = convertToFahrenheit( degreeCelsius );
116
117 // output converted temperature value
118 outputJTextField.setText( degreeCelsius +
119 " degrees Celsius is equal to " +
120 temperature.format( convertedDegree ) +
121 " degrees Fahrenheit." );
122
123 } // end method convertFahrenheitJButtonActionPerformed
124
125 // converts degrees to Celsius
126 private void convertCelsiusJButtonActionPerformed(
127 ActionEvent event )
128 {
129 // format temperature value
130 DecimalFormat temperature = new DecimalFormat( "0.00" );
131
132 // retrieve input from user
133 double degreeFahrenheit = Double.parseDouble(
134 degreesJTextField.getText() );
135
136 // convert from Fahrenheit to Celsius
137 double convertedDegree = convertToCelsius( degreeFahrenheit );
138
139 // output converted temperature value
140 outputJTextField.setText( degreeFahrenheit +
141 " degrees Fahrenheit is equal to " +
142 temperature.format( convertedDegree ) +
143 " degrees Celsius." );
144
145 } // end method convertCelsiusJButtonActionPerformed
146
147 // convert degree to Fahrenheit
148 private double convertToFahrenheit( double degree )
149 {
150 return ( 9.0 / 5.0 ) * degree + 32.0;
151
152 } // end method convertToFahrenheit
153
154 // convert degree to Celsius
155 private double convertToCelsius( double degree )
156 {
157 return ( 5.0 / 9.0 ) * ( degree - 32.0 );
158
159 } // end method convertToCelsius
160
161 // main method
162 public static void main( String[] args )
163 {
164 TemperatureConverter application = new TemperatureConverter();
165 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
166
167 } // end method main
168
169 } // end class TemperatureConverter
```