In: Computer Science
**********Java language************
1- (Conversions between Celsius and Fahrenheit) Write a class that contains the following two methods:
/** Convert from Celsius to Fahrenheit */
public static double celsiusToFahrenheit(double celsius)
/** Convert from Fahrenheit to Celsius */
public static double fahrenheitToCelsius(double fahrenheit)
The formula for the conversion is:
Celsius = (5.0 / 9) * (Fahrenheit – 32)
Fahrenheit = (9.0 / 5) * Celsius + 32
Program output should be like:
If you want to convert from Fahrenheit To Celsius press 0 vice versa press 1:
0
Enter the degree in Fahrenheit: 22
The degree in Celsius: -5.5
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>.
If you want to convert from Fahrenheit To Celsius press 0 vice versa press 1:
1
Enter the degree in Celsius: 33
The degree in Celsius: 91.4
import java.util.Scanner;
class Main
{
/** Convert from Celsius to Fahrenheit */
public static double celsiusToFahrenheit(double celsius)
{
return (9.0/5)*celsius+32;
}
/** Convert from Fahrenheit to Celsius */
public static double fahrenheitToCelsius(double fahrenheit)
{
return (5.0/9)*(fahrenheit-32);
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("If you want to convert from Fahrenheit To
Celsius press 0 vice versa press 1:");
int ch = sc.nextInt();
if(ch==0)
{
System.out.print("Enter the degree in Fahrenheit: ");
double temp = sc.nextDouble();
double result = fahrenheitToCelsius(temp);
System.out.print("The degree in Celsius: "+result);
}
else
{
System.out.print("Enter the degree in Celsius: ");
double temp = sc.nextDouble();
double result = celsiusToFahrenheit(temp);
System.out.print("The degree in Fahrenheit: "+result);
}
}
}