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

Java help - GUI application Write a GUI application that converts Celsius temper

ID: 3570213 • Letter: J

Question

Java help - GUI application

Write a GUI application that converts Celsius temperatures to Fahrenheit temperatures. The user should be able to enter a Celsius temperature, click a button, and then see the equivalent Fahrenheit temperature. Use the following formula to make the conversion ( F=9/5 C + 32) where F is the Fahrenheit temperature and C is the Celsius temperature.

I think (hope) I am close but I just cannot seem to get this to work.


import javax.swing.*; // Needed for swing class
import java.awt.event.*; // Needed for ActionListener Interface
import java.awt.*;


/**
The TempConversion class will ask you the temperature in Celsius.
When the calculate button is clicked, a dialog box will appear with
the temperature converted to Fahrenheit.
*/
public class TempConversion extends JFrame
{
   private JPanel panel;                   // To reference a panel
   private JLabel inputLabel;               // To reference a label
   private JLabel outputLabel;               // Display the temperature in Fahrenheit
   private JTextField celsius;               // To reference a text field
   private JTextField fahrenheit;           // Reference Fahrenheit field
   private JButton calcButton;               // To reference a button
   private final int WINDOW_WIDTH = 310;   // Window Width
   private final int WINDOW_HEIGHT = 100;   // Window Height

   /**
   Constructor
   */

   public TempConversion()
   {
       // Set the window title.
       setTitle("Temperature Converter");

       // Set the size of the window.
       setSize(WINDOW_WIDTH, WINDOW_HEIGHT);

       // Declare what happens when the close button is clicked.
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

       // Build a panel and add it to the frame.
       buildPanel();

       // Add the panel to the frame's contect pane.
       add(panel);

       // Display the window.
       setVisible(true);
   }

   /** The buildPanel method adds a label, a text field, and a button to a panel.
   */

   private void buildPanel()
   {
       // Create a label to display the instructions.
       inputLabel = new JLabel("Enter a Temperature in Celsius");
       // Create a label for the temperature output.
       outputLabel = new JLabel("The Temperature in Fahrenheit is");
       // Create a text field 5 characters wide.
       celsius = new JTextField(5);
       fahrenheit = new JTextField(5);
       // Create a "Calculate" button.
       calcButton = new JButton("Calculate");

       // Need to add an action listener to the button.
       calcButton.addActionListener(new calcButtonListener());

       // Create a JPanel object and let the panel field reference it.
       panel = new JPanel();

       // Add the label, text field, and buttons to panel.
       panel.add(inputLabel);
       panel.add(outputLabel);
       panel.add(celsius);
       panel.add(fahrenheit);
       panel.add(calcButton);
   }

   /** Create the Action Listener for the Calculate button. */

   private class calcButtonListener implements ActionListener
   {
       /** The actionPerformed executes when you click Calculate
       @param event The event object.
       */

       public void actionPerformed(ActionEvent event)
       {
           int fahrenheit, celsius;

           String text = celsius.getText();

           celsius = Integer.parseInt (text);
           fahrenheit = (celsius+32) * 5/9;

           JOptionPane.showMessageDialog(Interger.toString + " Celsius is " + (fahrenheit) + "degrees Fahrenheit.");
       }
   }
   /** Main method
   */
   public static void main(String[] args)
   {
       new TempConversion();
   }
}

Here are my errors:

java:89: error: int cannot be dereferenced            String text = celsius.getText();
                                 ^
.java:94: error: cannot find symbol            JOptionPane.showMessageDialog(Interger.toString + " Celsius is " + (fahrenheit) + "degrees Fahrenheit.");
                                          ^
symbol:   variable Interger
location: class TempConversion.calcButtonListener
2 errors

Tool completed with exit code 1

Explanation / Answer

// Author: import java.awt.BorderLayout; import java.awt.FlowLayout; import java.awt.GridLayout; import javax.swing.JTextField; import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; public class ConvertTemp extends JFrame { // instance variables JLabel labelFahr; JLabel labelCels; JTextField textFahr; JTextField textCels; JButton conFahToCel; JButton conCelToFah; public ConvertTemp () { super ("Temerature"); setLayout (new FlowLayout ()); labelFahr = new JLabel ("Fahrenheit: ", SwingConstants.LEFT); labelFahr.setToolTipText("This is a temerature scale"); add (labelFahr); textFahr = new JTextField (10); add (textFahr); labelCels = new JLabel ("Celsius: ", SwingConstants.LEFT); labelCels.setToolTipText("This is a scale and unit of measurement for temperature"); add (labelCels); textCels = new JTextField (10); add (textCels); conFahToCel = new JButton ("Convert Fahrenheit to Celsius"); add (conFahToCel); conCelToFah = new JButton ("Convert Celsius to Fahrenheit"); add (conCelToFah); JPanel panel = new JPanel(new GridLayout(2, 2, 12, 6)); panel.add(labelFahr); panel.add(labelCels); panel.add(textFahr); panel.add(textCels); add(panel, BorderLayout.NORTH); JPanel buttonPanel = new JPanel(); buttonPanel.add(conFahToCel); buttonPanel.add(conCelToFah); add(buttonPanel, BorderLayout.SOUTH); conFahToCel.addActionListener(new FahrListener ()); conCelToFah.addActionListener(new CelsListener ()); } // end constructor private class FahrListener implements ActionListener { public void actionPerformed(ActionEvent event) { if (event.getSource() == conFahToCel){ int conFahToCel = (int) ((5.0/9.0 * (((Double.parseDouble(textFahr.getText())) -32)))); textCels.setText(conFahToCel + "

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