From f4359524fbe0d00d51749e948e9b57820955a835 Mon Sep 17 00:00:00 2001 From: Aditya Sharma <114949288+adityaaa009@users.noreply.github.com> Date: Mon, 2 Oct 2023 18:46:39 +0530 Subject: [PATCH 1/2] Update TemperatureConverter.java MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This code creates a simple GUI with a text field for input and two buttons for conversion. When you click the “To Celsius” button, it converts the temperature from Fahrenheit to Celsius. When you click the “To Fahrenheit” button, it converts the temperature from Celsius to Fahrenheit. Please note that this is a basic example and does not include error handling for non-numeric or out-of-range input. You may want to add those in a production environment. --- src/TemperatureConverter.java | 55 ++++++++++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/src/TemperatureConverter.java b/src/TemperatureConverter.java index 716e04e..d685954 100644 --- a/src/TemperatureConverter.java +++ b/src/TemperatureConverter.java @@ -1,2 +1,55 @@ -public class TemperatureConverter { +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +public class TempConverter extends JFrame { + private JTextField textField; + private JButton celsiusButton; + private JButton fahrenheitButton; + + public TempConverter() { + createUI(); + } + + private void createUI() { + setDefaultCloseOperation(EXIT_ON_CLOSE); + setSize(300, 100); + setLocationRelativeTo(null); + + textField = new JTextField(10); + celsiusButton = new JButton("To Celsius"); + fahrenheitButton = new JButton("To Fahrenheit"); + + celsiusButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + int tempFahr = Integer.parseInt(textField.getText()); + textField.setText(String.valueOf((tempFahr - 32) * 5 / 9)); + } + }); + + fahrenheitButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + int tempCels = Integer.parseInt(textField.getText()); + textField.setText(String.valueOf(tempCels * 9 / 5 + 32)); + } + }); + + setLayout(new FlowLayout()); + add(textField); + add(celsiusButton); + add(fahrenheitButton); + } + + public static void main(String[] args) { + SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + new TempConverter().setVisible(true); + } + }); + } } + From 5d2de946e859e53e6c7c63812a5919c600c04adf Mon Sep 17 00:00:00 2001 From: Swarnodip Nag <77166101+Swarno-Coder@users.noreply.github.com> Date: Sun, 12 Oct 2025 16:00:20 +0530 Subject: [PATCH 2/2] Update src/TemperatureConverter.java Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/TemperatureConverter.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/TemperatureConverter.java b/src/TemperatureConverter.java index d685954..02459a1 100644 --- a/src/TemperatureConverter.java +++ b/src/TemperatureConverter.java @@ -24,16 +24,16 @@ private void createUI() { celsiusButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - int tempFahr = Integer.parseInt(textField.getText()); - textField.setText(String.valueOf((tempFahr - 32) * 5 / 9)); + double tempFahr = Double.parseDouble(textField.getText()); + textField.setText(String.format("%.2f", (tempFahr - 32) * 5.0 / 9.0)); } }); fahrenheitButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - int tempCels = Integer.parseInt(textField.getText()); - textField.setText(String.valueOf(tempCels * 9 / 5 + 32)); + double tempCels = Double.parseDouble(textField.getText()); + textField.setText(String.format("%.2f", tempCels * 9.0 / 5.0 + 32.0)); } });