Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Java GUI application to do temperature conversions between Celcius, Fahr

ID: 639261 • 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 Input? 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.

Input 100 Input Scale Output Scale Celcius O Celcius Fahrenheit OKelvin Fahrenheit OKelvin Output 212.00 F

Explanation / Answer

public class Converter {
public Converter()
{

}

// Method to convert from degrees Celcius to degrees Fahrenheit
public static float celciusToFahrenheit(float degCelcius)
{
float degFahrenheit;
degFahrenheit = degCelcius * 9/5 + 32;
return degFahrenheit;
}

// Method to convert from degrees Fahrenheit to degrees Celcius
public static float fahrenheitToCelcius(float degFahrenheit)
{
float degCelcius;
degCelcius = (degFahrenheit - 32) * 5/9;
return degCelcius;
}

// Method to convert from degrees Celcius to degrees Kelvin
public static float celciusToKelvin(float degCelcius)
{
float degKelvin;
degKelvin = degCelcius + 273.15f;
return degKelvin;
}

// Method to convert from degrees Kelvin to degrees Celcius
public static float kelvinToCelcius(float degKelvin)
{
float degCelcius;
degCelcius = degKelvin - 273.15f;
return degCelcius;
}

// Main method demonstrating usage of above methods
public static void main(String[] args)
{
System.out.print("100 degrees Celcius in Fahrenheit is: ");
System.out.println(celciusToFahrenheit(100));

System.out.print("-40 degrees Fahrenheit in Celcius is: ");
System.out.println(fahrenheitToCelcius(-40));

System.out.print("0 degrees Celcius in Kelvin is: ");
System.out.println(celciusToKelvin(0));

// to convert from Kelvin to Fahrenheit, combine two methods:
System.out.print("373.15 degrees Kelvin in Fahrenheit is: ");
System.out.println(celciusToFahrenheit(kelvinToCelcius(373.15f)));
}
}

one more example for u:

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.text.DecimalFormat;

@SuppressWarnings("serial")

public class ConvertTemp extends JFrame

implements ActionListener {

private JButton button, button2, button3, button4, button5, button6;

@SuppressWarnings("unused")

private JPanel panel;

@SuppressWarnings("unused")

private JFrame frame;

@SuppressWarnings("unused")

private JTextField Text;

@SuppressWarnings("unused")

private JLabel label;

private JTextField infield = new JTextField(10);

private JTextField outfield = new JTextField(10);

private JLabel inlabel = new JLabel("Temperature Value:");

private JLabel outlabel = new JLabel("Converted Temperature Value:");

DecimalFormat three = new DecimalFormat("0.000");//sets up the 3 Decimal places for use on the output

public static void main(String[] args) {

ConvertTemp frame = new ConvertTemp();

frame.createFrame();

frame.createButton1();

frame.createButton2();

frame.createButton3();

frame.createButton4();

frame.createButton5();

frame.createButton6();

frame.addLabels();

}

public void createFrame() {

Container window = getContentPane();

window.setLayout(new FlowLayout() ); //using the FlowLayout manager

setSize(400, 200); //setting the size of the initial box

setVisible(true); //allows for manual size changing of the box

setTitle("Temperature Converter");

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

panel = new JPanel(new GridLayout());

add(new JLabel("Temperature Conversions" + " C: Celsius" + " F: Farhrenheit"+ " K: Kelvin"));

}

public void createButton1() {

//this is to create the Fahrenheit to Celsius button

button = new JButton("F to C");

add(button);

button.addActionListener((ActionListener) this);//this allows an action to be performed when the button is pressed

}

public void createButton2() {

//this is to create the Fahrenheit to Kelvin button

button2 = new JButton("F to K");

add(button2);

button2.addActionListener((ActionListener) this);//this allows an action to be performed when the button is pressed

}

public void createButton3() {

//this is to create the Celsius to Kelvin button

button3 = new JButton("C to K");

add(button3);

button.addActionListener((ActionListener) this);//this allows an action to be performed when the button is pressed

}

public void createButton4() {

//this is to create the Celsius to Fahrenheit button

button4 = new JButton("C to F");

add(button4);

button4.addActionListener((ActionListener) this);//this allows an action to be performed when the button is pressed

}

public void createButton5() {

//this is to create the Kelvin to Celsius button

button5 = new JButton("K to C");

add(button5);

button5.addActionListener((ActionListener) this);//this allows an action to be performed when the button is pressed

}

public void createButton6() {

//this is to create the Kelvin to Fahrenheit button

button6 = new JButton("K to F");

add(button6);

button6.addActionListener((ActionListener) this);//this allows an action to be performed when the button is pressed

}

public void addLabels() {
1
add(inlabel);

add(infield);

add(outlabel);

add(outfield);

outfield.setEditable(false);//removes ability to enter a value in the output field

}

public void actionPerformed(ActionEvent i) {

int temp;

double newtemp = 0;

String inputString;

inputString = infield.getText();//pulls the number that was entered in the input field

temp = Integer.parseInt(inputString);// makes a new variable to handle the input

//tells the output how to calculate the input based on the button that was pressed

if(i.getSource() == button) {

newtemp = (5/9) * (temp - 32);//Fahrenheit to Celsius equation

}

else if(i.getSource() == button2) {

newtemp = (5/9) * (temp - 32) + 273;//Fahrenheit to Kelvin equation

}

else if(i.getSource() == button3) {

newtemp = temp + 273;//Celsius to Kelvin equation

}

else if(i.getSource() == button4) {

newtemp = ((9/5) * temp) + 32;//Celsius to Fahrenheit equation

}

else if(i.getSource() == button5) {

newtemp = temp - 273;//Kelvin to Celsius equation

}

else {

newtemp = ((temp - 273) * (9/5)) + 32;//Kelvin to Fahrenheit equation

}

outfield.setText(" "+ three.format(newtemp));

}

}