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

Write a program using loops that allows the user to convert a temperature given

ID: 3642384 • Letter: W

Question

Write a program using loops that allows the user to convert a temperature given in degrees from either Celcius to Farenheit or Fahrenheit to Celcius. Use the following formulas: Degree_C = 5(Degree_f-32)/9 Degree_F= (9(Degree_C/5)+32) Prompt user to enter a temperature and either a C or c for Celcius or an F or f for fahrenheit. Convert the temperature to fahrenheit if celcius is entered, or to celcius if farenheit is entered. Display the result in a readable format. If anything other than C,c,F, or f is entered, print and error message and stop

Explanation / Answer

import java.util.Scanner;

import javax.swing.JOptionPane;

public class TempConversion {

       public static void main(String[] args){

              double result;

              char degree;

              int input;

              input = new Integer(JOptionPane.showInputDialog("Enter a temperature: ")).intValue();

              degree = JOptionPane.showInputDialog("Celsius or Fahrenheit? ").charAt(0);

              if(degree == 'F' || degree == 'f') {

                     result = 5.0/9.0 * (input - 32);

                     JOptionPane.showMessageDialog(null, input + " Fahrenheit is " + result + " Celsius");

                     System.out.println(input + " Fahrenheit is " + result + " Celsius");

              }

              else if(degree == 'C' || degree == 'c') {

                     result = 9.0/5.0 * input + 32;

                     JOptionPane.showMessageDialog(null, input + " Celsius is " + result + " Fahrenheit");

              }

              else {

                     System.out.println("Invalid entry");

              }

       }

}

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