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

Conversation

TharunSM
Copy link

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized it.
  • All filenames are in PascalCase.
  • All functions and variable names follow Java naming conventions.
  • All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
  • All new code is formatted with clang-format -i --style=file path/to/your/file.java

@TharunSM
Copy link
Author

added the junit test required

Copy link
Author

@TharunSM TharunSM left a comment

Choose a reason for hiding this comment

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

code revisited

@TharunSM TharunSM requested a review from vil02 February 15, 2024 06:18
Copy link
Member

@vil02 vil02 left a 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);

Comment on lines 23 to 64
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 "";
    }
}

}

* 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.

@TharunSM
Copy link
Author

have change the code to sloap and intercept type, please review it

@TharunSM TharunSM requested a review from vil02 February 18, 2024 07:47
@vil02
Copy link
Member

vil02 commented Feb 19, 2024

@Tharuntech please have a look again at my previous comment: #5048 (review)

The piece of code there has just few gaps. Please fill them.

@TharunSM
Copy link
Author

im not sure what do u mean

@TharunSM
Copy link
Author

the code u commented doesnt provide coversion for all the temp types

@vil02
Copy link
Member

vil02 commented Feb 22, 2024

the code u commented doesnt provide coversion for all the temp types

This is one of the gaps, which needs to be filled.

Comment on lines +7 to +18
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"));
}
}
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);
}
}

Comment on lines +9 to +100
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);
}
}
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.

@vil02
Copy link
Member

vil02 commented Mar 19, 2024

@Tharuntech how is the progress?

Copy link

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!

@github-actions github-actions bot added the stale label Apr 27, 2024
Copy link

github-actions bot commented May 4, 2024

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!

@github-actions github-actions bot closed this May 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants