Skip to content

Commit ed22de7

Browse files
committed
changed temp conversion to slope and intercept type and also changed test cases
1 parent 1ee0cb1 commit ed22de7

File tree

2 files changed

+80
-129
lines changed

2 files changed

+80
-129
lines changed
Lines changed: 72 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -1,149 +1,100 @@
11
package com.thealgorithms.conversions;
22

3-
import java.text.DecimalFormat;
4-
import java.util.Scanner;
53
/**
64
* Convets the temperatures from user to Celsius/Fahrenheit/Kelvin
75
*
86
* @author Tharun S.M.
97
*/
108

119
public class TemperatureConversion {
12-
/*
13-
* Object of DecimalFormat Class
14-
* to round the output to 2 decimal point value
15-
*/
16-
private static final DecimalFormat rnd = new DecimalFormat("0.00");
17-
/*
18-
* Main method
19-
*
20-
* @param args Command line arguments
21-
*/
22-
23-
public static void main(String[] args) {
24-
25-
Scanner sc = new Scanner(System.in);
26-
27-
// Get the type of temprature input
28-
System.out.println("Enter the temperature input type (use numbers)");
29-
System.out.println("1 => Celsius");
30-
System.out.println("2 => Fahrenheit");
31-
System.out.println("3 => Kelvin");
32-
int fromType = sc.nextInt();
33-
34-
// get the actual temprature
35-
System.out.println("Enter the temperature :");
36-
String a = sc.next();
37-
float inTemp = Float.parseFloat(a);
38-
39-
// get the type you want to convert your temperature to
40-
System.out.println("Type you want to convert your temperature to (use numbers)");
41-
System.out.println("1 => Celsius");
42-
System.out.println("2 => Fahrenheit");
43-
System.out.println("3 => Kelvin");
44-
int toType = sc.nextInt();
45-
46-
if (fromType == toType) {
47-
System.out.println("Your Temperature is the same !!");
48-
} else if (fromType == 1 && toType == 2) {
49-
System.out.println("Converted value: " + convertCelsiusToFahrenheit(inTemp) + " °F");
50-
} else if (fromType == 1 && toType == 3) {
51-
System.out.println("Converted value: " + convertCelsiusToKelvin(inTemp) + " K");
52-
} else if (fromType == 2 && toType == 1) {
53-
System.out.println("Converted value: " + convertFahrenheitToCelsius(inTemp) + " °C");
54-
} else if (fromType == 2 && toType == 3) {
55-
System.out.println("Converted value: " + convertFahrenheitToKelvin(inTemp) + " K");
56-
} else if (fromType == 3 && toType == 1) {
57-
System.out.println("Converted value: " + convertKelvinToCelsius(inTemp) + " °C");
58-
} else if (fromType == 3 && toType == 2) {
59-
System.out.println("Converted value: " + convertKelvinToFahrenheit(inTemp) + " °F");
60-
} else {
61-
System.out.println("Please check your input and output types");
62-
}
63-
sc.close();
64-
}
65-
66-
/**
67-
* This method converts a Kelvin to Celsius.
68-
*
69-
* @param param float paramter
70-
* @return String
71-
*/
72-
public static String convertKelvinToCelsius(float inTemp) {
73-
74-
double result = 0;
75-
result = inTemp - 273.15;
76-
return rnd.format(result);
77-
}
10+
private static final double CELSIUS_SLOPE = 1.0;
11+
private static final double CELSIUS_INTERCEPT = 0.0;
12+
private static final double FAHRENHEIT_SLOPE = 1.8;
13+
private static final double FAHRENHEIT_INTERCEPT = 32.0;
14+
private static final double KELVIN_SLOPE = 1.0;
15+
private static final double KELVIN_INTERCEPT = 273.15;
7816

7917
/**
80-
* This method converts a Kelvin to Fahrenheit.
18+
* This function convert current temperature type to required temperature type
8119
*
82-
* @param param float paramter
83-
* @return String
20+
* @param temperature double, fromType String & toType String
21+
* @return double
8422
*/
85-
public static String convertKelvinToFahrenheit(float inTemp) {
23+
public static double convertTemperature(double temperature, String fromType, String toType) {
8624

87-
double result = 0;
88-
result = (inTemp * 1.8) - 459.67;
89-
return rnd.format(result);
90-
}
25+
if (fromType.equalsIgnoreCase(toType)) {
26+
return temperature;
27+
}
9128

92-
/**
93-
* This method converts a Fahrenheit to Kelvin.
94-
*
95-
* @param param float paramter
96-
* @return String
97-
*/
98-
public static String convertFahrenheitToKelvin(float inTemp) {
29+
double[] slopeAndIntercept = getSlopeAndIntercept(fromType, toType);
9930

100-
double result = 0;
101-
result = (inTemp + 459.67) / 1.8;
102-
return rnd.format(result);
31+
double fromSlope = slopeAndIntercept[0];
32+
double fromIntercept = slopeAndIntercept[1];
33+
double toSlope = slopeAndIntercept[2];
34+
double toIntercept = slopeAndIntercept[3];
35+
// converting to celsius temperature first, then to our required temperature
36+
double celsiusTemperature = (temperature - fromIntercept) / fromSlope;
37+
return celsiusTemperature * toSlope + toIntercept;
10338
}
10439

10540
/**
106-
* This method converts a Fahrenheit to Kelvin.
41+
* This function returns Slopes and Intercepts of from and to temperature types
10742
*
108-
* @param param float paramter
109-
* @return String
43+
* @param fromType String & toType String
44+
* @return double array of Slopes and Intercepts
11045
*/
111-
public static String convertFahrenheitToCelsius(float inTemp) {
112-
113-
double result = 0;
114-
result = (inTemp - 32) / 1.8;
115-
return rnd.format(result);
46+
public static double[] getSlopeAndIntercept(String fromType, String toType) {
47+
48+
double[] slopeAndIntercept = new double[4];
49+
if (fromType.equalsIgnoreCase("Celsius") && toType.equalsIgnoreCase("Fahrenheit")) {
50+
slopeAndIntercept[0] = CELSIUS_SLOPE;
51+
slopeAndIntercept[1] = CELSIUS_INTERCEPT;
52+
slopeAndIntercept[2] = FAHRENHEIT_SLOPE;
53+
slopeAndIntercept[3] = FAHRENHEIT_INTERCEPT;
54+
return slopeAndIntercept;
55+
} else if (fromType.equalsIgnoreCase("Fahrenheit") && toType.equalsIgnoreCase("Celsius")) {
56+
slopeAndIntercept[0] = FAHRENHEIT_SLOPE;
57+
slopeAndIntercept[1] = FAHRENHEIT_INTERCEPT;
58+
slopeAndIntercept[2] = CELSIUS_SLOPE;
59+
slopeAndIntercept[3] = CELSIUS_INTERCEPT;
60+
return slopeAndIntercept;
61+
} else if (fromType.equalsIgnoreCase("Celsius") && toType.equalsIgnoreCase("Kelvin")) {
62+
slopeAndIntercept[0] = CELSIUS_SLOPE;
63+
slopeAndIntercept[1] = CELSIUS_INTERCEPT;
64+
slopeAndIntercept[2] = KELVIN_SLOPE;
65+
slopeAndIntercept[3] = KELVIN_INTERCEPT;
66+
return slopeAndIntercept;
67+
} else if (fromType.equalsIgnoreCase("Kelvin") && toType.equalsIgnoreCase("Celsius")) {
68+
slopeAndIntercept[0] = KELVIN_SLOPE;
69+
slopeAndIntercept[1] = KELVIN_INTERCEPT;
70+
slopeAndIntercept[2] = CELSIUS_SLOPE;
71+
slopeAndIntercept[3] = CELSIUS_INTERCEPT;
72+
return slopeAndIntercept;
73+
} else if (fromType.equalsIgnoreCase("Kelvin") && toType.equalsIgnoreCase("Fahrenheit")) {
74+
slopeAndIntercept[0] = KELVIN_SLOPE;
75+
slopeAndIntercept[1] = KELVIN_INTERCEPT;
76+
slopeAndIntercept[2] = FAHRENHEIT_SLOPE;
77+
slopeAndIntercept[3] = FAHRENHEIT_INTERCEPT;
78+
return slopeAndIntercept;
79+
} else if (fromType.equalsIgnoreCase("Fahrenheit") && toType.equalsIgnoreCase("Kelvin")) {
80+
slopeAndIntercept[0] = FAHRENHEIT_SLOPE;
81+
slopeAndIntercept[1] = FAHRENHEIT_INTERCEPT;
82+
slopeAndIntercept[2] = KELVIN_SLOPE;
83+
slopeAndIntercept[3] = KELVIN_INTERCEPT;
84+
return slopeAndIntercept;
85+
} else {
86+
throw new IllegalArgumentException("Unsupported temperature conversion");
87+
}
11688
}
11789

118-
/**
119-
* This method converts a Celsius to Fahrenheit.
120-
*
121-
* @param param float paramter
122-
* @return String
123-
*/
124-
public static String convertCelsiusToFahrenheit(float inTemp) {
90+
public static void main(String[] args) {
12591

126-
double result = 0;
127-
result = (inTemp * 1.8) + 32;
128-
return rnd.format(result);
129-
}
92+
double temperature = 100;
93+
String fromType = "Celsius";
94+
String toType = "Fahrenheit";
13095

131-
/**
132-
* This method converts a Celsius to Kelvin.
133-
*
134-
* @param param float paramter
135-
* @return String
136-
*/
137-
public static String convertCelsiusToKelvin(float inTemp) {
96+
double convertedTemperature = convertTemperature(temperature, fromType, toType);
13897

139-
double result = 0;
140-
result = inTemp + 273.15;
141-
return rnd.format(result);
98+
System.out.println("Converted temperature: " + convertedTemperature);
14299
}
143-
144-
/*
145-
* Reference URL : https://en.wikipedia.org/wiki/Conversion_of_scales_of_temperature
146-
* This is not the most effecient method for Temp conversion But its easy to
147-
* understand and straight forward
148-
*/
149100
}

src/test/java/com/thealgorithms/conversions/TemperatureConversionTest.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
import org.junit.jupiter.api.Test;
66

7-
public class TemperatureConversionTest {
7+
class TemperatureConversionTest {
88

99
@Test
10-
public void testTemperatureConversion() {
11-
assertEquals("326.15", TemperatureConversion.convertCelsiusToKelvin(53));
12-
assertEquals("89.60", TemperatureConversion.convertCelsiusToFahrenheit(32));
13-
assertEquals("179.85", TemperatureConversion.convertKelvinToCelsius(453));
14-
assertEquals("137.93", TemperatureConversion.convertKelvinToFahrenheit(332));
15-
assertEquals("8.33", TemperatureConversion.convertFahrenheitToCelsius(47));
16-
assertEquals("273.71", TemperatureConversion.convertFahrenheitToKelvin(33));
10+
void testTemperatureConversion() {
11+
assertEquals(326.15, TemperatureConversion.convertTemperature(53, "Celsius", "Kelvin"));
12+
assertEquals(127.4, TemperatureConversion.convertTemperature(53, "Celsius", "Fahrenheit"));
13+
assertEquals(-220.14999999999998, TemperatureConversion.convertTemperature(53, "Kelvin", "Celsius"));
14+
assertEquals(-364.27, TemperatureConversion.convertTemperature(53, "Kelvin", "Fahrenheit"));
15+
assertEquals(11.666666666666666, TemperatureConversion.convertTemperature(53, "Fahrenheit", "Celsius"));
16+
assertEquals(284.81666666666666, TemperatureConversion.convertTemperature(53, "Fahrenheit", "Kelvin"));
1717
}
1818
}

0 commit comments

Comments
 (0)