public class TemperatureTest { public static void main( String args[] ) { Scanne
ID: 3617312 • Letter: P
Question
public class TemperatureTest
{
public static void main( String args[] )
{
Scanner input = new Scanner(System.in );
int choice; // the user's choice inthe menu
// Use a do-while loop to display a menu with choices
// to convert from Fahrenheit to Celsius, Celsius toFahrenheit,
// or quit.
// Get the user's choice if it is 1 or 2, prompt for thetemperature.
// Use a switch statement to determine the action. Eithercall
// the conversion method and output the result, otherwisequit.
// The program should keep running until the user choosesquit from the menu.
} // end main
} // end class TemperatureTest
Explanation / Answer
import java.util.*; public class TemperatureTest { // to convert fromFahrenheit to Celsius, public static double toCelcius(doublefahrenheit) { return(fahrenheit-32)*(5/9); } // to convert fromCelsius to Fahrenheit, public static double toFahrenheit(doublecelcius) { returncelcius*9/5 + 32; } public static void main( String args[] ) { Scanner input = new Scanner(System.in ); double fahrenheit, celcius; int choice; // the user's choice inthe menu // Use a do-while loop to display amenu with choices // to convert from Fahrenheit to Celsius, Celsius toFahrenheit, // or quit. do{ System.out.println("1. convert from Fahrenheit to Celsius"); System.out.println("2. convert from Celsius to Fahrenheit"); System.out.println("3. Quite"); // Get the user's choice if it is 1 or 2, prompt for thetemperature. choice =input.nextInt(); // Use a switch statement to determine theaction. Either call // theconversion method and output the result, otherwise quit. // Theprogram should keep running until the user chooses quit from themenu. switch(choice) { case 1: System.out.println( "Please Enter Fahrenheit Value "); fahrenheit = input.nextDouble(); celcius = toCelcius(fahrenheit); System.out.println( celcius + " Celcius"); break; case 2: System.out.println( "Please Enter Celcius Value "); celcius = input.nextDouble(); fahrenheit = toFahrenheit(celcius); System.out.println( fahrenheit + " Fahrenheit"); break; default: break; } } while (choice != 3); } // end main } // end class TemperatureTest
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.