Answer:
- import java.util.Scanner;
-
- public class Main {
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- System.out.print("Choose an option 1) Convert Fahrenheit to Celcius 2) Convert Celcius to Fahrenheit: ");
- int option = input.nextInt();
-
- if(option == 1){
- System.out.print("Please enter a temperature in Fahrenheit: ");
- double fahrenheit = input.nextDouble();
- System.out.println("The equavalent Celcius is: " + Celsius(fahrenheit));
- }
- else if(option == 2){
- System.out.print("Please enter a temperature in Celcius: ");
- double celcius = input.nextDouble();
- System.out.println("The equavalent Fahrenheit is: " + Fahrenheit(celcius));
- }
- else{
- System.out.println("Invalid option.");
- }
- }
-
- public static double Celsius(double fahrenheit){
- return 5.0/ 9.0 * (fahrenheit - 32);
- }
-
- public static double Fahrenheit(double celcius){
- return 9.0 / 5.0 * (celcius + 32);
- }
- }
Step-by-step explanation:
Firstly, import the Scanner class as we need to prompt user to choose an option to display the temperature value (Line 1).
Next we create a method Celcius that will take one input fahrenheit and then apply the formula to calculate and return the equivalent Celsius (Line 24 - 26).
Create another method Fahrenheit that will take one input celcius and then apply the formula to calculate and return the equivalent Fahrenheit (Line 28 - 30).
In the main program, use the scanner object to get user input the option to display temperature value (Line 5- 7).
Create an in-else-if block and check if the option is one, get an input Fahrenheit from user and call the Celsius method to display the equivalent temperature in Celsius (Line 9 -13).
If the option is two, get an input Celsius from user and call the Fahrenheit method to display the equivalent temperature in Fahrenheit (Line 14 -18).
If the input option is other than 1 or 2, display an error message (Line 19 - 21)