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 all 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,100 @@
package com.thealgorithms.conversions;

/**
* Convets the temperatures from user to Celsius/Fahrenheit/Kelvin
*
* @author Tharun S.M.
*/

public class TemperatureConversion {
private static final double CELSIUS_SLOPE = 1.0;
private static final double CELSIUS_INTERCEPT = 0.0;
private static final double FAHRENHEIT_SLOPE = 1.8;
private static final double FAHRENHEIT_INTERCEPT = 32.0;
private static final double KELVIN_SLOPE = 1.0;
private static final double KELVIN_INTERCEPT = 273.15;

/**
* This function convert current temperature type to required temperature type
*
* @param temperature double, fromType String & toType String
* @return double
*/
public static double convertTemperature(double temperature, String fromType, String toType) {

if (fromType.equalsIgnoreCase(toType)) {
return temperature;
}

double[] slopeAndIntercept = getSlopeAndIntercept(fromType, toType);

double fromSlope = slopeAndIntercept[0];
double fromIntercept = slopeAndIntercept[1];
double toSlope = slopeAndIntercept[2];
double toIntercept = slopeAndIntercept[3];
// converting to celsius temperature first, then to our required temperature
double celsiusTemperature = (temperature - fromIntercept) / fromSlope;
return celsiusTemperature * toSlope + toIntercept;
}

/**
* This function returns Slopes and Intercepts of from and to temperature types
*
* @param fromType String & toType String
* @return double array of Slopes and Intercepts
*/
public static double[] getSlopeAndIntercept(String fromType, String toType) {

double[] slopeAndIntercept = new double[4];
if (fromType.equalsIgnoreCase("Celsius") && toType.equalsIgnoreCase("Fahrenheit")) {
slopeAndIntercept[0] = CELSIUS_SLOPE;
slopeAndIntercept[1] = CELSIUS_INTERCEPT;
slopeAndIntercept[2] = FAHRENHEIT_SLOPE;
slopeAndIntercept[3] = FAHRENHEIT_INTERCEPT;
return slopeAndIntercept;
} else if (fromType.equalsIgnoreCase("Fahrenheit") && toType.equalsIgnoreCase("Celsius")) {
slopeAndIntercept[0] = FAHRENHEIT_SLOPE;
slopeAndIntercept[1] = FAHRENHEIT_INTERCEPT;
slopeAndIntercept[2] = CELSIUS_SLOPE;
slopeAndIntercept[3] = CELSIUS_INTERCEPT;
return slopeAndIntercept;
} else if (fromType.equalsIgnoreCase("Celsius") && toType.equalsIgnoreCase("Kelvin")) {
slopeAndIntercept[0] = CELSIUS_SLOPE;
slopeAndIntercept[1] = CELSIUS_INTERCEPT;
slopeAndIntercept[2] = KELVIN_SLOPE;
slopeAndIntercept[3] = KELVIN_INTERCEPT;
return slopeAndIntercept;
} else if (fromType.equalsIgnoreCase("Kelvin") && toType.equalsIgnoreCase("Celsius")) {
slopeAndIntercept[0] = KELVIN_SLOPE;
slopeAndIntercept[1] = KELVIN_INTERCEPT;
slopeAndIntercept[2] = CELSIUS_SLOPE;
slopeAndIntercept[3] = CELSIUS_INTERCEPT;
return slopeAndIntercept;
} else if (fromType.equalsIgnoreCase("Kelvin") && toType.equalsIgnoreCase("Fahrenheit")) {
slopeAndIntercept[0] = KELVIN_SLOPE;
slopeAndIntercept[1] = KELVIN_INTERCEPT;
slopeAndIntercept[2] = FAHRENHEIT_SLOPE;
slopeAndIntercept[3] = FAHRENHEIT_INTERCEPT;
return slopeAndIntercept;
} else if (fromType.equalsIgnoreCase("Fahrenheit") && toType.equalsIgnoreCase("Kelvin")) {
slopeAndIntercept[0] = FAHRENHEIT_SLOPE;
slopeAndIntercept[1] = FAHRENHEIT_INTERCEPT;
slopeAndIntercept[2] = KELVIN_SLOPE;
slopeAndIntercept[3] = KELVIN_INTERCEPT;
return slopeAndIntercept;
} else {
throw new IllegalArgumentException("Unsupported temperature conversion");
}
}

public static void main(String[] args) {

double temperature = 100;
String fromType = "Celsius";
String toType = "Fahrenheit";

double convertedTemperature = convertTemperature(temperature, fromType, toType);

System.out.println("Converted temperature: " + convertedTemperature);
}
}
Comment on lines +9 to +100
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
public class TemperatureConversion {
private static final double CELSIUS_SLOPE = 1.0;
private static final double CELSIUS_INTERCEPT = 0.0;
private static final double FAHRENHEIT_SLOPE = 1.8;
private static final double FAHRENHEIT_INTERCEPT = 32.0;
private static final double KELVIN_SLOPE = 1.0;
private static final double KELVIN_INTERCEPT = 273.15;
/**
* This function convert current temperature type to required temperature type
*
* @param temperature double, fromType String & toType String
* @return double
*/
public static double convertTemperature(double temperature, String fromType, String toType) {
if (fromType.equalsIgnoreCase(toType)) {
return temperature;
}
double[] slopeAndIntercept = getSlopeAndIntercept(fromType, toType);
double fromSlope = slopeAndIntercept[0];
double fromIntercept = slopeAndIntercept[1];
double toSlope = slopeAndIntercept[2];
double toIntercept = slopeAndIntercept[3];
// converting to celsius temperature first, then to our required temperature
double celsiusTemperature = (temperature - fromIntercept) / fromSlope;
return celsiusTemperature * toSlope + toIntercept;
}
/**
* This function returns Slopes and Intercepts of from and to temperature types
*
* @param fromType String & toType String
* @return double array of Slopes and Intercepts
*/
public static double[] getSlopeAndIntercept(String fromType, String toType) {
double[] slopeAndIntercept = new double[4];
if (fromType.equalsIgnoreCase("Celsius") && toType.equalsIgnoreCase("Fahrenheit")) {
slopeAndIntercept[0] = CELSIUS_SLOPE;
slopeAndIntercept[1] = CELSIUS_INTERCEPT;
slopeAndIntercept[2] = FAHRENHEIT_SLOPE;
slopeAndIntercept[3] = FAHRENHEIT_INTERCEPT;
return slopeAndIntercept;
} else if (fromType.equalsIgnoreCase("Fahrenheit") && toType.equalsIgnoreCase("Celsius")) {
slopeAndIntercept[0] = FAHRENHEIT_SLOPE;
slopeAndIntercept[1] = FAHRENHEIT_INTERCEPT;
slopeAndIntercept[2] = CELSIUS_SLOPE;
slopeAndIntercept[3] = CELSIUS_INTERCEPT;
return slopeAndIntercept;
} else if (fromType.equalsIgnoreCase("Celsius") && toType.equalsIgnoreCase("Kelvin")) {
slopeAndIntercept[0] = CELSIUS_SLOPE;
slopeAndIntercept[1] = CELSIUS_INTERCEPT;
slopeAndIntercept[2] = KELVIN_SLOPE;
slopeAndIntercept[3] = KELVIN_INTERCEPT;
return slopeAndIntercept;
} else if (fromType.equalsIgnoreCase("Kelvin") && toType.equalsIgnoreCase("Celsius")) {
slopeAndIntercept[0] = KELVIN_SLOPE;
slopeAndIntercept[1] = KELVIN_INTERCEPT;
slopeAndIntercept[2] = CELSIUS_SLOPE;
slopeAndIntercept[3] = CELSIUS_INTERCEPT;
return slopeAndIntercept;
} else if (fromType.equalsIgnoreCase("Kelvin") && toType.equalsIgnoreCase("Fahrenheit")) {
slopeAndIntercept[0] = KELVIN_SLOPE;
slopeAndIntercept[1] = KELVIN_INTERCEPT;
slopeAndIntercept[2] = FAHRENHEIT_SLOPE;
slopeAndIntercept[3] = FAHRENHEIT_INTERCEPT;
return slopeAndIntercept;
} else if (fromType.equalsIgnoreCase("Fahrenheit") && toType.equalsIgnoreCase("Kelvin")) {
slopeAndIntercept[0] = FAHRENHEIT_SLOPE;
slopeAndIntercept[1] = FAHRENHEIT_INTERCEPT;
slopeAndIntercept[2] = KELVIN_SLOPE;
slopeAndIntercept[3] = KELVIN_INTERCEPT;
return slopeAndIntercept;
} else {
throw new IllegalArgumentException("Unsupported temperature conversion");
}
}
public static void main(String[] args) {
double temperature = 100;
String fromType = "Celsius";
String toType = "Fahrenheit";
double convertedTemperature = convertTemperature(temperature, fromType, toType);
System.out.println("Converted temperature: " + convertedTemperature);
}
}
public class TemperatureConversion {
static class AffineConverter {
final double slope;
final double intercept;
AffineConverter(final double inSlope, final double inIntercept) {
slope = inSlope;
intercept = inIntercept;
}
public double convert(final double inValue) {
return slope * inValue + intercept;
}
AffineConverter invert() {
assert (slope != 0.0);
return new AffineConverter(1.0 / slope, -intercept / slope);
}
AffineConverter compose(final AffineConverter other) {
return new AffineConverter(slope * other.slope, intercept * other.slope + other.intercept);
}
}
public static final AffineConverter KELVIN_TO_CELSIUS = new AffineConverter(1.0, -273.15);
public static final AffineConverter CELSIUS_TO_KELVIN = KELVIN_TO_CELSIUS.invert();
public static final AffineConverter CELSIUS_TO_FAHRENHEIT = new AffineConverter(9.0 / 5.0, 32);
public static final AffineConverter FAHRENHEIT_TO_CELSIUS = CELSIUS_TO_FAHRENHEIT.invert();
public static final AffineConverter KELVIN_TO_FAHRENHEIT = KELVIN_TO_CELSIUS.compose(CELSIUS_TO_FAHRENHEIT);
public static final AffineConverter FAHRENHEIT_TO_KELVIN = KELVIN_TO_FAHRENHEIT.invert();
}

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;

class TemperatureConversionTest {

@Test
void testTemperatureConversion() {
assertEquals(326.15, TemperatureConversion.convertTemperature(53, "Celsius", "Kelvin"));
assertEquals(127.4, TemperatureConversion.convertTemperature(53, "Celsius", "Fahrenheit"));
assertEquals(-220.14999999999998, TemperatureConversion.convertTemperature(53, "Kelvin", "Celsius"));
assertEquals(-364.27, TemperatureConversion.convertTemperature(53, "Kelvin", "Fahrenheit"));
assertEquals(11.666666666666666, TemperatureConversion.convertTemperature(53, "Fahrenheit", "Celsius"));
assertEquals(284.81666666666666, TemperatureConversion.convertTemperature(53, "Fahrenheit", "Kelvin"));
}
}
Comment on lines +7 to +18
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
class TemperatureConversionTest {
@Test
void testTemperatureConversion() {
assertEquals(326.15, TemperatureConversion.convertTemperature(53, "Celsius", "Kelvin"));
assertEquals(127.4, TemperatureConversion.convertTemperature(53, "Celsius", "Fahrenheit"));
assertEquals(-220.14999999999998, TemperatureConversion.convertTemperature(53, "Kelvin", "Celsius"));
assertEquals(-364.27, TemperatureConversion.convertTemperature(53, "Kelvin", "Fahrenheit"));
assertEquals(11.666666666666666, TemperatureConversion.convertTemperature(53, "Fahrenheit", "Celsius"));
assertEquals(284.81666666666666, TemperatureConversion.convertTemperature(53, "Fahrenheit", "Kelvin"));
}
}
class TemperatureConversionTest {
static final double EPS = 1e-10;
@Test
void testTemperatureConversion() {
assertEquals(326.15, TemperatureConversion.CELSIUS_TO_KELVIN.convert(53), EPS);
assertEquals(127.4, TemperatureConversion.CELSIUS_TO_FAHRENHEIT.convert(53), EPS);
assertEquals(-220.15, TemperatureConversion.KELVIN_TO_CELSIUS.convert(53), EPS);
assertEquals(-364.27, TemperatureConversion.KELVIN_TO_FAHRENHEIT.convert(53), EPS);
assertEquals(11.666666666666666, TemperatureConversion.FAHRENHEIT_TO_CELSIUS.convert(53), EPS);
assertEquals(284.81666666666666, TemperatureConversion.FAHRENHEIT_TO_KELVIN.convert(53), EPS);
}
}