Explain the temperature converter Java?

326    Asked by KeithSutherland in Java , Asked on Oct 11, 2022

 I have made a program that converts Celsius to Fahrenheit and Fahrenheit to Celsius. How is my code?


import java.util.Scanner;
public class TemperatureConversion {
    public static void main(String[] args) {
        double fahrenheit, celsius;
        Scanner input = new Scanner(System.in);
        //  Celsius to Fahrenheit u2103 degree Celsius symbol
        System.out.println("Input temperature (u2103): ");
        celsius = input.nextDouble();
        System.out.println(celsius * 1.8 + 32 + " u2109 n");
        // Fahrenheit to Celsius u2109 degree Fahrenheit symbol
        System.out.println("Input temperature (u2109: ");
        fahrenheit = input.nextDouble();
        System.out.print((fahrenheit - 32) / 1.8 + " u2103");
    }


Answered by Michael MILLER

Temperature converter Java solves the problem, and that's good. Period.

But you're presenting your code here to get advice on how to improve your programming skills, so here are a few remarks. Take them as a hint where to go from here, as soon as you feel ready for the "next level".

Separate user interaction from computation

Your main method contains both aspects in one piece of code, even within the same line (e.g. System.out.println(celsius * 1.8 + 32 + " u2109
");). Make it a habit to separate tasks that can be named individually into methods of their own:

input a Celsius value (a method double readCelsius())

input a Fahrenheit value (a method double readFahrenheit())

convert from Celsius to Fahrenheit (a method double toFahrenheit(double celsius))

convert from Fahrenheit to Celsius (a method double toCelsius(double fahrenheit))

output a Fahrenheit value (a method void printFahrenheit(double fahrenheit))

output a Celsius value (a method void printCelsius(double celsius))

With separate methods, it will be easier to later change your program to e.g. use a window system, or do bulk conversions of many values at once, etc.

More flexible workflow

Your current program always forces the user to do exactly one C-to-F and then one F-to-C conversion. This will rarely both be needed at the same time. I'd either ask the user at the beginning for the conversion he wants, or make it two different programs. By the way, doing so will be easier if the different tasks have been separated.

Minor hints

Combine variable declaration and value assignment. You wrote double fahrenheit, celsius; and later e.g. celsius = input.nextDouble();, I'd combine that to read double celsius = input.nextDouble();. This way, when reading your program (maybe half a year later), you immediately see at the same place that celsius is a double number coming from some input.

I'd avoid the special characters like u2109 and instead write two simple characters °F. Not all fonts on your computer will know how to show that u2109 Fahrenheit symbol, so this might end up as some ugly-looking question mark inside a rectangle or similar. Using two separate characters does the job in a more robust way.



Your Answer

Interviews

Parent Categories