ok, so i am trying to make an interface for converting temperatures. right now, the compiler does not like the method names because it insists the methods don’t exist. I have no ideas. hopefully, this is the only problem. any takers?
public class mathTest
{
public static void main(String[] args)
{
mathTest myMathTest = new mathTest();
double x = 90;
double y = 0;
System.out.println("The temperature of 90 is in Fahrenheits.");
System.out.println("In Celsius, the temperature is: " + myMathTest.Cel());
System.out.println("The temperature of 0 is in Celsius.");
System.out.println("In Fahrenheits, the temperature is: " + myMathTest.Fahren());
}
}
public class math implements TemperatureInterface
{
public double Cel()
{
double cel = (x - 32) * 5/9;
return cel;
}
public double Fahren()
{
double fahren = (y * 9)/5 + 32;
return fahren;
}
}
public interface TemperatureInterface
{
public double Cel();
public double Fahren();
}
It looks like the methods in the class “math” are trying to use variables that are not declared in that class. You need to pass the variables to the methods as arguments.
Also, in the class “mathTest”, you should be calling math.cel, as the methods are defined in “math”, not methTest. And you need to declare and initialise “math” in the code for “mathTest”.
I don’t have access to a compiler right now so I can’t confirm that this is all that is wrong with it.
Would just changing the following to the below fix it up?
to:
i.e. you’re declaring myMathTest as an instance of the class you’re actually inside of, whereas you’d want to be declaring an instance of the other ‘math’ class.
Oh yeah, and just read Saffer’s post above - you also need to be passing those x and y values to the Cel and Fahren methods as arguments of type ‘double’
Firstly, your class “mathTest” never interacts with the class math. What you want in this case is the two to be the same, i.e:
public class MathTest implements TemperatureInterface {
...
}
You should not view the method main() as a part of MathTest, but as an entry point into your class structure. As the method is static, it exists on another plane of existence than the instance of MathTest you create with the keyword ‘new’
Secondly, your methods have to take the parameters x and y, i.e: