Temperature Converter (°C,°F, K) in Java

Hello guys, in this article we will create a Temperature Converter for Celsius, Fahrenheit and Kelvin units in Java.

The formulae for conversation between the units are given below:

1. Celcius to Fahrenheit: °F = (°C × 9/5) + 32
2. Celcius to Kelvin: °C + 273.15
3. Fahrenheit to Celcius: °C = (°F − 32) × 5/9
4. Fahrenheit to Kelvin: K = (°F − 32) × 5/9 + 273.15
5. Kelvin to Celcius: °C = K − 273.15
6. Kelvin to Fahrenheit: °F = (K − 273.15) × 9/5 + 32

In our program for the temperature converter, we will create six methods, each handling the conversions mentioned above. From each method, we will return the converted value to the main method.

We will also create two more methods. One to scan the input from the user and the other to print the converted value.
The input method will return the value entered by the user to main method.

The program will provide options from 1-7 and ask the user to enter a choice. If the user enters a number between 1-6, the program will perform that particular conversion. Choice 7 will exit the program. To achieve this, we will use a switch statement in the do-while loop. The loop will continue to accept choices and perform conversions until the user enters 7.

Code:

import java.util.*;
class tempConverter{
static Scanner sc = new Scanner(System.in); // Scanner Class

// Method to convert Celcius to Fahrenheit
static double C_F(double C){
double F = (C * 9/5) + 32;
return F;
}
// Method to convert Celcius to Kelvin
static double C_K(double C){
double K = C + 273.15;
return K;
}

// Method to convert Fahrenheit to Celcius
static double F_C(double F){
double C = (F - 32) * 5/9;
return C;
}
// Method to convert Fahrenheit to Kelvin
static double F_K(double F){
double K = (F - 32) * 5/9 + 273.15;
return K;
}

// Method to convert Kelvin to Celcius
static double K_C(double K){
double C = K - 273.15;
return C;
}
// Method to convert Kelvin to Fahrenheit
static double K_F(double K){
double F = (K - 273.15) * 9/5 + 32;
return F;
}

// Method to read the value of temperature given by the user
static double input(String word){
System.out.println("Enter "+word+" value:");
double val = sc.nextDouble();
return val;
}

// Method to print converted value of temperature
static void output(double val, String word){
System.out.printf("%s value: %.2f",word,val);
}

// Driver Method
public static void main(String args[]){
System.out.println("1. Celcius to Fahrenheit\n2. Celcius to Kelvin\n"+
"3. Fahrenheit to Celcius\n4. Fahrenheit to Kelvin\n"+
"5. Kelvin to Celcius\n6. Kelvin to Fahrenheit\n7. Exit");
do{
System.out.println("\nEnter Choice: ");
int ch = sc.nextInt();
double num = 0;
switch(ch){
case 1: num = input("Celcius");
output(C_F(num), "Fahrenheit");
break;
case 2: num = input("Celcius");
output(C_K(num), "Kelvin");
break;
case 3: num = input("Fahrenheit");
output(F_C(num), "Celcius");
break;
case 4: num = input("Fahrenheit");
output(F_K(num), "Kelvin");
break;
case 5: num = input("Kelvin");
output(K_C(num), "Celcius");
break;
case 6: num = input("Kelvin");
output(K_F(num), "Fahrenheit");
break;
case 7: System.exit(0);
break;
default: System.out.println("Invalid Input");
}
}while(true);
}
}

Output:

1. Celcius to Fahrenheit
2. Celcius to Kelvin
3. Fahrenheit to Celcius
4. Fahrenheit to Kelvin
5. Kelvin to Celcius
6. Kelvin to Fahrenheit
7. Exit

Enter Choice:
1
Enter Celcius value:
67.9
Fahrenheit value: 154.22

Enter Choice:
2
Enter Celcius value:
45.88
Kelvin value: 319.03

Enter Choice:
3
Enter Fahrenheit value:
23.4
Celcius value: -4.78

Enter Choice:
4
Enter Fahrenheit value:
34
Kelvin value: 274.26

Enter Choice:
5
Enter Kelvin value:
100
Celcius value: -173.15

Enter Choice:
6
Enter Kelvin value:
273.15
Fahrenheit value: 32.00