# Java interfaces...

**URL:** <https://boards.straightdope.com/t/java-interfaces/612295>\
**Category:** Factual Questions\
**Created:** [February 9, 2012, 8:51am UTC](https://boards.straightdope.com/t/java-interfaces/612295 "2012-02-09T08:51:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![vanotd21](https://avatars.discourse-cdn.com/v4/letter/v/82dd89/32.png) [@vanotd21](https://boards.straightdope.com/u/vanotd21)\
**Post date:** [February 9, 2012, 8:51am UTC](https://boards.straightdope.com/t/java-interfaces/612295/1 "2012-02-09T08:51:19Z")

</div>

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?

```auto

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

```

```auto

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

```

```auto

public interface TemperatureInterface
{
	public double Cel();
	
	public double Fahren();

}

```

---

<div class="post-metadata">

**Author:** ![Saffer](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/saffer/32/6440_2.png) [@Saffer](https://boards.straightdope.com/u/Saffer)\
**Post date:** [February 9, 2012, 9:13am UTC](https://boards.straightdope.com/t/java-interfaces/612295/2 "2012-02-09T09:13:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Hallucinex](https://avatars.discourse-cdn.com/v4/letter/h/ba9def/32.png) [@Hallucinex](https://boards.straightdope.com/u/Hallucinex)\
**Post date:** [February 9, 2012, 1:23pm UTC](https://boards.straightdope.com/t/java-interfaces/612295/3 "2012-02-09T13:23:07Z")

</div>

Would just changing the following to the below fix it up?

> [@](#):
>
> mathTest myMathTest = new mathTest();

to:

> [@](#):
>
> math myMathTest = new math();

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’

---

<div class="post-metadata">

**Author:** ![kombatminipig](https://avatars.discourse-cdn.com/v4/letter/k/b5ac83/32.png) [@kombatminipig](https://boards.straightdope.com/u/kombatminipig)\
**Post date:** [February 9, 2012, 2:31pm UTC](https://boards.straightdope.com/t/java-interfaces/612295/4 "2012-02-09T14:31:29Z")

</div>

Hm, there’s a ton of trouble here.

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:

```auto

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:

```auto

public double convertToCelcius(double temperatureInFarenheit) {
    return (temperatureInFarenheit - 32) * 5/9;
}

```

Likewise, these parameters have to be declared in TemperatureInterface:

```auto

public interface TemperatureInterface {
    double convertToCelcius(double temperatureInFarenheit);	
    double convertToFarenheit(double temperatureInCelcius);
}

```

That should give you enough to go by to solve the rest yourself.
