Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Implement the following integer methods: a)method Celsius return the Celsius equ

ID: 3626250 • Letter: I

Question

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;
}

}

Explanation / Answer

There are two main problem in this code, first: it does not have main method, second: when you divide 5/9 you need to cast to double, otherwise it returns 0, and 9/5 return 1. here is appropriate code. import java.util.Scanner; public class Convert { // convert temperatures public static void main(String args[]){ ConvertTemperature(); } public static 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 = ((double)(9 / 5)) * (celsius + 32); return fahrenheit; } public static double celsius(double fahrenheit) { double celsius; celsius = ((double)5 / 9) * (fahrenheit - 32); return celsius; } }