In this exercise, you will create a temperature converter which will convert Fah
ID: 3775937 • Letter: I
Question
In this exercise, you will create a temperature converter which will convert Fahrenheit values to Celsius and vice-versa. You will need the following two formulas which relate the temperature f in Fahrenheit to the temperature c in Celsius: f = c times 9/5 + 32 c = (f - 32) times 5/9. The input will be a string consisting of a floating-point number followed immediately by the letter F or C, such as "13.2C". You should convert to the other temperature scale and print the converted value in the same format. For example, if the input is "8F" then the output should be (approximately) "-13.333C", and if the input is "12.5C" then the output should be "54. 5F".Explanation / Answer
FahrenheitToCelsius.java
import java.util.Scanner;
public class FahrenheitToCelsius {
public static void main(String[] args) {
//Declaring variables
int choice;
//Scanner class object is used to read the inputs entered by the user
Scanner sc=new Scanner(System.in);
do
{
//Displaying the menu
System.out.println(" :: Menu ::");
System.out.println("1.Fahrenheit to Celsius");
System.out.println("2.Celsius to Fahrenheit");
System.out.println("3.Exit");
//Getting the user choice
System.out.print("Enter choice :");
choice=sc.nextInt();
//Based on the choice .The corresponding case will get executed.
switch(choice)
{
case 1:{
//Declaring variables
String strfah,strcel;
double fahrenheit,celsius;
//Getting the temperature value in Fahrenheit entered by the user
System.out.print(" Enter the Temperature in Fahrenheit(F):");
strfah=sc.next();
int index=strfah.indexOf("F");
//Parsing the String to Double
fahrenheit=Double.parseDouble(strfah.substring(0,index));
//Converting the fahrenheit to celsius
celsius=Math.floor(((fahrenheit-32)*5/9) * 1000) / 1000.0;
//Concatenating with "C" by converting the celsius value to String
strcel=String.valueOf(celsius)+"C";
//Displaying the output
System.out.println(" The Temperature in Celsius(C) is "+strcel);
continue;
}
case 2:{
//Declaring variables
String strfah,strcel;
double fahrenheit,celsius;
//Getting the temperature value in Celsius entered by the user
System.out.print(" Enter the Temperature in Celsius(C):");
strcel=sc.next();
int index=strcel.indexOf("C");
//Parsing the String to Double
celsius=Double.parseDouble(strcel.substring(0,index));
//Converting the celsius to fahrenheit
fahrenheit=Math.floor((((celsius*9)/5)+32) * 1000) / 1000.0;
//Concatenating with "F" by converting the Fahrenheit value to String
strfah=String.valueOf(fahrenheit)+"F";
//Displaying the output
System.out.println(" The Temperature in Fahrenheit(F) is "+strfah);
continue;
}
case 3:{
System.out.println("** Program Exit **");
break;
}
default:{
System.out.println("Invalid inttput.");
continue;
}
}
}while(choice!=3);
}
}
______________________
Output:
:: Menu ::
1.Fahrenheit to Celsius
2.Celsius to Fahrenheit
3.Exit
Enter choice :1
Enter the Temperature in Fahrenheit(F):8F
The Temperature in Celsius(C) is -13.334C
:: Menu ::
1.Fahrenheit to Celsius
2.Celsius to Fahrenheit
3.Exit
Enter choice :2
Enter the Temperature in Celsius(C):12.5C
The Temperature in Fahrenheit(F) is 54.5F
:: Menu ::
1.Fahrenheit to Celsius
2.Celsius to Fahrenheit
3.Exit
Enter choice :3
** Program Exit **
____________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.