In: Computer Science
Temperature Conversion Menu (100 pts)
The three common temperature scales are Celsius, Fahrenheit and Kelvin. The conversion formulae for each of the scales is shown below (where °C, °F and K represent the temperatures in degrees Celsius, degrees Fahrenheit and Kelvin respectively):
Celsius to Fahrenheit: °F = (9.0/5) ´ (°C) + 32
Celsius to Kelvin: K = °C + 273.15
Kelvin to Celsius: °C = K – 273.15
Kelvin to Fahrenheit: °F = (9.0/5) ´ (K – 273.15) + 32
Fahrenheit to Celsius: °C = (5.0/9) ´ (°F – 32)
Fahrenheit to Kelvin: K = (5.0/9) ´ (°F – 32) + 273.15
Write a program that can convert the temperature using the given formulae.
Your code should:
2 – convert from Celsius to Kelvin
3 – convert from Kelvin to Celsius
4 – convert from Kelvin to Fahrenheit
5 – convert from Fahrenheit to Celsius
6 – convert from Fahrenheit to Kelvin
7 – quit the program
Samples of the output are shown below. (Note, match the wording as closely as possible and accomplish the same tasks.)
Please select an option below (1 to 7):
1. Convert from Celsius to Fahrenheit
2. Convert from Celsius to Kelvin
3. Convert from Kelvin to Celsius
4. Convert from Kelvin to Fahrenheit
5. Convert from Fahrenheit to Celsius
6. Convert from Fahrenheit to Kelvin
7. Quit
Enter choice: 8
Invalid entry. Renter choice: 1
Enter the temperature: 100
100.0 degrees Celsius is 212.0 degrees Fahrenheit
Please select an option below (1 to 7):
1. Convert from Celsius to Fahrenheit
2. Convert from Celsius to Kelvin
3. Convert from Kelvin to Celsius
4. Convert from Kelvin to Fahrenheit
5. Convert from Fahrenheit to Celsius
6. Convert from Fahrenheit to Kelvin
7. Quit
Enter choice: 2
Enter the temperature: 0
0.0 degrees Celsius is 273.15 Kelvin
Please select an option below (1 to 7):
1. Convert from Celsius to Fahrenheit
2. Convert from Celsius to Kelvin
3. Convert from Kelvin to Celsius
4. Convert from Kelvin to Fahrenheit
5. Convert from Fahrenheit to Celsius
6. Convert from Fahrenheit to Kelvin
7. Quit
Enter choice: 3
Enter the temperature: -40
-40.0 degrees Fahrenheit is -40.0 degrees Celsius
Please select an option below (1 to 7):
1. Convert from Celsius to Fahrenheit
2. Convert from Celsius to Kelvin
3. Convert from Kelvin to Celsius
4. Convert from Kelvin to Fahrenheit
5. Convert from Fahrenheit to Celsius
6. Convert from Fahrenheit to Kelvin
7. Quit
Enter choice: 4
Enter the temperature: 70
70.0 degrees Fahrenheit is 294.26111111111106 Kelvin
Please select an option below (1 to 7):
1. Convert from Celsius to Fahrenheit
2. Convert from Celsius to Kelvin
3. Convert from Kelvin to Celsius
4. Convert from Kelvin to Fahrenheit
5. Convert from Fahrenheit to Celsius
6. Convert from Fahrenheit to Kelvin
7. Quit
Enter choice: 5
Enter the temperature: 100
100.0 Kelvin is -173.14999999999998 degrees Celsius
Please select an option below (1 to 7):
1. Convert from Celsius to Fahrenheit
2. Convert from Celsius to Kelvin
3. Convert from Kelvin to Celsius
4. Convert from Kelvin to Fahrenheit
5. Convert from Fahrenheit to Celsius
6. Convert from Fahrenheit to Kelvin
7. Quit
Enter choice: 6
Enter the temperature: 100
100.0 Kelvin is -279.66999999999996 degrees Fahrenheit
Please select an option below (1 to 7):
1. Convert from Celsius to Fahrenheit
2. Convert from Celsius to Kelvin
3. Convert from Kelvin to Celsius
4. Convert from Kelvin to Fahrenheit
5. Convert from Fahrenheit to Celsius
6. Convert from Fahrenheit to Kelvin
7. Quit
Enter choice: 7
Ok bye!
Following java program provides menu driven program to convert temperature in one unit to another based on choice.
PFB source code with sample output.
Note : No language was specified, so wrote in java.
--------------------------
TemperatureConversion.java
--------------------------
import java.util.Scanner;
public class TemperatureConversion {
public static double celsiusToFahrenheit(double temperature){
//Celsius to Fahrenheit: °F = (9.0/5) ´ (°C) + 32
return ((9.0/5)*temperature+32);
}
public static double celsiusToKelvin(double temperature){
//Celsius to Kelvin: K = °C + 273.15
return (temperature + 273.15);
}
public static double kelvinToCelsius(double temperature){
//Kelvin to Celsius: °C = K – 273.15
return (temperature - 273.15);
}
public static double kelvinToFahrenheit(double
temperature){
//Kelvin to Fahrenheit: °F = (9.0/5) ´ (K – 273.15) + 32
return ((9.0/5) * (temperature-273.15) + 32);
}
public static double fahrenheitToCelsius(double
temperature){
//Fahrenheit to Celsius: °C = (5.0/9) ´ (°F – 32)
return ((5.0/9)*(temperature-32));
}
public static double fahrenheitToKelvin(double
temperature){
//Fahrenheit to Kelvin: K = (5.0/9) ´ (°F – 32) + 273.15
return ((5.0/9)*(temperature-32)+273.15);
}
public static void menu(){
int choice;
double temperature=0;
Scanner sc = new Scanner(System.in);
do{
System.out.println("\n\n");
System.out.println("Please select an option below (1 to
7):");
System.out.println("1 - Convert from Celsius to Fahrenheit");
System.out.println("2 - Convert from Celsius to Kelvin");
System.out.println("3 - Convert from Kelvin to Celsius");
System.out.println("4 - Convert from Kelvin to Fahrenheit");
System.out.println("5 - Convert from Fahrenheit to Celsius");
System.out.println("6 - Convert from Fahrenheit to Kelvin");
System.out.println("7 - Quit the program");
System.out.print("Enter choice: ");
choice = sc.nextInt();
while(choice<1 || choice>7){
System.out.print("Invalid entry. Renter choice: ");
choice = sc.nextInt();
}
if(choice<=6){
System.out.print("Enter the temperature: ");
temperature = sc.nextDouble();
}
double convertedTemperature;
switch(choice){
case 1: convertedTemperature =
celsiusToFahrenheit(temperature);
System.out.println(temperature + " degrees Celsius is " +
convertedTemperature + " degrees Fahrenheit");
break;
case 2: convertedTemperature = celsiusToKelvin(temperature);
System.out.println(temperature + " degrees Celsius is " +
convertedTemperature + " Kelvin");
break;
case 3: convertedTemperature = kelvinToCelsius(temperature);
System.out.println(temperature + " Kelvin is " +
convertedTemperature + " degrees Celsius");
break;
case 4: convertedTemperature =
kelvinToFahrenheit(temperature);
System.out.println(temperature + " Kelvin is " +
convertedTemperature + " degrees Fahrenheit");
break;
case 5: convertedTemperature =
fahrenheitToCelsius(temperature);
System.out.println(temperature + " degrees Fahrenheit is " +
convertedTemperature + " degrees Celsius");
break;
case 6: convertedTemperature =
fahrenheitToKelvin(temperature);
System.out.println(temperature + " degrees Fahrenheit is " +
convertedTemperature + " Kelvin");
break;
case 7: System.out.println("Ok bye!");
return;
}
}while(true);
}
public static void main(String[] args) {
//calling menu
menu();
}
}
--------------------------
---------------------
sample output
---------------------
---------------------
Let me know, if you face any issue.