JAVA The formula for concerting a temperature from Fahrenheit to Celsius is C =
ID: 3697572 • Letter: J
Question
JAVA
The formula for concerting a temperature from Fahrenheit to Celsius is
C = 5/9(F - 32)
Where F is the Fahrenheit temperature and C is the Celsius temperature. Write a method named convert that accepts a Fahrenheit temperature as an argument. The method should return the temperature, converted to Celsius. Demonstrate the method by calling it in a loop that displays a table of the Fahrenheit temperatures 0 through 20 and their Celsius equivalent.
public class Temperature
{
public static void main (String[] args)
{
// write a loop to call the method convert to display a table of
// the Fahrenheit temperatures 0 through 20 and their Celsius equivalent
}
// Write the method convert starting here
public static
}
Explanation / Answer
Hello there ,
Please find below code and it's output.
Let me know if you have any queries .
Thanks.
/**
* Class that provide functionality to conver Fahrenheit to Celsius.
*
* @author dipal.prajapati
*
*/
public class Temperature {
static String format = "|%1$-20s|%2$-20s| ";
public static void main(String args[]) {
float celsius = 0;
System.out.format(format, "Fahrenheit", "Celsius");
System.out.println("------------------------------------------");
for (int i = 0; i <= 20; i++) {
celsius = convert(i);
System.out.format(format, i, celsius);
}
}
public static float convert(float fahrenheit) {
float celsius = (fahrenheit - 32) * 5 / 9;
return celsius;
}
}
=====O/P====
|Fahrenheit |Celsius |
------------------------------------------
|0 |-17.777779 |
|1 |-17.222221 |
|2 |-16.666666 |
|3 |-16.11111 |
|4 |-15.555555 |
|5 |-15.0 |
|6 |-14.444445 |
|7 |-13.888889 |
|8 |-13.333333 |
|9 |-12.777778 |
|10 |-12.222222 |
|11 |-11.666667 |
|12 |-11.111111 |
|13 |-10.555555 |
|14 |-10.0 |
|15 |-9.444445 |
|16 |-8.888889 |
|17 |-8.333333 |
|18 |-7.7777777 |
|19 |-7.2222223 |
|20 |-6.6666665 |
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.