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

This is the program that I have. I need for it to display the converted temperat

ID: 639590 • Letter: T

Question

This is the program that I have. I need for it to display the converted temperature to only two decimal places. I also need to have error handling setup so that if you select a conversion option before putting a value in the input window it will display a message that reads "No Input". This program is written in Java.

import java.awt.BorderLayout;

import java.text.DecimalFormat;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.*;

import javax.swing.ButtonGroup;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JRadioButton;

import javax.swing.JTextArea;

import javax.swing.JTextField;

public class TemperatureGUI implements ActionListener {

JFrame frame;

JLabel out1, in1, out2, in2;

JRadioButton celsin, celsout, fahrenin, fahrenout, kelvin, kelvout;

JPanel top, left, right, bottom;

JTextField textfield;

JTextArea area;

TemperatureGUI()

{

in1 = new JLabel("Input ");

textfield = new JTextField(10);

top = new JPanel();

top.add(in1);

top.add(textfield);

in2 = new JLabel("Input Scale");// Label for the Input buttons

celsin = new JRadioButton("Celsius (u00B0C)");

fahrenin = new JRadioButton("Fahrenheit (u00B0F)");

kelvin = new JRadioButton("Kelvin (u00B0K)");

ButtonGroup inputgroup = new ButtonGroup();// Groups the 3 input buttons

inputgroup.add(celsin); //only allows the user to select 1 input.

inputgroup.add(fahrenin);

inputgroup.add(kelvin);

left = new JPanel(new GridLayout(5, 1, 8, 5));// Aligns the label and buttons

left.add(in2);

left.add(celsin);

left.add(fahrenin);

left.add(kelvin);

out2 = new JLabel("Output Scale");// Label for the Output buttons

celsout = new JRadioButton("Celsius (u00B0C)");

fahrenout = new JRadioButton("Fahrenheit (u00B0F)");

kelvout = new JRadioButton("Kelvin (u00B0K)");

ButtonGroup outputgroup = new ButtonGroup();

outputgroup.add(celsout);

outputgroup.add(fahrenout);

outputgroup.add(kelvout);

right = new JPanel(new GridLayout(5, 1, 8, 5));

right.add(out2);

right.add(celsout);

right.add(fahrenout);

right.add(kelvout);

out1 = new JLabel("Output ");// Label for the Output answer after conversion

area = new JTextArea(1,10);// Text area to show the conversion of the input.

area.setEditable(false);

bottom = new JPanel();

bottom.add(out1);

bottom.add(area);

frame = new JFrame("Temperature Conversion");

frame.add(top, BorderLayout.NORTH);// places the input on top

frame.add(left, BorderLayout.WEST);// the input buttons will be to the left.

frame.add(right, BorderLayout.EAST);// the output buttons will be to the right.

frame.add(bottom, BorderLayout.SOUTH);// the final conversion output

// will be to the bottom.

textfield.addActionListener(this);

frame.setSize(300, 200);

frame.setVisible(true);

celsin.addActionListener(this);

fahrenin.addActionListener(this);

kelvin.addActionListener(this);

celsout.addActionListener(this);

fahrenout.addActionListener(this);

kelvout.addActionListener(this);

}

public void actionPerformed(ActionEvent act)

{

TempConvCalc cal = new TempConvCalc();

String s = textfield.getText();

  

if (s.length() > 0) {

double temps = Double.parseDouble(s);

if (celsin.isSelected() && fahrenout.isSelected())

{

area.setText(cal.CtoF(temps) + "u00B0F");

}

else if (celsin.isSelected() && kelvout.isSelected())

{

area.setText(cal.CtoK(temps) + "u00B0K");

}

if (fahrenin.isSelected() && celsout.isSelected())

{

area.setText(cal.FtoC(temps) + "u00B0C");

}

else if (fahrenin.isSelected() && kelvout.isSelected())

{

area.setText(cal.FtoK(temps) + "u00B0K");

}

if (kelvin.isSelected() && celsout.isSelected())

{

area.setText(cal.KtoC(temps) + "u00B0C");

}

else if (kelvin.isSelected() && fahrenout.isSelected())

{

area.setText(cal.KtoF(temps) + "u00B0F");

}

}

}

public static void main(String[] args)

{

TemperatureGUI app = new TemperatureGUI();

}

}

This is a second class for the main program

public class TempConvCalc

{

float input;

float res;

public double CtoF(double temps)

{

double res;

res = (temps * 1.80) + 32;

return res;

}

public double CtoK(double temps)

{

double res;

res = temps + 273.15;

return res;

}

public double FtoC(double temps)

{

double res;

res = ((temps - 32)/(1.80));

return res;

}

public double FtoK(double temps)

{

double res;

res = ((temps - 32)/(1.80)) + 273.15;

return res;

}

public double KtoC(double temps)

{

double res;

res = temps - 273.15;

return res;

}

public double KtoF(double temps)

{

double res;

res = (temps - 273.15)*1.80 + 32;

return res;

}

}

Explanation / Answer

use this snippet when you are reading

after you getting result..use math floor function

it displays only two decimal places

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote