Week 4 Programming Assignment Write a Java GUI application to do temperature con
ID: 3543231 • Letter: W
Question
Write a Java GUI application to do temperature conversions between Celcius, Fahranheit, and Kelvin. The GUI display should look something like the following:
Your program must meet the following requirements:
1. Do not use any of the GUI editing capabilities of Eclipse for this assignment. Do all the GUI layout work based on what you have learned in class in the last 2 weeks.
2. The GUI and event handling setup should be done in the constructor of your GUI class or in private methods called from the constructor.
3. The display must have a label and JTextField where the user inputs a value which must appear in the upper part of the frame as shown above.
4. There should be a set of 3 radio buttons which indicate the input scale of the value to be converted. The 3 input scale buttons must be vertically aligned on the left side of the display as shown above.
5. There should be a set of 3 radio buttons which indicate the output scale to be converted to. The 3 output scale buttons must be vertically aligned and appear on the right side of the display as shown above.
6. Event handling must be setup so that selection of any input or output button causes an event which triggers the event handling code to determine which of 9 possible conversions is needed.
7. Event handling must also respond to the user hitting the enter key in the input textfield! For this event, the event handling code must also determine which conversion is needed based on which buttons are selected.
8. Display the conversion result in an output text field or in a JLabel which appears in the bottom part of the display as shown above. Output the degree symbol and output scale information as shown above. You can output a degree symbol by putting the character value 176 into a formatted string.
9. If a radio button is clicked when there is nothing in the input textfield, the event handler must display the string"No Inpu" in the output textfield.
10. Your program must accurately convert from Fahrenheit, Celcius, Kelvin to Fahrenheit, Celcius, Kelvin. Only the selected conversion is displayed in the output textfield.
11. You must create a separate class which does all the temperature conversion calculations. It should have 6 static methods, one for each possible conversion. Each of the 6 methods takes an input temperature and returns the appropriate converted temperature. This class could be used in any other application where temperature conversion is needed. You can find conversion formulas at the following website:
http://en.wikipedia.org/wiki/Temperature_conversion
12. When the conversion selection changes due to clicking either an input or output scale button, the output area must change to show only the new result.
13. HINT: For radio button events, use the ItemListener interface and use the isSelected method on the radio buttons to find out which buttons are turned on!
14. The output must display 2 digits after the decimal point.
Make sure that you test all your conversion formulas and the error case handling!!!
Also make sure that you add comments to your program!
Take screen shots of the program doing 2 different conversions and handling one of the error cases. Paste the screen shots and the source code for your program into a Word document.
/******************THIS IS WHAT I HAVE SO FAR****************************/
//TerperatureConversion.java
public class TerperatureConversion {
//define 6 static methods, one for each possible conversion
public static double fahrenheitToCelcius (double fahrenheit)
{
//[*c] = ([*F] - 32) + 5/9
/* double celcius;
// celcius = (fahrenheit -32 + 5.0 / 9.0);
return celcius; */
//or
return (fahrenheit - 32 + 5.0 / 9.0);
}//end fahrenheitToCelcius
public static double CelciusToFahrenheit(double input) {
// TODO Auto-generated method stub
return 0;
}
}
Explanation / Answer
// Program converts temperatures.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Convert extends JFrame
{
private JPanel convertFrom, convertTo;
private JLabel label1, label2, label3, label4;
private JTextField temperature1, temperature2;
private ButtonGroup radioFrom, radioTo;
private JRadioButton celciusBoxTo, fahrenheitBoxTo, kelvinBoxTo, celciusBoxFrom, fahrenheitBoxFrom, kelvinBoxFrom;
// set up
GUI public Convert()
{
super( "Temperature Conversion" );
fahrenheitBoxFrom = new JRadioButton( "Fahrenheit", true );
celciusBoxFrom = new JRadioButton( "Celcius", false );
kelvinBoxFrom = new JRadioButton( "Kelvin", false );
radioFrom = new ButtonGroup();
radioFrom.add( fahrenheitBoxFrom );
radioFrom.add( celciusBoxFrom );
radioFrom.add( kelvinBoxFrom );
fahrenheitBoxTo = new JRadioButton( "Fahrenheit", false );
celciusBoxTo = new JRadioButton( "Celcius", true );
kelvinBoxTo = new JRadioButton( "Kelvin", false );
radioTo = new ButtonGroup();
radioTo.add( fahrenheitBoxTo );
radioTo.add( celciusBoxTo );
radioTo.add( kelvinBoxTo );
convertFrom = new JPanel();
convertFrom.setLayout( new GridLayout( 1, 3 ) );
convertFrom.add( fahrenheitBoxFrom );
convertFrom.add( celciusBoxFrom );
convertFrom.add( kelvinBoxFrom );
convertTo = new JPanel();
convertTo.setLayout( new GridLayout( 1, 3 ) );
convertTo.add( fahrenheitBoxTo );
convertTo.add( celciusBoxTo );
convertTo.add( kelvinBoxTo );
label1 = new JLabel( "Convert from:" );
label2 = new JLabel( "Convert to:" );
label3 = new JLabel( "Enter Numeric Temperature: " );
label4 = new JLabel( "Comparable Temperature is: " );
temperature1 = new JTextField( 10 );
temperature1.addActionListener( new ActionListener()
{
// anonymous inner class //
perform conversions public void actionPerformed( ActionEvent event )
{
int convertTemp, temp;
temp = Integer.parseInt( ( ( JTextField ) event.getSource() ).getText() ); // fahrenheit to celcius
if ( fahrenheitBoxFrom.isSelected() && celciusBoxTo.isSelected() )
{
convertTemp = ( int ) ( 5.0f / 9.0f * ( temp - 32 ) );
temperature2.setText( String.valueOf( convertTemp ) );
}
// fahrenheit to kelvin
else if ( fahrenheitBoxFrom.isSelected() && kelvinBoxTo.isSelected() )
{
convertTemp = ( int ) ( 5.0f / 9.0f * ( temp - 32 ) + 273 );
temperature2.setText( String.valueOf( convertTemp ) );
}
// celcius to fahrenheit
else if ( celciusBoxFrom.isSelected() && fahrenheitBoxTo.isSelected() )
{
convertTemp = ( int ) ( 9.0f / 5.0f * temp + 32 );
temperature2.setText( String.valueOf( convertTemp ) );
}
// celcius to kelvin
else if ( celciusBoxFrom.isSelected() && kelvinBoxTo.isSelected() )
{
convertTemp = temp + 273;
temperature2.setText( String.valueOf( convertTemp ) );
}
// kelvin to celcius
else if ( kelvinBoxFrom.isSelected() && celciusBoxTo.isSelected() )
{
convertTemp = temp - 273;
temperature2.setText( String.valueOf( convertTemp ) );
}
// kelvin to fahrenheit
else if ( kelvinBoxFrom.isSelected() && fahrenheitBoxTo.isSelected() )
{
convertTemp = ( int ) ( 5.0f / 9.0f * ( temp - 273 ) + 32 );
temperature2.setText( String.valueOf( convertTemp ) );
}
}
// end method actionPerformed
} // end anonymous inner class );
//end call to addActionListener temperature2 = new JTextField( 10 );
temperature2.setEditable( false );
// add components to GUI Container container = getContentPane();
container.setLayout( new GridLayout( 8, 1 ) );
container.add( label1 );
container.add( convertFrom );
container.add( label3 );
container.add( temperature1 );
container.add( label2 );
container.add( convertTo );
container.add( label4 );
container.add( temperature2 );
setSize( 250, 225 );
setVisible( true );
} // end constructor
public static void main ( String args[] )
{
Convert application = new Convert(); application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
} // end class Convert
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.