Temperature Converter Using Java

by | Apr 13, 2023 | Basic Coding, Coding, Java

Home » Coding » Temperature Converter Using Java

Introduction of the Project

Java is a popular programming language known for its versatility and wide range of applications. One common programming exercise for beginners is creating a temperature converter, which takes a temperature input in one unit and converts it to another. In this project tutorial, we will explore how to develop a temperature converter using Java.

A temperature converter is a simple program that allows users to input a temperature value in one unit, such as Celsius, Fahrenheit, or Kelvin, and then convert it to another unit based on a predefined conversion formula. For example, users may input a temperature value in Celsius, and the program will convert it to Fahrenheit or Kelvin, depending on the desired output.

To develop a temperature converter in Java, we will need to understand basic programming concepts, such as data types, input/output, and conditional statements. We will also need to define the conversion formulas for each temperature unit and implement them in our program. Additionally, we will create a user-friendly interface using the Swing module for inputting temperature values and displaying the converted result.

We will walk through the step-by-step process of developing a temperature converter using Java, starting from designing the user interface, implementing the conversion formulas, handling user input and output, and testing the program. By the end of this project tutorial, you will have a basic understanding of how to develop a temperature converter using Java and be able to apply the concepts learned to other programming projects. So, let’s dive in and start building our temperature converter!

 

Objectives

1. To implement the right mathematics formula such that the temperature can be accurately converted from one unit to another.

2. To create a user interface with buttons and graphics for the user to interact with our temperature converter module.

Requirements

Before you start building a temperature converter using Java, you will need to have the following prerequisites:

1. Basic Knowledge of Java: You should have a basic understanding of Java programming concepts, including data types, variables, operators, conditional statements (such as if-else statements), loops (such as for or while loops), and basic input/output operations (such as using the Scanner class).

2. Java Development Environment: You will need to have a Java Development Kit (JDK) installed on your computer, which includes the Java compiler (javac) and Java runtime environment (JRE). You can download the JDK from the official Oracle Java website and follow the installation instructions.

3. Integrated Development Environment (IDE): While not strictly necessary, it is recommended to use an Integrated Development Environment (IDE) for Java development, such as Eclipse, IntelliJ IDEA, or NetBeans. We have used IntelliJ IDEA for our project

4. Understanding of Temperature Units and Conversion Formulas: You should have a basic understanding of different temperature units, such as Celsius, Fahrenheit, and Kelvin, and the conversion formulas between them. For example, the formula to convert Celsius to Fahrenheit is:

F = (C * 9/5) + 32

where F is the temperature in Fahrenheit and

C is the temperature in Celsius.

Understanding these conversion formulas will be essential in implementing the logic for the temperature converter.

5. Familiarity with Graphic User Interface (GUI) Design: Java’s Swing library, which is used for building graphical user interfaces (GUIs), will be required to design a user-friendly interface for inputting temperature values and displaying the converted results in a visually appealing manner.

