RHS -> Allan->SWC -> Exercises -> TemperatureConverter

TemperatureConverter

In this exercise we'll extend the MyUtils class we made earlier - we'll add methods to convert a temperature from one temperature system to another.
But first we'll build the form to test the extensions.

Create the project "Temperature Converter" and create a "JFrame Form" - call it TempCalcTestForm.
Add a JPanel - extend it a little bit.

JRadioButton
We'll need a swing-component we haven't used before so first we'll experiment a little with that.
Add a JRadioButton and a JButton to the panel.

As we're only experimenting you can keep the default-names for the components given by NetBeans.

Your form should look something like:

     

Double-click on the button and in the method jButton1ActionPerformed() method add the following if-else statement:

if (jRadioButton1.isSelected()) {
  jRadioButton1.setText("Selected");
}
else {
  jRadioButton1.setText("Not selected");
}

Run (execute) the application.
Test how the text for the radiobutton changes as you click the button when the radiobutton is selected and when it is unselected.

Now you know what the method isSelected() (the method returns a boolean value) is used for in a JRadioButton.

ButtonGroup
But the magic doesn't stop here!
Add a JRadioButton more.
Add a ButtonGroup to your panel - note that this component is invisible. You can only see that it is added if you look in the Inspector - it is seen as a node under "Other Components".

Click on the first JRadioButton (jRadioButton1) so it is selected.
In the Properties section you'll find a property called "buttonGroup". The value is <none> but if you click on the blue arrow at the end of the value-field you can choose buttonGroup1 - do it!
Do the same for the second JRadioButton (jRadioButton2).

Add (keep the existing code) the following if-else statement to the code for the button:

if (jRadioButton2.isSelected()) {
  jRadioButton2.setText("Selected");
}
else {
  jRadioButton2.setText("Not selected");
}

Run the application and see what happens when you select or unselect the radiobuttons and click the button.

Note: When a number of radiobuttons is tied to a common buttongroup only one of them can be selected at the same time! So if you select one, all the others will be unselected automatically.

Before we move on we'll add some methods to the MyUtils class.

Convert between temperatures
Copy the MyUtils class to your project.

Add the following method:

public static double celcius2Fahrenheit(double c) {
  return (9.0 / 5.0) * c + 32;
}

This method implements the first of the following formulas (f is temperature in fahrenheit, c is temperature in celcius and r is temperature in reamur) :

  Celcius -> Fahrenheit  
  Fahrenheit -> Celcius  
  Celcius -> Reamur  
  Reamur -> Celcius  
  Fahrenheit -> Reamur  
  Reamur -> Fahrenheit  

Implement five more methods using the five other formulas to convert temperatures.
Remember to make the methods static - you know why!

Compile MyUtils.

Test the conversion methods
Now go back to the TempCalcTestForm and build a form like this

     

Note the following:

To make a button selected when the program starts, add a statement in the forms constructor like this: nameOfButton.setSelected(true); (you should of course use your own variable-name instead of "nameOfButton")

Here is an example of a calculation:

     

Maintained by: Allan Helboe
Updated: 28 November, 2007 15:57