0% found this document useful (0 votes)
5 views13 pages

Java Temperature Converter App

The document describes a Temperature Converter application developed using Java Swing, which allows users to convert temperatures between Celsius, Fahrenheit, and Kelvin. It emphasizes the use of GUI components, event handling, and method overloading, showcasing core Java programming concepts. The project aims to provide an interactive tool that combines mathematical formulas with user-friendly design, making it suitable for educational purposes.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views13 pages

Java Temperature Converter App

The document describes a Temperature Converter application developed using Java Swing, which allows users to convert temperatures between Celsius, Fahrenheit, and Kelvin. It emphasizes the use of GUI components, event handling, and method overloading, showcasing core Java programming concepts. The project aims to provide an interactive tool that combines mathematical formulas with user-friendly design, making it suitable for educational purposes.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

TEMPERATURE CONVERTER

COURSE NAME: JAVA PROGRAMMING LABORATORY


BACHELOR OF TECHNOLOGY

IN
ELECTRONICS AND COMMUNICATION
ENGINEERING

BY

ANANYA.P (22261A0404)
[Link] VARDHAN (22261A0426)
[Link] KUMAR (22261A0449)

1
Abstract
This project presents a Temperature Converter application developed
using Java Swing, designed to convert temperature values between
Celsius, Fahrenheit, and Kelvin. The purpose of the project is to
provide a simple, interactive, and user-friendly tool that demonstrates
the practical use of GUI components, event handling, and method
overloading in Java. The system consists of input fields, drop-down
unit selectors, and action buttons, allowing users to enter a temperature
value, choose the source and target units, and instantly obtain the
converted result. Three overloaded convert() methods are implemented
to handle conversions based on the selected input unit, thereby
showcasing polymorphism and clean code structure.

The application uses Swing components such as JFrame, JTextField,


JComboBox, JButton, and JLabel to create a structured interface, while
ActionListener manages user actions efficiently. Error handling is
included to ensure invalid numeric inputs are detected and reported.
The outcome is a functional and efficient desktop tool that performs
accurate temperature conversions and serves as a strong demonstration
of core programming concepts in Java. The project successfully
integrates GUI design with logical computation, making it suitable for
academic coursework and further enhancement.

2
Introduction
a. Background of the Topic
Temperature is a basic physical quantity used in science, engineering,
and daily life. Different systems use different temperature scales such
as Celsius, Fahrenheit, and Kelvin, which makes conversion between
these units important. In programming education, creating a
temperature converter is a useful exercise because it combines
mathematical formulas with user interaction and GUI development.
This project uses Java Swing to build a small application that performs
these conversions in an easy and efficient way.

b. Objective of the Project


The objective of this project is to develop a functional Temperature
Converter application that allows users to convert values between
Celsius, Fahrenheit, and Kelvin. The project aims to create a simple
graphical interface using Java Swing where users can enter a
temperature, select the units, and get an accurate result instantly. It also
focuses on applying key Java concepts such as event handling and
method overloading to perform conversions efficiently. Overall, the
project’s objective is to combine basic temperature conversion
formulas with GUI programming to produce an easy-to-use and
educational software tool.

3
System Design / Methodology

a. Tools and Technologies Used

1. Java Programming Language

o Used to implement the core temperature conversion logic using


method overloading.

o Provides object-oriented features required for building a


structured and reusable program.

2. Java Swing

o Used to design the graphical user interface with components like


JFrame, JButton, and JTextField.

o Allows creating an interactive window for user input and


displaying results.

3. Java AWT

o Used for event handling through ActionListener and


ActionEvent to detect button actions.

o Provides layout managers like GridLayout to arrange GUI


components neatly.

4. Java Development Kit (JDK)

Required for compiling and running the Java Swing application.

4
b. Flow Chart

5
Implementation and Results
a. Code
import [Link].*;
import [Link].*;
import [Link].*;

public class TemperatureConverterGUI extends JFrame implements


