4
\$\begingroup\$

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");
    }
}
\$\endgroup\$
7
\$\begingroup\$

It solves the problem, and that's good. Period.

But you're presenting your code here to get advice 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 \n");). 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.

\$\endgroup\$
4
  • \$\begingroup\$ Thank you for the comment and tips on my code. It really helped me a lot on how to improve my code. I'll be studying and experiment on how things work in Java. Thank you. \$\endgroup\$ – user226918 Jun 30 '20 at 2:32
  • 1
    \$\begingroup\$ Opposing opinion on the special characters: The degree sign is a special character which will be mangled if the code is transferred via a system that does not understand unicode. Unfortunaltey in the year 2020 it is still best to only rely on US-ASCII when dealing with file formats that do not include charset information (such as Java source code and property files). So use "\u00B0F" and "\u00B0C" instead. \$\endgroup\$ – TorbenPutkonen Jun 30 '20 at 4:51
  • 1
    \$\begingroup\$ @TorbenPutkonen Yes, your comment goes even further than my recommendation. I wrote my answer hoping that the OP's editor and compiler are correctly set up, and both agree on a character encoding containing the "degree" character. If so, I'd prefer °F over \u00B0F because of readability. \$\endgroup\$ – Ralf Kleberhoff Jun 30 '20 at 9:01
  • \$\begingroup\$ Thank you for all of your help. I have now fixed the code for conversion and added other temperature scale to convert with: pastebin.com/LcapCu8s \$\endgroup\$ – user226918 Jul 9 '20 at 1:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy