6.8 (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:
fahrenheit = (9.0 / 5) * celsius + 32
celsius = (5.0 / 9) * (fahrenheit – 32)
Write a test program that invokes these methods to display the following tables:
celsius fahrenheit fahrenheit celsius
40.0 104.0 120.0 48.89
39.0 102.2 110.0 43.33
…
32.0 89.6 40.0 4.44
31.0 87.8 30.0 -1.11
6.8(摄氏度和华氏度之间的转换)编写一个包含以下内容的类
两种方法:
/**从摄氏度转换为华氏度*/
public static double celsiusToFahrenheit(double celsius)
/**从华氏温度转换为摄氏温度*/
public static double fahrenheitToCelsius(double fahrenheit)
换算公式为:
华氏度=(9.0/5)*摄氏度+32
摄氏度=(5.0/9)*(华氏度–32)
编写一个测试程序,调用这个两个方法来显示如下表格:
摄氏度 华氏度 华氏度 摄氏度
40.0 104.0 120.0 48.89
39.0 102.2 110.0 43.33
…
32.0 89.6 40.0 4.44
31.0 87.8 30.0 -1.11
代码如下:
public class Unite6Test8
{
public static void main(String[] args)
{
Change change = new Change();
System.out.println("Celsius\t\tFahrenheit\t\tFahrenheit\t\tCelsius");
for(double i = 40,j = 120;i >= 31 && j >= 30;i -= 1,j -= 10)
System.out.printf("%.1f\t\t%.1f\t\t\t%.1f\t\t\t%.2f\n", i, change.celsiusToFahrenheit(i), j, change.fahrenheitToCelsius(j));
}
}
class Change
{
public static double celsiusToFahrenheit(double celsius)
{
double Fahrenheit = (9.0/5)*celsius+32;
return Fahrenheit;
}
public static double fahrenheitToCelsius(double fahrenheit)
{
double Celsius = (5.0/9)*(fahrenheit-32);
return Celsius;
}
}
结果如下: