Skip to content

Temperature conversion added to conversions - issue#5042 #5048

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
package com.thealgorithms.conversions;

import java.text.DecimalFormat;
import java.util.Scanner;
/**
* Convets the temperatures from user to Celsius/Fahrenheit/Kelvin
*
* @author Tharun S.M.
*/

public class TemperatureConversion {
/*
* Object of DecimalFormat Class
* to round the output to 2 decimal point value
*/
private static final DecimalFormat rnd = new DecimalFormat("0.00");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why can't we just convert double to double? I would suggest that.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the slope approach, im not well aware of that subject, and the value is set to float to stop huge decimal value inputs, but the decimal format is required to round the values

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do u want to use Affine Converter instead of the conditional if statements?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my opinion, the most natural type to represent temperature is double. There is no need to do any rounding.

Regarding, the AffineConverter, among the temperature units, which you have suggested, each conversion can be done with a formula of the type

valueInNewUnit = someSlope * valueInOldUnit + someIntercept

I suggested to have such AffineConverter, because then the problem of changing the units can be simplified just to hard coding the slope and the intercept (e.g. in case of converting Kelvin to Celsius, the slope is 1, and the intercept is -273.15).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok ill look into it

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you want me to hard code the user inputs also?, because how do we recognize what temperature type is being input by the user

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you want me to hard code the user inputs also?, because how do we recognize what temperature type is being input by the user

In a sense: yes. We do not have programs here expecting inputs from users. The tests should be written in such a way, that they will also illustrate how to use your code.

/*
* Main method
*
* @param args Command line arguments
*/

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

// Get the type of temprature input
System.out.println("Enter the temperature input type (use numbers)");
System.out.println("1 => Celsius");
System.out.println("2 => Fahrenheit");
System.out.println("3 => Kelvin");
int fromType = sc.nextInt();

// get the actual temprature
System.out.println("Enter the temperature :");
String a = sc.next();
float inTemp = Float.parseFloat(a);

// get the type you want to convert your temperature to
System.out.println("Type you want to convert your temperature to (use numbers)");
System.out.println("1 => Celsius");
System.out.println("2 => Fahrenheit");
System.out.println("3 => Kelvin");
int toType = sc.nextInt();

if (fromType == toType) {
System.out.println("Your Temperature is the same !!");
} else if (fromType == 1 && toType == 2) {
System.out.println("Converted value: " + convertCelsiusToFahrenheit(inTemp) + " °F");
} else if (fromType == 1 && toType == 3) {
System.out.println("Converted value: " + convertCelsiusToKelvin(inTemp) + " K");
} else if (fromType == 2 && toType == 1) {
System.out.println("Converted value: " + convertFahrenheitToCelsius(inTemp) + " °C");
} else if (fromType == 2 && toType == 3) {
System.out.println("Converted value: " + convertFahrenheitToKelvin(inTemp) + " K");
} else if (fromType == 3 && toType == 1) {
System.out.println("Converted value: " + convertKelvinToCelsius(inTemp) + " °C");
} else if (fromType == 3 && toType == 2) {
System.out.println("Converted value: " + convertKelvinToFahrenheit(inTemp) + " °F");
} else {
System.out.println("Please check your input and output types");
}
sc.close();
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not needed.

Suggested change
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Get the type of temprature input
System.out.println("Enter the temperature input type (use numbers)");
System.out.println("1 => Celsius");
System.out.println("2 => Fahrenheit");
System.out.println("3 => Kelvin");
int fromType = sc.nextInt();
// get the actual temprature
System.out.println("Enter the temperature :");
String a = sc.next();
float inTemp = Float.parseFloat(a);
// get the type you want to convert your temperature to
System.out.println("Type you want to convert your temperature to (use numbers)");
System.out.println("1 => Celsius");
System.out.println("2 => Fahrenheit");
System.out.println("3 => Kelvin");
int toType = sc.nextInt();
if (fromType == toType) {
System.out.println("Your Temperature is the same !!");
} else if (fromType == 1 && toType == 2) {
System.out.println("Converted value: " + convertCelsiusToFahrenheit(inTemp) + " °F");
} else if (fromType == 1 && toType == 3) {
System.out.println("Converted value: " + convertCelsiusToKelvin(inTemp) + " K");
} else if (fromType == 2 && toType == 1) {
System.out.println("Converted value: " + convertFahrenheitToCelsius(inTemp) + " °C");
} else if (fromType == 2 && toType == 3) {
System.out.println("Converted value: " + convertFahrenheitToKelvin(inTemp) + " K");
} else if (fromType == 3 && toType == 1) {
System.out.println("Converted value: " + convertKelvinToCelsius(inTemp) + " °C");
} else if (fromType == 3 && toType == 2) {
System.out.println("Converted value: " + convertKelvinToFahrenheit(inTemp) + " °F");
} else {
System.out.println("Please check your input and output types");
}
sc.close();
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import java.util.Scanner;

public class TemperatureConverter {

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);

