Hi I need help creating a program in JAVA. This material is covered in the Intro
ID: 3917119 • Letter: H
Question
Hi I need help creating a program in JAVA. This material is covered in the Intro to JAVA textbook.
Temp Converter2 - Write a program that is based on a menu that will allow a user to enter a temp as either Kelvin, Celsius, or Fahrenheit. The user then will select from a menu as to what measurement they wish to convert to.
A menu that gives the user the a choice of entering a new value, processing/displaying the associated value, or exit
Selection (1) or (E)nter - The user is prompted for a temperature, lets say they type 100. and then K, C, or F for the scale
The user then is returned to the menu for their next choice
If they choose (2) or (P)rocess they should be asked which scale they wish to convert to), and number and scale stored should be converted and the new scale and displaye
The user then is returned to the menu for their next choice
Explanation / Answer
Given below is the code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you
TemperatureConvert.java
------------
import java.util.Scanner;
public class TemperatureConvert {
public static void main(String[] args) {
double celsius= 0;
double input = 0, output = 0;
int choice = 0;
char unit1 = 'C', unit2 ;
Scanner keyboard = new Scanner(System.in);
while(choice != 3)
{
System.out.println("1. Input temperature");
System.out.println("2. Process temperature");
System.out.println("3. Quit");
System.out.print("Enter your choice: ");
choice = keyboard.nextInt();
switch(choice){
case 1:
System.out.print("Enter temperature (E.g. 98.6 F, 310.15 K, 37 C): ");
input = keyboard.nextDouble();
unit1 = keyboard.next().toUpperCase().charAt(0);
celsius = convertToCelsius(input, unit1); //always store in celsius irrespective of what user entered
break;
case 2:
System.out.print("Which scale to convert to C, F or K? ");
unit2 = keyboard.next().toUpperCase().charAt(0);
output = convertFromCelsius(celsius, unit2);
System.out.printf("%.1f %c = %.1f %c ", input, unit1, output, unit2);
case 3:
break;
default:
System.out.println("Invalid menu choice!");
}
}
}
static double convertToCelsius(double temp, char from)
{
if(from == 'C')
return temp;
else if(from == 'F')
return (temp - 32) * 5/9;
else if(from == 'K')
return temp - 273.15;
else
return 0;
}
static double convertFromCelsius(double temp, char to){
if(to == 'C')
return temp;
else if(to == 'F')
return (temp * 9/5) + 32;
else if(to == 'K')
return temp + 273.15;
else
return 0;
}
}
output
----
1. Input temperature
2. Process temperature
3. Quit
Enter your choice: 1
Enter temperature (E.g. 98.6 F, 310.15 K, 37 C): 37 C
1. Input temperature
2. Process temperature
3. Quit
Enter your choice: 2
Which scale to convert to C, F or K? F
37.0 C = 98.6 F
1. Input temperature
2. Process temperature
3. Quit
Enter your choice: 2
Which scale to convert to C, F or K? K
37.0 C = 310.2 K
1. Input temperature
2. Process temperature
3. Quit
Enter your choice: 2
Which scale to convert to C, F or K? C
37.0 C = 37.0 C
1. Input temperature
2. Process temperature
3. Quit
Enter your choice: 1
Enter temperature (E.g. 98.6 F, 310.15 K, 37 C): 100 F
1. Input temperature
2. Process temperature
3. Quit
Enter your choice: 2
Which scale to convert to C, F or K? C
100.0 F = 37.8 C
1. Input temperature
2. Process temperature
3. Quit
Enter your choice: 2
Which scale to convert to C, F or K? F
100.0 F = 100.0 F
1. Input temperature
2. Process temperature
3. Quit
Enter your choice: 2
Which scale to convert to C, F or K? K
100.0 F = 310.9 K
1. Input temperature
2. Process temperature
3. Quit
Enter your choice: 3