Create a program that converts Celsius to Fahrenheit and Fahrenheit to Celsius a
ID: 3663835 • Letter: C
Question
Create a program 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.
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.*;
class Celcius{
public static void main(String[] args){
int celsius=0, fahrenheit=32;
System.out.format("%-15s%-15s%-15s%-15s ","Celsius", "Fahrenheit", "Fahrenheit","Celsius");
System.out.println("-----------------------------------------------------");
while(celsius<=100 || fahrenheit<=212 ){
if(celsius<=100)
System.out.format("%-15d%-15.2f%-15d%-15.2f ",celsius, celToFah (celsius), fahrenheit, fahToCel(fahrenheit));
else
System.out.format("%33d%17.2f ",fahrenheit, fahToCel(fahrenheit));
celsius++;
fahrenheit++;
}
}
public static double celToFah(int celsius){
double F;
F=32+(celsius*180.0/100.0);
return F;
}
public static double fahToCel(int fahrenheit){
double C;
C=(fahrenheit-32)*100.0/180.0;
return C;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.