Question

IN JAVA…PLEASE comment the code thoroughly so I can understand the thought process. Write a GUI...

IN JAVA…PLEASE comment the code thoroughly so I can understand the thought process. 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 F is the Fahrenheit temperature and C is the Celsius temperature.

Instead of only converting from Celsius to Fahrenheit, also convert from Fahrenheit to Celsius depending on the user's choice.

Some hints: Use JTextField and JLabel for required input. The KiloConverter program is a good example to get you started. Please make sure you comment your code thoroughly. The code should be nicely formatted and should use proper variables.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

ANSWER:

import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.text.DecimalFormat;

import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

public class TempCalc {
   static JLabel outText = new JLabel("");
   static String input = "", output = "";
   static JTextField inputText = new JTextField();
   static DecimalFormat df = new DecimalFormat(".##");

   public TempCalc() {

       JFrame f = new JFrame();
       JLabel inputLabel = new JLabel("Input");
       JLabel outputLabel = new JLabel("Outut");

       inputLabel.setBounds(150, 70, 80, 30);

       inputText.setBounds(190, 70, 300, 30);
       outText.setBounds(190, 400, 300, 30);
       outputLabel.setBounds(150, 400, 300, 30);

       JLabel inputScale = new JLabel("Input Scale");
       inputScale.setBounds(75, 100, 300, 30);
       ButtonGroup bg = new ButtonGroup();
       JRadioButton input1 = new JRadioButton("Celcius");
       JRadioButton input2 = new JRadioButton("Farhenheit");

       input1.setBounds(75, 140, 300, 30);
       input2.setBounds(75, 180, 300, 30);
       bg.add(input1);
       bg.add(input2);

       JLabel outputScale = new JLabel("output Scale");
       outputScale.setBounds(575, 100, 300, 30);
       ButtonGroup bg1 = new ButtonGroup();
       JRadioButton output1 = new JRadioButton("Celcius");
       JRadioButton output2 = new JRadioButton("Farhenheit");

       output1.setBounds(575, 140, 300, 30);
       output2.setBounds(575, 180, 300, 30);
       bg1.add(output1);
       bg1.add(output2);

       input1.addItemListener(new ItemListener() {
           public void itemStateChanged(ItemEvent e) {
               System.out.println(e.getStateChange());
               if (e.getStateChange() == 1) {
                   input = "C";
                   if (input.length() != 0 && output.length() != 0) {
                       if (output.equals("F")) {
                           outText.setText(
                                   df.format(TemparatureCal.celciusToFahrenheit(inputText.getText().trim())));
                       }
                       if (output.equals("K")) {

                           outText.setText(df.format(TemparatureCal.celciusToKelvin(inputText.getText().trim())));
                       }
                       if (output.equals("C")) {
                           outText.setText(df.format(Double.parseDouble(inputText.getText().trim())));
                       }
                   } else {
                       outText.setText("No Input");
                   }
               }
           }
       });
       input2.addItemListener(new ItemListener() {
           public void itemStateChanged(ItemEvent e) {
               System.out.println(e.getStateChange());
               if (e.getStateChange() == 1) {
                   input = "F";
                   if (input.length() != 0 && output.length() != 0) {
                       if (output.equals("F")) {
                           outText.setText((df.format(Double.parseDouble(inputText.getText().trim()))) + "");
                       }
                       if (output.equals("K")) {
                           outText.setText(
                                   df.format(TemparatureCal.fahrenheitToKelvin(inputText.getText().trim())) + "");
                       }
                       if (output.equals("C")) {
                           outText.setText(
                                   df.format(TemparatureCal.fahrenheitTocelcius(inputText.getText().trim())) + "");
                       }
                   } else {
                       outText.setText("No Input");
                   }
               }
           }
       });

      

       output1.addItemListener(new ItemListener() {
           public void itemStateChanged(ItemEvent e) {
               System.out.println(e.getStateChange());
               if (e.getStateChange() == 1) {
                   output = "C";
                   if (input.length() != 0 && output.length() != 0) {
                       if (input.equals("F")) {
                           outText.setText(
                                   df.format(TemparatureCal.fahrenheitTocelcius(inputText.getText().trim())));
                       }
                       if (input.equals("K")) {
                           outText.setText(df.format(TemparatureCal.kelvinToCelcius(inputText.getText().trim())) + "");
                       }
                       if (input.equals("C")) {
                           outText.setText(df.format(Double.parseDouble(inputText.getText().trim())));
                       }
                   } else {
                       outText.setText("No Input");
                   }
               }
           }
       });

       output2.addItemListener(new ItemListener() {
           public void itemStateChanged(ItemEvent e) {
               if (e.getStateChange() == 1) {
                   output = "F";
                   if (input.length() != 0 && output.length() != 0) {
                       if (input.equals("F")) {
                           outText.setText(df.format(Double.parseDouble(inputText.getText().trim())) + "");
                       }
                       if (input.equals("K")) {
                           outText.setText(
                                   df.format(TemparatureCal.kelvinToFahrenheit(inputText.getText().trim())) + "");
                       }
                       if (input.equals("C")) {
                           outText.setText(
                                   df.format(TemparatureCal.celciusToFahrenheit(inputText.getText().trim())) + "");
                       }
                   } else {
                       outText.setText("No Input");
                   }
               }

           }
       });
      

       f.add(output1);
       f.add(output2);
       f.add(input1);
       f.add(input2);
       f.add(inputLabel);
       f.add(inputText);
       f.add(outText);
       f.add(outputLabel);
       f.setSize(1000, 600);
       f.setLayout(null);
       f.setVisible(true);
   }

   public static void main(String[] args) {
       new TempCalc();
   }
}

