Convert Degrees Celsius to FahrenheitUse simple methods to convert degrees Fahrenheit to Celsius.
Java
Convert degrees. Humans do not agree on many things. On thing that differs between places is the unit of measurement for temperatures. Both Fahrenheit and Celsius may be used.
To build the next great weather app, a method may be used to convert between these units. We can use a simple expression for both directions in the conversion.
Example methods. In a real program, we would likely want to place the ConvertCToF and ConvertFToC methods in a special class. But for now we can just place them in the Program class.
Detail The 2 methods each converts with a mathematical expression. We can perform these conversions reliably this way.
public class Program { static double ConvertCToF(double c) { // Convert Celsius to Fahrenheit. return ((9.0 / 5.0) * c) + 32; } static double ConvertFToC(double f) { // Convert Fahrenheit to Celsius. return ((5.0 / 9.0) * (f - 32)); } public static void main(String[] args) { // Test our methods. double result1 = ConvertCToF(100); System.out.println(result1); double result2 = ConvertFToC(212); System.out.println(result2); double result3 = ConvertCToF(50); System.out.println(result3); double result4 = ConvertFToC(122); System.out.println(result4); } }
212.0 100.0 122.0 50.0
Notes, above program. The program converts 100 degrees Celsius to 212 degrees Fahrenheit. This is correct according to my research.
And For the opposite direction, 122 degrees Fahrenheit gives us 50 degrees Celsius.
A review. Most units of measurement can be easily converted with a special method. Well-tested, reusable methods can help program development.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.