When I compile the code it tells me I have 2 errors, both variable may not of been initialized errors. Variables celsius and fahrenheit are the problems. I believe I have already initialized them in their respective methods.
import java.io.*;
class Converter
{
double celsius,fahrenheit,temperature,inFahrenheit,inCelsius;
double Celsius (double temperature)
{
celsius = (5.0 / 9.0) * (temperature - 32);
return celsius;
}
double Fahrenheit (double temperature)
{
fahrenheit = (9.0 / 5.0) * temperature + 32;
return fahrenheit;
}
}
public class ConverterTester
{
public static void main(String[] args)throws IOException
{
double temperature,fahrenheit,celsius;
InputStreamReader inStream = new InputStreamReader (System.in);
BufferedReader stdin = new BufferedReader (inStream);
String intemperature,inCelciusOrFahrenheit;
System.out.println("What is the temperature");
intemperature = stdin.readLine();
temperature = Double.parseDouble(intemperature);
System.out.println("What is the temperature you wish to convert to, Celsius or Fahrenheit");
inCelciusOrFahrenheit = stdin.readLine();
if (inCelciusOrFahrenheit.equals("Celsius"))
{
Converter Conversion1 = new Converter();
Conversion1.Celsius(celsius);
System.out.println("Your new temperature is " + celsius);
}
else if(inCelciusOrFahrenheit.equals("Fahrenheit"))
{
Converter Conversion2 = new Converter();
Conversion2.Fahrenheit(fahrenheit);
System.out.println("Your new temperature is " + fahrenheit);
}
else
{
System.out.println("Please enter a correct temperature");
System.exit(0);
}
}
}
The errors occur when I call the Celsius method and Fahrenheit method, I'm not sure if I'm allowed to use the variables when I call the methods. I however have not been able to find anything saying it is not allowed though.