61.8k views
1 vote
Implement the following integer methods:

a)method Celsius return 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 method from part (a) and (b) to write an application the enables the user either to enter a Fahrenheit temperature and display the Celsius equivalent or enter a Celsius temperature and display the Fahrenheit equivalent

This is the software program and the problem I am having is that I cannot get the program to give me the correct answer!


// Exercise 6.22: Convert.java
// Use a method to write an application to enable the user
// Either to enter a Fahrenheit and Celsius equivalent
// Or enter a Celsius temperature and display the Fahrenheit
// program use scanner class
import java.util.Scanner;

public class Convert
{
// convert temperatures
public void ConvertTemperature()
{
Scanner input = new Scanner( System.in );
double convert;
int selection;
double temp;
double converts;


do
{
// print and prompt for input from user
System.out.println("***********************************************");
System.out.println( "Main Menu" );
System.out.println( "Enter 1 for Fahrenheit to Celsius equivalent " );
System.out.println( "Enter 2 for Celsius to Fahrenheit equivalent" );
System.out.println( "3 to Exit\\ " );
System.out.print( "Selection: " );
selection = input.nextInt();

// converts celsius to fahrenheit
// converts fahrenheit to celsius

switch ( selection )
{
case 1:
System.out.println("Enter fahrenheit temperature: " );
temp = input.nextInt();
convert = celsius( temp );
System.out.printf("%f degrees F = %f degrees C\\",temp,convert );
break;

case 2:
System.out.println("Enter celsius temperature: " );
temp = input.nextInt();
converts = fahrenheit( temp );
System.out.printf("%f degrees C = %f degrees F\\", temp, converts );
break;

case 3:
break;

default:
System.out.println( "Invalid selection" );



} // end of switch
} while( selection != 3);
} // end of convertTemperatures
public static double fahrenheit(double celsius)
{
double fahrenheit;
fahrenheit = 9 / 5 * (celsius + 32);

return fahrenheit;
}

public static double celsius(double fahrenheit)
{
double celsius;
celsius = 5 / 9 * (fahrenheit - 32);

return celsius;
}

}

User Anegru
by
5.7k points

1 Answer

3 votes

Answer:

  1. import java.util.Scanner;
  2. public class Main {
  3. public static void main(String[] args) {
  4. Scanner input = new Scanner(System.in);
  5. System.out.print("Choose an option 1) Convert Fahrenheit to Celcius 2) Convert Celcius to Fahrenheit: ");
  6. int option = input.nextInt();
  7. if(option == 1){
  8. System.out.print("Please enter a temperature in Fahrenheit: ");
  9. double fahrenheit = input.nextDouble();
  10. System.out.println("The equavalent Celcius is: " + Celsius(fahrenheit));
  11. }
  12. else if(option == 2){
  13. System.out.print("Please enter a temperature in Celcius: ");
  14. double celcius = input.nextDouble();
  15. System.out.println("The equavalent Fahrenheit is: " + Fahrenheit(celcius));
  16. }
  17. else{
  18. System.out.println("Invalid option.");
  19. }
  20. }
  21. public static double Celsius(double fahrenheit){
  22. return 5.0/ 9.0 * (fahrenheit - 32);
  23. }
  24. public static double Fahrenheit(double celcius){
  25. return 9.0 / 5.0 * (celcius + 32);
  26. }
  27. }

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)

User Roman Shevchenko
by
4.8k points