-
Notifications
You must be signed in to change notification settings - Fork 19.9k
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
TharunSM
wants to merge
8
commits into
TheAlgorithms:master
from
TharunSM:Temperature-Conversion-issue#5042
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
97dd5f1
TempConversion-Celsius
TharunSM 309fecc
Added all the functionality and tested
TharunSM 9f11dbd
Added reference URL and clang-format configuration
TharunSM 272f4a1
clang-format check
TharunSM 29a9bc9
clang-format fix
TharunSM 1ee0cb1
Few fix in code and added Junit test
TharunSM ed22de7
changed temp conversion to slope and intercept type and also changed …
TharunSM 7b053dc
Merge branch 'master' into Temperature-Conversion-issue#5042
TharunSM File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
100 changes: 100 additions & 0 deletions
100
src/main/java/com/thealgorithms/conversions/TemperatureConversion.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
18 changes: 18 additions & 0 deletions
18
src/test/java/com/thealgorithms/conversions/TemperatureConversionTest.java
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.