Source Code

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Main extends JFrame
{
JTextField tempinput, tempoutput;
ButtonListener buttonListener;
JLabel inputlabel,convertlabel,outputlabel,imglabel;
JComboBox comboBox1, comboBox2;
public Main()
{
Container c = getContentPane();
c.setLayout(null);
c.setBackground(Color.WHITE);
setTitle("TEMPERATURE CONVERTER");
setSize(400,300);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
String s1[] = {"Celsius", "Fahrenheit", "Kelvin"};
comboBox1 = new JComboBox(s1);
comboBox1.setBounds(200,20,90,20);
add(comboBox1);
String s2[] = {"Celsius", "Fahrenheit", "Kelvin"};
comboBox2 = new JComboBox(s2);
comboBox2.setBounds(100,90,90,20);
add(comboBox2);
inputlabel=new JLabel("Temperature:");
inputlabel.setFont(new Font("arial",Font.BOLD,14));
inputlabel.setSize(270,20);
inputlabel.setLocation(0,20);
convertlabel = new JLabel("Convert to: ");
convertlabel.setFont(new Font("arial",Font.BOLD,14));
convertlabel.setSize(270,20);
convertlabel.setLocation(0,90);
tempinput = new JTextField(10);
tempinput.setSize(40,20);
tempinput.setLocation(120,20);
tempoutput = new JTextField(10);
tempoutput.setSize(120,20);
tempoutput.setLocation(90,190);
outputlabel = new JLabel("Output:");
outputlabel.setFont(new Font("arial",Font.BOLD,14));
outputlabel.setSize(270,20);
outputlabel.setLocation(0,190);
JButton button =new JButton("Convert");
button.setSize(80,30);
button.setLocation(130,130);
button.setBackground(Color.LIGHT_GRAY);
buttonListener = new ButtonListener();
button.addActionListener(buttonListener);
imglabel = new JLabel("");
imglabel.setIcon(new ImageIcon("images.png"));
imglabel.setBounds(190,30,438,200);
c.add(outputlabel);
c.add(convertlabel);
c.add(inputlabel);
c.add(tempinput);
c.add(tempoutput);
c.add(button);
c.add(comboBox1);
c.add(comboBox2);
c.add(imglabel);
tempoutput.setEditable(false);
}
private class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String temp = (String) comboBox1.getSelectedItem();
String tempConvert = (String) comboBox2.getSelectedItem();
if(temp.equals("Celsius") && tempConvert.equals("Fahrenheit")){
double c = Double.parseDouble(tempinput.getText());
double f = (double) (c*1.8+32);
tempoutput.setText(String.valueOf(f));
}
else if (temp.equals("Celsius") && tempConvert.equals("Kelvin")) {
double c = Double.parseDouble(tempinput.getText());
double k = (double) (c+273);
tempoutput.setText(String.valueOf(k));
}
else if (temp.equals("Celsius") && tempConvert.equals("Celsius")) {
double c = Double.parseDouble(tempinput.getText());
tempoutput.setText(String.valueOf(c));
}
if(temp.equals("Fahrenheit") && tempConvert.equals("Celsius")) {
double f = Double.parseDouble(tempinput.getText());
double c = (double)((f - 32)*5/9);
tempoutput.setText(String.valueOf(c));
}
else if(temp.equals("Fahrenheit") && tempConvert.equals("Kelvin")) {
double f = Double.parseDouble(tempinput.getText());
double k = (double)((f - 32)*5/9 + 273.15);
tempoutput.setText(String.valueOf(k));
}
else if(temp.equals("Fahrenheit") && tempConvert.equals("Fahrenheit")) {
double f = Double.parseDouble(tempinput.getText());
tempoutput.setText(String.valueOf(f));
}
if(temp.equals("Kelvin") && tempConvert.equals("Fahrenheit")) {
double k = Double.parseDouble(tempinput.getText());
double f = (double) ((k*(9/5))-459.67);
tempoutput.setText(String.valueOf(f));
}
else if (temp.equals("Kelvin") && tempConvert.equals("Kelvin")) {
double k = Double.parseDouble(tempinput.getText());
tempoutput.setText(String.valueOf(k));
}
else if (temp.equals("Kelvin") && tempConvert.equals("Celcius")) {
double k = Double.parseDouble(tempinput.getText());
double c = (double) (k-273);
tempoutput.setText(String.valueOf(c));
}
}
}
public static void main(String[] args)
{
new Main();
}
}

Explanation of the Code

As per the defined objectives, we can break the code into 2 parts. One involves creating the GUI, and the other is the temperature conversion logic.

Let us look at the GUI first:

1. For the interface, we will create two text fields. One field to input a temperature in a certain unit, and the other text field will be an output showing the converted temperature in another unit.

2. We will create one button also using Swing. This Button will be the ‘Convert’ button that will allow users to convert temperature from one unit to another.

3. We will also have two combo boxes. Combo boxes are essentially drop-down lists where a user can select one option from several options. One combo box will allow the user to choose the unit of the input and the other combo box, the unit of the output to which the temperature will be converted.

Moving to the temperature conversion, we have applied the following rules:

1. If the input temperature is in Fahrenheit, we will use (f – 32)*(5/9) to convert to Celsius and (f – 32)*(5/9) + 273.15) to convert to Kelvin.

2. If the input temperature is in Celsius, we will use (c*1.8 + 32) to convert to Fahrenheit and (c + 273) to convert to Kelvin.

3. If the input temperature is in Kelvin, we will use [(k*(9/5)) – 459.67] to convert to Fahrenheit and (k-273) to convert to Celsius.

4. If the conversion unit is the same as the input unit, then we will simply print the input.

Output

Celsius to Fahrenheit

Fahrenheit to Kelvin

Temperature Converter Using Java - Fahrenheit to Kelvin

We have successfully built a Temperature converter using Java. This is a robust code with an easy-to-use graphic interface, as you can convert from any temperature unit to a different unit accurately using this Java application!

 

More Java Projects>>>

You May Also Like To Create…

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *