Question: Modify the program and create two methods to convert from Celsius to Fahrenheit and vice versa. public static double CelsiusToFahrenheit (double Celsius) public static double
Modify the program and create two methods to convert from Celsius to Fahrenheit and vice versa. public static double CelsiusToFahrenheit (double Celsius) public static double FahrenheitToCelsius (double Fahrenheit) Formulae for the conversion: Fahrenheit = Celsius * 9 / 5 + 32 Celsius = (Fahrenheit 32) * 5 / 9 Hint 1: Please consider using a well known F to C and/or C to F calculator such as what you may find in a simple Google search to double-check your work. Hint 2: If you are an individual who likes to use parenthesis to more clearly show your calculations, be sure to take typecasting into account when dealing with your fractions. Refer to Chapter 2 for a refresher, if necessary.
import java.util.Scanner; public class TemperatureConvertion { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); char convertFrom = 'C'; double celsius=0,fahrenheit=0,celsius_result=0,temprature_result=0; do { System.out.println("Convert from degree Celsius, enter C; Convert from degree Fahrenheit, enter F: "); convertFrom = Character.toUpperCase(scnr.next().charAt(0)); } while (!((convertFrom == 'C') || (convertFrom == 'F'))); System.out.println("Enter degree in " + convertFrom + ": "); switch (convertFrom) { case 'C': System.out.println("Enter The Temperature in Celsius:"); celsius=scnr.nextInt(); temprature_result=CelsiusToFahrenheit(celsius); System.out.println("The Converted Temperature(in fahrenheit) is:"+temprature_result); break; case 'F': System.out.println("Enter The Temperature in fahrenheit:"); fahrenheit=scnr.nextInt(); celsius_result=FahrenheitToCelsius(fahrenheit); System.out.println("The Converted Temperature(in Celsius) is:"+celsius_result); break; default: System.out.println("Wrong Choice:"); } scnr.close(); } private static double FahrenheitToCelsius(double fahrenheit) { double celsius=0; celsius=(fahrenheit-32)*5/9; return celsius; } private static double CelsiusToFahrenheit(double celsius) { double fahrenheit=0; fahrenheit=celsius*9/5+32; return fahrenheit; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
