here is the question:
(Temperature Conversions) Implement the following integer methods: (a) Method celsius returns 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 methods from parts (a) and (b) to write an application that enables the user either to enter a Fahrenheit temperature and display the Celsius equivalent or to enter a Celsius temperature and display the Fahrenheit equivalent.
this is the method code: package Assignment.Q047;
public class TemperatureConversions
{
public double celsius( double fahrenheit )
{
double celsius = 5.0 / 9.0 * (fahrenheit - 32);
return celsius;
}
public double fahrenheit( double celsius )
{
double fahrenheit = 9.0 / 4.0 * (celsius + 32);
return fahrenheit;
}
}
and here is the main function:
import java.util.Scanner;
public class TemperatureConversionsTest
{
public static void main(String args[])
{
Scanner input = new Scanner( System.in );
TemperatureConversions test = new TemperatureConversions();
int choice;
do
{
System.out.println( "******This is a temperature conversion method******" );
System.out.println( "Enter '1': Coversion from Celsius to Fahrenheit" );
System.out.println( "Enter '2': Coversion from Fahrenheit to Celsius" );
System.out.println( "Enter '3': Exit Program" );
System.out.print( "Choice: " );
choice = input.nextInt();
if ( choice != 3 )
{
System.out.print( "Enter temperature that need to be converted: " );
double temperature = input.nextDouble();
switch ( choice )
{
case 1:
double fahrenheit = test.fahrenheit(temperature);
System.out.printf( "%d Celsius is %d Fahrenheit\n" + temperature, fahrenheit);
break;
case 2:
double celsius = test.celsius(temperature);
System.out.printf( "%d Fahrenheit is %d Celsius\n" + temperature, celsius);
break;
}
}
} while ( choice != 3 );
}
}
I am using the Apache NEtbeans IDE to run the code. My code does not shows any error when it is typed . BUt once it is excuted and and entered the choice, it shows lot of errors.
below is the error:
******This is a temperature conversion method******
Enter '1': Coversion from Celsius to Fahrenheit
Enter '2': Coversion from Fahrenheit to Celsius
Enter '3': Exit Program
Choice: 2
Enter temperature that need to be converted: 98
Exception in thread "main" java.util.IllegalFormatConversionException: d != java.lang.Double
at java.base/java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:4426)
at java.base/java.util.Formatter$FormatSpecifier.printInteger(Formatter.java:2938)
at java.base/java.util.Formatter$FormatSpecifier.print(Formatter.java:2892)
at java.base/java.util.Formatter.format(Formatter.java:2673)
at java.base/java.io.PrintStream.format(PrintStream.java:1209)
at java.base/java.io.PrintStream.printf(PrintStream.java:1105)
at Assignment.Q047.TemperatureConversionsTest.main(TemperatureConversionsTest.java:36)
Please help solve this problem as i am new to Java programming.