class TemparatureCal {
   static public double celciusToFahrenheit(String input) {
       double temp = Double.parseDouble(input);
       temp = temp * 9 / 5 + 32;
       return temp;
   }

   static public double celciusToKelvin(String input) {
       double temp = Double.parseDouble(input);
       temp = temp + 273.15;
       return temp;
   }

   static public double fahrenheitTocelcius(String input) {
       double temp = Double.parseDouble(input);
       temp = (temp - 32) * 5 / 9;
       return temp;
   }

   static public double fahrenheitToKelvin(String input) {
       double temp = Double.parseDouble(input);
       temp = (temp + 459.67) * 5 / 9;
       return temp;
   }

   static public double kelvinToFahrenheit(String input) {
       double temp = Double.parseDouble(input);
       temp = temp * 9 / 5 - 459.67;
       return temp;
   }

   static public double kelvinToCelcius(String input) {
       double temp = Double.parseDouble(input);
       temp = temp - 273.15;
       return temp;
   }
}

Add a comment
Know the answer?
Add Answer to:
IN JAVA…PLEASE comment the code thoroughly so I can understand the thought process. Write a GUI...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • IN JAVA…Write a GUI application that converts Celsius temperatures to Fahrenheit temperatures. The user should be...

    IN JAVA…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 equiva-lent Fahrenheit temperature. Use the following formula to make the conversion: F = (9/5)C + 32 F is the Fahrenheit temperature and C is the Celsius temperature. Instead of only converting from Celsius to Fahrenheit, also convert from Fahrenheit to Celsius depending on the user's choice. Some hints: Use JTextField and...

  • Question: Fahrenheit To Celsius Temperature Converter GUI Assignment Write A GUI To... java Fahrenholt to Celsius...

    Question: Fahrenheit To Celsius Temperature Converter GUI Assignment Write A GUI To... java Fahrenholt to Celsius Temperature Converter GUI Assignment Write a Gul to convert Fahrenheit temperatures to Celsius temperatures and has the following appearance: Com Convert It must include the following foatures • The frame we must say 'Fahrenheit to Celsius Temperature Converter • A border layout will be used for the GUI • The JLabelite of the GUI wil suy Fahrerholt to Celsius Temperature Converter and be in...

  • (in python) Write a GUI application that converts Celsius temperatures to Fahrenheit temperatures. The user s......

    (in python) Write a GUI application that converts Celsius temperatures to Fahrenheit temperatures. The user s... 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

  • IN JAVA - COMMENT CODE WELL Write a class named Card which will represent a card...

    IN JAVA - COMMENT CODE WELL Write a class named Card which will represent a card from a deck of cards. A card has a suit and a face value. Suits are in order from low to high: Clubs, Diamonds, Hearts and Spades. The card values from low to high: 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, and Ace. Write a Deck class that contains 52 cards. The class needs a method named shuffle that...

  • ***Java Project*** Please upload the entire code and attach the screenshots of the code. The screenshots help me to write the code, so please attach that. Thank you so much. If you could use the comme...

    ***Java Project*** Please upload the entire code and attach the screenshots of the code. The screenshots help me to write the code, so please attach that. Thank you so much. If you could use the comment to explain the code, it would be perfect! Thank you so much~ Design and code a Swing GUl for a two-player tic-tac-toe (noughts and crosses) game on a 3 x 3 game board. The JFrame should use a BorderLayout with a JLabel in the...

  • The Gui has all the right buttons, but from there i get lost. I need to...

    The Gui has all the right buttons, but from there i get lost. I need to know whats wrong with my assignment can someone please help. The code I have so far is listed below, could you please show me the errors in my code. PYTHON Create the GUI(Graphical User Interface). Use tkinter to produce a form that looks much like the following. It should have these widgets. Temperature Converter GUI Enter a temperature (Entry box)                 Convert to Fahrenheit...

  • Intro To Java Class. AS SIMPLE AS POSSIBLE. Please post the pic of code, will up...

    Intro To Java Class. AS SIMPLE AS POSSIBLE. Please post the pic of code, will up Vote! You will need to use Blue) for the following questions. Although these are programming exercises, remember always that the first step to the program is to put your ideas on paper to organize your thoughts. Remember Section 1.6: The Programming Process. Centigrade to Fahrenheit Table Write a program that displays a table of the centigrade temperatures 0 through 20 and their Fahrenheit equivalents....

  • Java please The kelvin is the base unit of temperature in the International System of Units...

    Java please The kelvin is the base unit of temperature in the International System of Units (SI), having the unit symbol K. It is named after the Belfast-born, Glasgow University engineer and physicist William Thomson, 1st Baron Kelvin (1824–1907). It uses absolute zero as its null point. The Celsius scale, also known as the centigrade scale, is a temperature scale used by the International System of Units (SI). The Celsius scale is based on 0 °C for the freezing point...

  • Java program Write a Temperature class that represents temperatures in degrees in both Celsius and Fahrenheit....

    Java program Write a Temperature class that represents temperatures in degrees in both Celsius and Fahrenheit. Use a floating- point number for the temperature and a character for the scale, eitherでfor Celsius or 'F' for fahrenheit. The class should have Four constructors: one for the number of degrees, one for the scale, one for both the degrees and scale, and a default constructor. For each of these constructors, assume zero degrees if no value is specified and celsius if no...

  • In java code: Write a Temperature class that has two private instance variables: • degrees: a...

    In java code: Write a Temperature class that has two private instance variables: • degrees: a double that holds the temperature value • scale: a character either ‘C’ for Celsius or ‘F’ for Fahrenheit (either in uppercase or lowercase) The class should have (1) four constructor methods: one for each instance variable (assume zero degrees if no value is specified and assume Celsius if no scale is specified), one with two parameters for the two instance variables, and a no-argument...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT