Question: Java Homework 05 I mprove the performance of the application TConverter developed in Lab 06 (TConverter_dynamic.zip), in such a way that the user can insert
Java Homework 05
Improve the performance of the application TConverter developed in Lab 06 (TConverter_dynamic.zip), in such a way that the user can insert the temperature in Celsius scale or in Fahrenheit scale and get the corresponding temperature in the other scale by clicking on the button Convert. After showing the result for the temperature conversion, if the button Convert is clicked-on again, all input-output text-fields in the GUI will be cleared. Additionally add a button Reset for the same purpose.
Note 2: The application must be robust to wrong inputs. If the input inserted is wrong, the app must show a warning in a message dialog and additionally all the input-output text-fields in the GUI will be cleared.
Concepts in practice: Event-driven Programming, Java GUI, Java package SWING.
Files to be used:
//T and TConverter are the files found in the TConverter_dynamic.zip
//T
package TConverter;
public class T{ private double c; private double f; public T(double c, double f) { this.c = c; this.f = f; }
public double getCelsius() { double c = (f-32)*5/9; //formula from fahrenheit to celsius return c; }
public double getFarenheits() { double f = ((c*9/5)+32); //formula from celsius to fahrenheit return f; }
}
//T Converter
package TConverter;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.TitledBorder;
public class TConverter extends JFrame {
private JTextField c= new JTextField();
private JTextField f= new JTextField();
private JButton button = new JButton("Convert");
public TConverter() {
JPanel p1 = new JPanel(new GridLayout(2, 2)); //set rows and columns
p1.add(new JLabel("Celsius"));
p1.add(c);
p1.add(new JLabel("Fahrenheit"));
p1.add(f);
p1.setBorder(new TitledBorder("Convert C2F or F2C"));
JPanel p3 = new JPanel(new FlowLayout(FlowLayout.CENTER)); //center button
p3.add(button);
// Add the panels to the frame
add(new JPanel(), BorderLayout.NORTH);
add(p1, BorderLayout.CENTER);
add(p3, BorderLayout.SOUTH);
button.addActionListener(new ButtonListener());
}
private class ButtonListener implements ActionListener{
public void actionPerformed(ActionEvent e){
try
{
double c_input=Double.parseDouble(c.getText());
T t=new T(c_input,0);
f.setText(String.format("%.1f",t.getFarenheits()));
}
catch(Exception ex ){}
}
}
public static void main(String[] args) {
TConverter frame = new TConverter();
frame.setTitle("TConvert");
frame.setLocationRelativeTo(null); // Center the frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // close when click on x
frame.setVisible(true);
frame.setSize(200,150);
frame.setResizable(false);
}
}
Files to deliver:
TConverter_homework07.zip including; TConvereter.java, T.java and TConverter.jar files.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
