a.write a program that allows the user to convert a temperature given in degrees
ID: 3642239 • Letter: A
Question
a.write a program that allows the user to convert a temperature given in degrees from either celsius to fahrenheit or fahrenheit to celsius.use the following formulas: degrees_c=5(degrees_f-32)/9 , degrees_f=(9(degrees_c)/5)+32) ,prompt the user to enter a temperature and either a C or c for celsius or and F or f for Fahrenheit.convert the temperature to fahrenheit if celsius is entered or to celsius if fahrenheit is entered.display the result in a readable formate ,use a loop so the user can convert other temperatures.if the user enters a letter other than C or F-in either uppercase or lowercase-after a temperature ,print an error message and ask the user to reenter a valid selection .do not ask the user to reenter the numeric portion of the temperature again,however, after each conversion, ask the user to type Qor q to quit or to press any other key to repeat the loop and perform another conversion.b. repeat the project a using the JOptionPane.offer the user Yes and No buttons to end execution instead of asking for character input of Q or q.JOptionPane is described in the graphics supplements of chapter 2 and 3.
Explanation / Answer
Please find below the program as per your requirement. import java.util.Scanner; public class TemperatureConversion { public static void main(String[] args) { System.out.println("Enter Temperature"); Scanner sc = new Scanner(System.in); float temperature = sc.nextFloat(); System.out.println("Convert to Fahrenheit(F) or Celcius(C)?"); String option = sc.next(); if ((option.equalsIgnoreCase("F") || option.equalsIgnoreCase("C"))) { while (option.equalsIgnoreCase("F") || option.equalsIgnoreCase("C") && !option.equalsIgnoreCase("Q")) { if (option.equalsIgnoreCase("F")) { float c = (5 * (temperature - 32) / 9); System.out.println(temperature + " F = " + c + " C"); } else if (option.equalsIgnoreCase("C")) { float f = (9 * temperature / 5) + 32; System.out.println(temperature + " C = " + f + " F"); } System.out .println("Would like to continue, If yes Enter Fahrenheit(F) or Celcius(C) or Quit (Q) "); option = sc.next(); if (option.equalsIgnoreCase("Q")) { break; } else if (!(option.equalsIgnoreCase("F") || option .equalsIgnoreCase("C"))) { System.out.println("Enter a Valid Selection"); break; } else { System.out.println("Enter Temperature"); temperature = sc.nextFloat(); } } } else { System.out.println("Enter a Valid Selection"); } } } Output : Enter Temperature 30 Convert to Fahrenheit(F) or Celcius(C)? F 30.0 F = -1.1111112 C Would like to continue, If yes Enter Fahrenheit(F) or Celcius(C) or Quit (Q) C Enter Temperature 20 20.0 C = 68.0 F Would like to continue, If yes Enter Fahrenheit(F) or Celcius(C) or Quit (Q) F Enter Temperature 20 20.0 F = -6.6666665 C Would like to continue, If yes Enter Fahrenheit(F) or Celcius(C) or Quit (Q) Q
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.