Login
Order Now
Support
theprogrammingassignmenthelp

Java Program - Convert from Celsius to Fahrenheit

  • 5th Jun, 2019
  • 17:03 PM


public class Problem1 {

    public static double celsiusToFahrenheit(double celsius) {
        //System.out.println("DEBUG: celsius is: " + celsius);
        return ((9.0 / 5) * celsius) + 32;
    }

    public static double fahrenheitToCelsius(double fahrenheit) {
        //System.out.println("DEBUG: fahrenheit is: " + fahrenheit);
        return (5.0 / 9.0) * (fahrenheit - 32);
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub        
        if (args.length != 2) {
            System.out.println("Invalid argument command line.");
            System.out.println("Use: Problem1 F/C temperature");
            return;
        }
        if (args[0].equals("F")) {
            System.out.println("The temperature in celsius is: " + (int)fahrenheitToCelsius(Double.parseDouble(args[1])));
        } else if (args[0].equals("C")) {
            System.out.println("The temperature in fahrenheit is: " + (int)celsiusToFahrenheit(Double.parseDouble(args[1])));
        } else {
            System.out.println("Invalid argument command line. Type F or C only");
        }
    }
}
 

Share this post