    // Get the type of temperature input
    System.out.println("Enter the temperature input type (use numbers)");
    System.out.println("1 => Celsius");
    System.out.println("2 => Fahrenheit");
    System.out.println("3 => Kelvin");
    int fromType = sc.nextInt();

    // Get the actual temperature
    System.out.println("Enter the temperature:");
    float inTemp;
    if (sc.hasNextFloat()) {
        inTemp = sc.nextFloat();
    } else {
        System.out.println("Invalid temperature input. Please enter a valid number.");
        sc.close();
        return;
    }

    // Get the type you want to convert your temperature to
    System.out.println("Enter the temperature output type (use numbers)");
    System.out.println("1 => Celsius");
    System.out.println("2 => Fahrenheit");
    System.out.println("3 => Kelvin");
    int toType = sc.nextInt();

    if (fromType < 1 || fromType > 3 || toType < 1 || toType > 3) {
        System.out.println("Invalid input or output type. Please enter a number between 1 and 3.");
    } else if (fromType == toType) {
        System.out.println("Your Temperature is the same: " + inTemp);
    } else {
        float convertedTemp;
        switch (fromType) {
            case 1:
                convertedTemp = toType == 2 ? convertCelsiusToFahrenheit(inTemp) : convertCelsiusToKelvin(inTemp);
                break;
            case 2:
                convertedTemp = toType == 1 ? convertFahrenheitToCelsius(inTemp) : convertFahrenheitToKelvin(inTemp);
                break;
            case 3:
                convertedTemp = toType == 1 ? convertKelvinToCelsius(inTemp) : convertKelvinToFahrenheit(inTemp);
                break;
            default:
                convertedTemp = 0;
        }
        System.out.println("Converted value: " + convertedTemp + getUnit(toType));
    }
    sc.close();
}

public static float convertCelsiusToFahrenheit(float celsius) {
    return (celsius * 9 / 5) + 32;
}

public static float convertCelsiusToKelvin(float celsius) {
    return celsius + 273.15f;
}

public static float convertFahrenheitToCelsius(float fahrenheit) {
    return (fahrenheit - 32) * 5 / 9;
}

public static float convertFahrenheitToKelvin(float fahrenheit) {
    return (fahrenheit + 459.67f) * 5 / 9;
}

public static float convertKelvinToCelsius(float kelvin) {
    return kelvin - 273.15f;
}

public static float convertKelvinToFahrenheit(float kelvin) {
    return (kelvin * 9 / 5) - 459.67f;
}

public static String getUnit(int type) {
    switch (type) {
        case 1:
            return "C";
        case 2:
            return "F";
        case 3:
            return "K";
        default:
            return "";
    }
}

}
you can use this code in is more enhanced

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import java.util.Scanner;

