Create a program in Java that converts Celsius to Fahrenheit and Fahrenheit to C
ID: 3663989 • Letter: C
Question
Create a program in Java that converts Celsius to Fahrenheit and Fahrenheit to Celsius and displays as a table on the
console as shown in the sample output below. Remember to use proper naming convention for variables. Please do not include arrays and return.
Define 2 value-returning methods & use the following formulas:
Calculate Celsius to Fahrenheit. Fahrenheit = Celsius * 9/5 + 32
Calculate Fahrenheit to Celsius. Celsius = (Fahrenheit - 32) * 5/9;
Using a loop (while loop, do/while loop, or for loop), display 20 lines of results incremented as shown in the
sample output below.
Required: Format columns one and three to 1 decimal place. Format columns two and four to 2 decimal places.
See sample output
The program should be very similar to this sample output:
Celsius Fahrenheit | Fahrenheit Celsius
----------------------------------------
0.0 32.00 | 32.0 0.00
2.5 36.50 | 37.0 2.78
5.0 41.00 | 42.0 5.56
7.5 45.50 | 47.0 8.33
10.0 50.00 | 52.0 11.11
12.5 54.50 | 57.0 13.89
15.0 59.00 | 62.0 16.67
17.5 63.50 | 67.0 19.44
20.0 68.00 | 72.0 22.22
22.5 72.50 | 77.0 25.00
25.0 77.00 | 82.0 27.78
27.5 81.50 | 87.0 30.56
30.0 86.00 | 92.0 33.33
32.5 90.50 | 97.0 36.11
35.0 95.00 | 102.0 38.89
37.5 99.50 | 107.0 41.67
40.0 104.00 | 112.0 44.44
42.5 108.50 | 117.0 47.22
45.0 113.00 | 122.0 50.00
47.5 117.50 | 127.0 52.78
50.0 122.00 | 132.0 55.56
Explanation / Answer
import java.util.Scanner;
public class Temp_Convert
{
public static void main(String[] args)
{
print(" === Converting Temperature === ");
convertTemperature();
}
private static void convertTemperature()
{
Scanner input = new Scanner(System.in);
print(" Enter 1 for Fahrenheit to Celsius"+ " Enter 2 for Celsius to Fahrenheit"+ " Something else to Exit." + " Your Option:");
int selection = input.nextInt();
if (selection == 1)
{
print("Enter a degree in Fahrenheit:");
far2cel();
}
else if (selection == 2)
{
print("Enter a degree in Celsius:");
cel2far();
}
else
{
print("Bye..");
}
}
private static void cel2far()
{
Scanner input = new Scanner(System.in);
Double celsius = input.nextDouble();
print(celsius + " celsius is " + ((celsius * 9 / 5.0) + 32)+ " Fahrenheit");
convertTemperature();
}
private static void far2cel()
{
Scanner input = new Scanner(System.in);
Double Fahrenheit = input.nextDouble();
print(Fahrenheit + " Fahrenheit is " + ((Fahrenheit - 32) * (5 / 9.0))+ " celsius");
convertTemperature();
}
private static void print(String string)
{
System.out.print(" " + string);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.