Question 3(7 points) write a Java program that asks the user to enter the curren
ID: 3910185 • Letter: Q
Question
Question 3(7 points) write a Java program that asks the user to enter the current temperature is in Fahrenheit, then the temperature from the keyboard. If the entered t program should calculate and display the Celsius equivalent to it. If the entered temperature is in Celsius, then your program should calculate and display the Fahrenheit equivalent to it. Your program should have at least two methods, method 1 is called convertToCelsius0, and the other method is called convertToFahrenheit). To convert from Fahrenheit to Celsius, you can use the following equation: celsius 5.0/9.0 *(fahrenheit-32) To convert from Celsius to Fahrenheit, you can use the following equation: Fahrenheit- 9.0/5.0*celsius +32Explanation / Answer
import java.util.Scanner;
class temperaureconverter {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter 'C' for celsius 'F' for Farenheit:");//Enter Capital C or F
char type = sc.next().charAt(0); //to take input as character
if(type=='C'){ //if type of temperature was celsius
System.out.println("Enter temperature:"); //to input temperature
double temp=sc.nextDouble();
converttofarenheit(temp); //method to convert temperature
}
else if(type=='F') //if type of temperature was fahrenheit
{
System.out.println("Enter temperature:");
double temp=sc.nextDouble();
converttocelsius(temp); //method to convert temperature
}
}
public static void converttofarenheit(double n){ //method is of void type as it doesnot return anything
double fahrenheit;
fahrenheit=(((1.8)*n)+32); //here 9/5=1.8 and formula was given
System.out.println("Temperature in fahrenheit="+fahrenheit);
}
public static void converttocelsius(double n){ //method is of void type as it doesnot return anything
double celsius;
celsius=(0.555*(n-32)); //here 5/9=0.555 and formula was given
System.out.println("temperature in celsius="+celsius);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.