public class TemperatureConverter {

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);

    // Get the type of temperature input
    System.out.println("Enter the temperature input type (use numbers)");
    System.out.println("1 => Celsius");
    System.out.println("2 => Fahrenheit");
    System.out.println("3 => Kelvin");
    int fromType = sc.nextInt();

    // Get the actual temperature
    System.out.println("Enter the temperature:");
    float inTemp;
    if (sc.hasNextFloat()) {
        inTemp = sc.nextFloat();
    } else {
        System.out.println("Invalid temperature input. Please enter a valid number.");
        sc.close();
        return;
    }

    // Get the type you want to convert your temperature to
    System.out.println("Enter the temperature output type (use numbers)");
    System.out.println("1 => Celsius");
    System.out.println("2 => Fahrenheit");
    System.out.println("3 => Kelvin");
    int toType = sc.nextInt();

    if (fromType < 1 || fromType > 3 || toType < 1 || toType > 3) {
        System.out.println("Invalid input or output type. Please enter a number between 1 and 3.");
    } else if (fromType == toType) {
        System.out.println("Your Temperature is the same: " + inTemp);
    } else {
        float convertedTemp;
        switch (fromType) {
            case 1:
                convertedTemp = toType == 2 ? convertCelsiusToFahrenheit(inTemp) : convertCelsiusToKelvin(inTemp);
                break;
            case 2:
                convertedTemp = toType == 1 ? convertFahrenheitToCelsius(inTemp) : convertFahrenheitToKelvin(inTemp);
                break;
            case 3:
                convertedTemp = toType == 1 ? convertKelvinToCelsius(inTemp) : convertKelvinToFahrenheit(inTemp);
                break;
            default:
                convertedTemp = 0;
        }
        System.out.println("Converted value: " + convertedTemp + getUnit(toType));
    }
    sc.close();
}

public static float convertCelsiusToFahrenheit(float celsius) {
    return (celsius * 9 / 5) + 32;
}

public static float convertCelsiusToKelvin(float celsius) {
    return celsius + 273.15f;
}

public static float convertFahrenheitToCelsius(float fahrenheit) {
    return (fahrenheit - 32) * 5 / 9;
}

public static float convertFahrenheitToKelvin(float fahrenheit) {
    return (fahrenheit + 459.67f) * 5 / 9;
}

public static float convertKelvinToCelsius(float kelvin) {
    return kelvin - 273.15f;
}

public static float convertKelvinToFahrenheit(float kelvin) {
    return (kelvin * 9 / 5) - 459.67f;
}

public static String getUnit(int type) {
    switch (type) {
        case 1:
            return "C";
        case 2:
            return "F";
        case 3:
            return "K";
        default:
            return "";
    }
}

}


/**
* This method converts a Kelvin to Celsius.
*
* @param param float paramter
* @return String
*/
public static String convertKelvinToCelsius(float inTemp) {

double result = 0;
result = inTemp - 273.15;
return rnd.format(result);
}

/**
* This method converts a Kelvin to Fahrenheit.
*
* @param param float paramter
* @return String
*/
public static String convertKelvinToFahrenheit(float inTemp) {

double result = 0;
result = (inTemp * 1.8) - 459.67;
return rnd.format(result);
}

/**
* This method converts a Fahrenheit to Kelvin.
*
* @param param float paramter
* @return String
*/
public static String convertFahrenheitToKelvin(float inTemp) {

double result = 0;
result = (inTemp + 459.67) / 1.8;
return rnd.format(result);
}

/**
* This method converts a Fahrenheit to Kelvin.
*
* @param param float paramter
* @return String
*/
public static String convertFahrenheitToCelsius(float inTemp) {

double result = 0;
result = (inTemp - 32) / 1.8;
return rnd.format(result);
}

/**
* This method converts a Celsius to Fahrenheit.
*
* @param param float paramter
* @return String
*/
public static String convertCelsiusToFahrenheit(float inTemp) {

double result = 0;
result = (inTemp * 1.8) + 32;
return rnd.format(result);
}

/**
* This method converts a Celsius to Kelvin.
*
* @param param float paramter
* @return String
*/
public static String convertCelsiusToKelvin(float inTemp) {

double result = 0;
result = inTemp + 273.15;
return rnd.format(result);
}

/*
* Reference URL : https://en.wikipedia.org/wiki/Conversion_of_scales_of_temperature
* This is not the most effecient method for Temp conversion But its easy to
* understand and straight forward
*/
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Due to linear nature of each conversion, each each pair of units, should be tested with at least two input values.

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.thealgorithms.conversions;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

public class TemperatureConversionTest {

@Test
public void testTemperatureConversion() {
assertEquals("326.15", TemperatureConversion.convertCelsiusToKelvin(53));
assertEquals("89.60", TemperatureConversion.convertCelsiusToFahrenheit(32));
assertEquals("179.85", TemperatureConversion.convertKelvinToCelsius(453));
assertEquals("137.93", TemperatureConversion.convertKelvinToFahrenheit(332));
assertEquals("8.33", TemperatureConversion.convertFahrenheitToCelsius(47));
assertEquals("273.71", TemperatureConversion.convertFahrenheitToKelvin(33));
}
}