-
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
Temperature conversion added to conversions - issue#5042 #5048
Conversation
src/main/java/com/thealgorithms/conversions/TemperatureConversion.java
Outdated
Show resolved
Hide resolved
added the junit test required |
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.
code revisited
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.
What do you think about such approach (pseudocode)
public class TemperatureConversion {
static class AffineConverter {
final double slope;
final double intercept;
public 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() {
return new AffineConverter(...);
}
}
public static final AffineConverter kelvinToCelsius = new AffineConverter(1.0, -273.15);
public static final AffineConverter celsiusToKelvin = kelvinToCelsius.invert();
public static final AffineConverter celsiusToFahrenheit = new AffineConverter(...);
public static final AffineConverter kelvinToFahrenheit = kelvinToCelsius.compose(celsiusToFahrenheit); // or the other way around
}
Then it can be used as:
final var absoluteZeroInCelsius = TemperatureConversion.kelvinToCelsius.convert(0.0);
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(); | ||
} |
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.
This is not needed.
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(); | |
} |
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.
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
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.
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 "";
}
}
}
* Object of DecimalFormat Class | ||
* to round the output to 2 decimal point value | ||
*/ | ||
private static final DecimalFormat rnd = new DecimalFormat("0.00"); |
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.
Why can't we just convert double
to double
? I would suggest that.
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.
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
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.
do u want to use Affine Converter instead of the conditional if statements?
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.
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).
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.
ok ill look into it
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.
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
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.
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.
have change the code to sloap and intercept type, please review it |
@Tharuntech please have a look again at my previous comment: #5048 (review) The piece of code there has just few gaps. Please fill them. |
im not sure what do u mean |
the code u commented doesnt provide coversion for all the temp types |
This is one of the gaps, which needs to be filled. |
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")); | ||
} | ||
} |
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.
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); | |
} | |
} |
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); | ||
} | ||
} |
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.
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(); | |
} |
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.
Due to linear nature of each conversion, each each pair of units, should be tested with at least two input values.
@Tharuntech how is the progress? |
This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contribution! |
Please reopen this pull request once you have made the required changes. If you need help, feel free to ask in our Discord server or ping one of the maintainers here. Thank you for your contribution! |
clang-format -i --style=file path/to/your/file.java