Question

In: Computer Science

Question 2: Create a very simple temperature converter form having two text fields. The first one...

Question 2: Create a very simple temperature converter form having two text fields. The first one is where the user will enter temperature. The second field is where the computed temperature value is displayed depending on the unit (F or C).

Solutions

Expert Solution

Things to consider:

  • Here, first of all, we have created all the required fields like labels and text fields.
  • After that, whenever when we enter some value to any of the fields either Celsius or Fahrenheit, we are using the formula to convert to the other as can be seen in the program for the conversion.
  • Whatever the result is, we are setting it to the corresponding text field.
  • Note: Please read the program line by line for better understanding.

Please refer to the comments of the program for more clarity.

// Importing required packages
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

// Extending the JFrame for adding the text fields
public class TempConversion extends JFrame
{
    // initializing the constants
    private static final int WINDOW_WIDTH = 500;
    private static final int HEIGHT = 80;
    private static final double far_to_c = 5.0/9.0;
    private static final double c_to_far = 9.0/5.0;
    private static final int Factor = 32;
    
    // initializing the labels
    private JLabel cLabel;
    private JLabel fLabel;

    // initializing the text fields
    private JTextField cTF;
    private JTextField fTF;
    
    // // initializing the handlers
    private CHandler cHandler;
    private FHandler fHandler;
    
    
    // Function to convert the Celsius to Fahrenheit and vice versa
    public TempConversion()
    {

        // Setting the title
        setTitle("Temperature Converter");
        Container c = getContentPane();

        // Setting the layout
        c.setLayout(new GridLayout(1,4));

        // Providing values to the labels
        cLabel = new JLabel("Temp in C: ",
                SwingConstants.RIGHT);
        fLabel = new JLabel("Temp in F: ",
                SwingConstants.RIGHT);

        // Defining the size of the text field
        cTF = new JTextField(7);
        fTF = new JTextField(7);

        // Adding the components to the container
        c.add(cLabel);
        c.add(cTF);
        c.add(fLabel);
        c.add(fTF);

        // Initializing the handlers
        cHandler = new CHandler();
        fHandler = new FHandler();

        cTF.addActionListener(cHandler);
        fTF.addActionListener(fHandler);

        // Setting the windows size
        setSize (WINDOW_WIDTH, HEIGHT);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    // Handler implementing the Action Listener
    private class CHandler implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            double c, f;

            // Logic of the conversion
            c = Double.parseDouble(cTF.getText());
            f = c * c_to_far + Factor;
            fTF.setText(String.format("%.2f", f));
        }
    }

    // Handler for Fahrenheit implementing the Action Listener
    private class FHandler implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            double c, f;

            // Logic for conversion
            f = Double.parseDouble(fTF.getText());
            c = (f - Factor) * far_to_c;
            cTF.setText(String.format("%.2f", c));
        }
    }


    // Main method or the starting point of the program.
    public static void main(String[] args)
    {
        TempConversion t = new TempConversion();
        t.setLocationRelativeTo(null);
    }
}
 

Sample Input-Output/CodeRun - 1:

Sample Input-Output/CodeRun - 2:

Please let me know in the comments in case of any confusion. Also, please upvote if you like.


Related Solutions

Create a very simple temperature converter form having two text fields. The first one is where...
Create a very simple temperature converter form having two text fields. The first one is where the user will enter temperature. The second field is where the computed temperature value is displayed depending on the unit (F or C).use html and javascript
Create an HTML form with the following: 2 text input fields 1 text area 2 radio...
Create an HTML form with the following: 2 text input fields 1 text area 2 radio buttons or 3 checkboxes 1 button Include labels for fields
Using PHP and MySQL Create a simple web form with at least 5 fields Include validation...
Using PHP and MySQL Create a simple web form with at least 5 fields Include validation and error messages Create a MySQL Database Create a table to store submissions from the form Only insert new data into the database when all validation is passed Use PHP to create an HTML table showing all the content of the database   New submissions should appear in table
Create a Java Swing form that will have two Buttons: (1) the first linked to an...
Create a Java Swing form that will have two Buttons: (1) the first linked to an event handler to write data from two TextFields on the form to an output file; and (2) the other with an event handler to read data from the same file as input and write output to a binary ObjectOutputStream file.
Hey. I am having trouble with a finance question: ----------------------------------------------------------------------------------------------------------------------------------------- Consider two bonds. The first is...
Hey. I am having trouble with a finance question: ----------------------------------------------------------------------------------------------------------------------------------------- Consider two bonds. The first is a 6% coupon bond with six years to maturity, and a yield to maturity of 4.5% annual rate, compounded semi-annually. The second bond is a 2% coupon bond with six years to maturity and a yield to maturity of 5.0%, annual rate, compounded semi-annually. Given the data for the first two bonds, now consider a third bond: a zero coupon bond with six years...
create T-accounts and balance sheet for the following question (this is one of several transactions) First...
create T-accounts and balance sheet for the following question (this is one of several transactions) First quarter of 2017: The firm (1) sells the 200 standard mugs in inventory to a customer who pays cash and picks up the mugs from the firm’s storage area and (2) receives an order for 8000 standard mugs. The supplier agrees to produce the mugs and ship directly to the customer; the firm agrees to pay the shipping cost ($390 for 8,000 mugs) to...
Using Visual Basic 2017, Create a form with two textboxes and one button. A user enters...
Using Visual Basic 2017, Create a form with two textboxes and one button. A user enters the course she is taking in the first text box and then clicks on the button. Then if the user entered "Program Language" in the first text box, the second text box will show "Awesome pick!". If the user entered something else, the text box shows "Ok." Please write the correct code.
putting two arguments into standard form, one for a 'yes' answer to your topic question and...
putting two arguments into standard form, one for a 'yes' answer to your topic question and one for a 'no' argument to your topic question. are going to do the Covid Confidentiality question, then you need two arguments with the following conclusions: 'Yes' Argument one two So, it's morally permissible for the university to reveal the identity of the Covid-positive employee 'No' Argument one two So, it's morally wrong for the university to reveal the identity of the Covid-positive employee...
Question 2. Consider a simple economy with two types of workers. There are capable workers (type...
Question 2. Consider a simple economy with two types of workers. There are capable workers (type A), constituting 70% of the population, with remaining workers (type B) being of average ability. There are two types of job opportunities as well, labeled Good and Bad. In a Bad job, either type of worker produces 20 units of output. In a Good job, Type A worker produces 80 units, and the Type B worker produces 0. The economy is doing well so...
Question 2. Consider a simple economy with two types of workers. There are capable workers (type...
Question 2. Consider a simple economy with two types of workers. There are capable workers (type A), constituting 70% of the population, with remaining workers (type B) being of average ability. There are two types of job opportunities as well, labeled Good and Bad. In a Bad job, either type of worker produces 20 units of output. In a Good job, Type A worker produces 80 units, and the Type B worker produces 0. The economy is doing well so...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT