Concept
How do you design a Java program that converts Fahrenheit to Celsius?
ComputerScienceOne / Converting Units
"Let’s write a program that will prompt the user to enter a temperature in degrees Fahrenheit and convert it to degrees Celsius using the formula C = (F − 32) · 5/9.\n\nWe begin with the basic program outline which will include a package and class declaration. We’ll also need to read from the standard input, so we’ll import the Scanner class. We’ll want our class to be executable, so we need to put a main() method in our class. Finally, we’ll document our program to indicate its purpose.\n\nIt is common for programmers to use a comment along with a TODO note to themselves as a reminder of things that they still need to do with the program. Let’s first outline the basic steps that our program will go through:\n\n1. We’ll first prompt the user for input, asking them for a temperature in Fahrenheit.\n\n2. Next we’ll read the user’s input, likely into a floating point number as degrees can be fractional.\n\n3. Once we have the input, we can calculate the degrees Celsius by using the formula above.\n\n4. Lastly, we will want to print the result to the user to inform them of the value.\n\nAs we read each step it becomes apparent that we’ll need a couple of variables: one to hold the Fahrenheit (input) value and one for the Celsius (output) value. It also makes sense that each of these should be double variables as we want to support fractional values. At the top of our main() method, we’ll add the variable declarations:\n\ndouble fahrenheit, celsius;\n\nWe’ll also need a Scanner, initialized to read from the standard input:\n\nScanner s = new Scanner(System.in);\n\nEach of the steps is now straightforward; we’ll use a System.out.println() statement in the first step to prompt the user for input:\n\nSystem.out.println(\"Please enter degrees in Fahrenheit: \");\n\nIn the second step, we’ll use our Scanner to read in a value from the user for the fahrenheit variable. Recall that we use the method s.nextDouble() to read a double value from the user.\n\nfahrenheit = s.nextDouble();\n\nWe can now compute celsius using the formula provided:\n\ncelsius = (fahrenheit - 32) * (5 / 9);\n\nFinally, we use System.out.printf() to output the result to the user:\n\nSystem.out.printf(\"%f Fahrenheit is %f Celsius\n\n\", fahrenheit, celsius);"
Related Ideas
- How do you outline a C program that converts Fahrenheit to Celsius?ComputerScienceOne · Converting Units
- Why does integer division make the Fahrenheit-to-Celsius program produce zero?ComputerScienceOne · Computing Quadratic Roots
- How can PHP convert a Fahrenheit temperature to Celsius?ComputerScienceOne · Converting Units
- How do you write a basic Hello World program in Java?ComputerScienceOne · Getting Started: Hello World
- How are variables declared and assigned in Java?ComputerScienceOne · Declaration & Assignment
- How can PHP read and clean user input from standard input?ComputerScienceOne · Converting Units
- Why is scanf() considered a risky input mechanism?ComputerScienceOne · Converting Units
- How can a Java program read income and children inputs while rejecting negative values?ComputerScienceOne · Life & Taxes