(Temperature Conversions) Implement the following integer methods: a) Method cel
ID: 3803666 • Letter: #
Question
(Temperature Conversions) Implement the following integer methods: a) Method celsius returns the Celsius equivalent of a Fahrenheit temperature, using the calculation celsius = 5.0 / 9.0 * (fahrenheit - 32); b) Method fahrenheit returns the Fahrenheit equivalent of a Celsius temperature, using the calculation fahrenheit = 9.0 / 5.0 * celsius + 32; c) Use the methods from parts (a) and (b) to write an application that enables the user either to enter a Fahrenheit temperature and display the Celsius equivalent or to enter a Celsius temperature and display the Fahrenheit equivalent.
Complete this exercise to perform temperature conversions. Submit your .java and .class files as well as screenshots of output for testing with the following data: F = 100; calculate C C = 212 calculate F C = 32 calculate F
Explanation / Answer
TempConverter.java
import java.text.DecimalFormat;
import java.util.Scanner;
public class TempConverter {
public static void main(String[] args) {
DecimalFormat df = new DecimalFormat("0.00");
Scanner scan = new Scanner(System.in);
System.out.println("Enter the Fahrenheit temperature:");
double fahrenheit = scan.nextDouble();
double c = celsius(fahrenheit);
System.out.println("Temparature in celsius is "+df.format(c));
System.out.println("Enter the Celsius temperature:");
double celsius = scan.nextDouble();
double f = fahrenhei(celsius);
System.out.println("Temparature in fahrenheit is "+df.format(f));
}
public static double celsius (double fahrenheit){
double celsius = ( 5 /(double) 9 ) * ( fahrenheit - 32 );
return celsius;
}
public static double fahrenhei (double celsius){
return celsius*(9/5) + 32;
}
}
Output:
Enter the Fahrenheit temperature:
100
Temparature in celsius is 37.78
Enter the Celsius temperature:
212
Temparature in fahrenheit is 244.00
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.