ActionListener {

private JTextField inputField, resultField;


private JComboBox<String> fromUnit, toUnit;
private JButton convertBtn, clearBtn;

public TemperatureConverterGUI() {

setTitle("Temperature Converter");
setSize(350, 220);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(5, 2, 10, 10));
setLocationRelativeTo(null);

String[] units = {"Celsius", "Fahrenheit", "Kelvin"};

inputField = new JTextField();


resultField = new JTextField();

6
[Link](false);

fromUnit = new JComboBox<>(units);


toUnit = new JComboBox<>(units);

convertBtn = new JButton("Convert");


clearBtn = new JButton("Clear");

[Link](this);
[Link](this);

add(new JLabel("From:"));
add(fromUnit);
add(new JLabel("To:"));
add(toUnit);
add(new JLabel("Enter Value:"));
add(inputField);
add(new JLabel("Result:"));
add(resultField);
add(convertBtn);
add(clearBtn);

setVisible(true);
}
// Celsius → other units
public double convert(double value, String to) {

7
if ([Link]("Fahrenheit")) return (value * 9/5) + 32;
if ([Link]("Kelvin")) return value + 273.15;
return value;
}

// Fahrenheit → other units


public double convert(String from, double value) {
if ([Link]("Celsius")) return (value - 32) * 5/9;
if ([Link]("Kelvin")) return (value - 32) * 5/9 + 273.15;
return value;
}

// Kelvin → other units


public double convert(double value, String from, boolean dummy)
{
if ([Link]("Celsius")) return value - 273.15;
if ([Link]("Fahrenheit")) return (value - 273.15) * 9/5 + 32;
return value;
}
@Override
public void actionPerformed(ActionEvent e) {

if ([Link]() == convertBtn) {
try {
double input = [Link]([Link]());
String from = (String) [Link]();

8
String to = (String) [Link]();
double result;

if ([Link]("Celsius")) {
result = convert(input, to);
} else if ([Link]("Fahrenheit")) {
result = convert(to, input);
} else {
result = convert(input, to, true);
}

[Link]([Link]("%.2f", result));

} catch (Exception ex) {


[Link](this, "Invalid Input!");
}
}
if ([Link]() == clearBtn) {
[Link]("");
[Link]("");
}
}
public static void main(String[] args) {
new TemperatureConverterGUI();
}
}

9
Explanation
[Link] the GUI Window

The class extends JFrame to create the main application window and
implements ActionListener to handle button clicks. In the
constructor, the frame title, size, layout (GridLayout), and close
operation are set, and the window is centered on the screen.

2. Adding Input and Output Components

Two text fields are created:

o inputField for entering the temperature

o resultField for displaying the converted result (set as non-


editable)

Two combo boxes, fromUnit and toUnit, contain the temperature


units Celsius, Fahrenheit, and Kelvin.

3. Adding Buttons and Registering Events

Two buttons, Convert and Clear, are added. Each button is connected
to the event handler using addActionListener(this). All button
actions are processed inside the actionPerformed() method.

4. Handling Convert Button Click

When the Convert button is pressed, the program reads the value
from the input field and converts it to a number using
[Link](). If the input is invalid, an error message is
displayed using JOptionPane.

10
5. Applying Method Overloading for Conversion

Three overloaded convert() methods are used to perform


temperature conversions:

o convert(double value, String to) – for Celsius


o convert(String from, double value) – for Fahrenheit
o convert(double value, String from, boolean dummy) – for
Kelvin

Based on the selected "From" unit, the correct overloaded method is


executed.

6. Displaying the Converted Result

The converted temperature is formatted to two decimal places using


[Link]("%.2f", result) and displayed in the result text field.

7. Handling the Clear Button

When the Clear button is pressed, both the input and result fields are
cleared, allowing the user to enter a new temperature value.

11
b. Results Achieved

12
Conclusion
The Temperature Converter application successfully demonstrates how
Java Swing can be used to build an interactive and user-friendly GUI-
based program. Through the use of components such as text fields,
combo boxes, and buttons, the project allows users to easily convert
temperatures between Celsius, Fahrenheit, and Kelvin. The
implementation also showcases the concept of method overloading,
where multiple versions of the convert() method handle different types
of temperature conversions efficiently.

Overall, this project highlights effective event handling, proper input


validation, and a clear separation of GUI design and logic. It serves as
a practical example of applying core Java principles to create a
functional desktop application, while strengthening understanding of
graphical interfaces and object-oriented programming concepts.

13

You might also like