Bifalgorithm post · Computer science one · Converting Units

How do you design a Java program that converts Fahrenheit to Celsius?

How do you design a Java program that converts Fahrenheit to Celsius? (Computer science one · Converting Units) — "Let’s write a program that will prompt the u…

Ideas in this flowchart

  • Java program for converting Fahrenheit temperatures to Celsius
  • The program prompts for a Fahrenheit value and converts it using C = (F − 32) · 5/9.
  • Basic program structure
  • Declare the package and class.
  • Import the Scanner class to read standard input.
  • Add a main() method so the class is executable.
  • Document the program and use TODO comments for unfinished work.
  • Input and output data setup
  • Declare double variables for fractional values: double fahrenheit, celsius;
  • Initialize a Scanner for standard input: Scanner s = new Scanner(System.in);
  • Program execution steps
  • Prompt the user: System.out.println("Please enter degrees in Fahrenheit: ");
  • Read the Fahrenheit value: fahrenheit = s.nextDouble();
  • Calculate Celsius: celsius = (fahrenheit - 32) * (5 / 9);
  • Print the result with System.out.printf() showing both Fahrenheit and Celsius.
Feed/

How do you design a Java program that converts Fahrenheit to Celsius?

No cover

Channel

Computer science one

1 page

"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);"

Flowchart preview
👍
0
0 comments0 shares

Sign in to view and post comments.