*
The moose likes Beginning Java and the fly likes How To use varargs in java with methods in interface??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How To use varargs in java with methods in interface???" Watch "How To use varargs in java with methods in interface???" New topic
Author

How To use varargs in java with methods in interface???

harshal deshpande
Ranch Hand

Joined: Sep 06, 2012
Posts: 34
hello,

i am implementing simple program using interface in java.

the program i am doing is calculate area,perimeter of different shapes

its working fine till calculating area but when i want to calculate perimeter of triangle am unable to implement it as it takes three parameters(x,y,z) ;

but as i passed only two parameters (x,y) to calculate area i am unable to calculate perimeter.

i dont want to extend another interface to do this please guide me.

area of triangle=(x*y)/2;perimeter of triangle=(x+y+z).

how to get these values in java using varargs in interface methods .

i googled it but there is no single document on this kind of question

the only thing i found is syntax and i.e:
say public double perimeterVarArgs(double...x); thats it.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

What does your current interface look like?

It sounds like your method currently looks like it needs to take parameters which are particular to a shape. Different shapes are defined using different parameters - for example, for a triangle you need three points, while for a circle you'd need the center point and the radius.

The problem you're having now is an indication that there's a mistake in the design of the interface. Your interface should be designed so that it isn't dependent on how the shape is defined.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 370
    
    1

harshal deshpande wrote:
the only thing i found is syntax and i.e:
say public double perimeterVarArgs(double...x); thats it.

And that is what you all need..
Add it to your interface and provide an implementation in the class that implements that interface..
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4343
    
    8

There's a bit in the tutorial here: http://docs.oracle.com/javase/tutorial/java/javaOO/arguments.html

All you have to do, though, is use that syntax, and then access the parameter as an array. That is, in your example there, the type of x is double[].

However, what you're trying to do sounds very strange. Why would you want a single method signature that returns both a perimeter and an area? They're completely different concepts. And do note, the definition of x and y in those two formulas are not the same.
harshal deshpande
Ranch Hand

Joined: Sep 06, 2012
Posts: 34
thats where the problem lies sir

i dont know how to do it .

i tried in implemented class triangle like

double perimeterVarArgs(double x,double y,double z)
and return (x+y+z);

thats not working i dont even know whether its correct format or not
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4343
    
    8

When you say "that's not working", what do you mean? Because apart from the unhelpful name, that's a valid method.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4343
    
    8

Maybe we need to see more of your code. If you've actually got a triangle class, I'd have thought that a perimeter() method shouldn't take any arguments, because it would be calculated from the state of the object (instance variables). So maybe I'm misunderstanding what you're trying to do.
harshal deshpande
Ranch Hand

Joined: Sep 06, 2012
Posts: 34
thats not working means its giving an error as
"Exception in thread "main" java.lang.AbstractMethodError: com.mine.practise.Circle.perimeterVarargs([D)D
at simpragma.mine.practise.Main.main(Main.java:10)"
i am not getting this sir.i am reading the link you sent just to me

R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 370
    
    1

harshal deshpande wrote:thats not working means its giving an error as
Can you post your code here.. We can probably help you better after that..
harshal deshpande
Ranch Hand

Joined: Sep 06, 2012
Posts: 34
this is the interface which i have created

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Inside a varargs method, the argument that you pass looks like an array. So it could be something like:

However, as I already said, it's strange that you have a method in your interface that needs to take those parameters in the first place. Because what parameters you need depends on the shape, and it would be really strange if you had to pass all the parameters that define the shape into the method that calculates the perimeter. Instead, as Matthew suggests, the method should use the state of the object that you're calling the method on.

I imagine that you'd have an interface that looks like this:

Then you'd create classes for different shapes that implement the interface.

Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4343
    
    8

harshal deshpande wrote:"Exception in thread "main" java.lang.AbstractMethodError: com.mine.practise.Circle.perimeterVarargs([D)D
at simpragma.mine.practise.Main.main(Main.java:10)"

Then your problem isn't really with var-args. The problem is that you have an abstract method you have to implement, and you're trying to implement it with a different signature. But you can't do that - you have to implement the signature that is originally declared. If that signature isn't right, then you need to change the abstract method.

(That's not actually a compiler error - it looks like you're trying to run code that hasn't actually compiled, which is usually a mistake. Try compiling the code and see what message you get).
harshal deshpande
Ranch Hand

Joined: Sep 06, 2012
Posts: 34
what jasper sir said is absolutely right i think am making mistake and i am writting in same manner as he described.

here is a thing which i am doing


i dont know whats happening
R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 370
    
    1

And there is the error...
You are changing the method definition in your class... It is no more a var-arg method.. Rather a 3 arg method..

Values passed in your var-arg methods are actually passed as arrays..

so your method looks like:-

public void perimeterVararg(double... a).
And your three values are in array a.
harshal deshpande
Ranch Hand

Joined: Sep 06, 2012
Posts: 34
ohhhh yesssssss sir R jain sir
thanks a lot
its working fine sir.

great am happy.......
R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 370
    
    1

Good that you got the answer.. But you follow the approach that Jesper has described above...

Rather than passing all your parameters to your method, you should keep the attributes of the triangle as instance variables and initialize them through a two arg constructor...
And then your zero arg add and perimeter method can just use those variables to calculate and return what you want..
harshal deshpande
Ranch Hand

Joined: Sep 06, 2012
Posts: 34
hello,and thanks for the guidance you provided yesterday to me

i tried what R.Jain sir and jasper sir said to me but am not sure whether its correct or not please if you can check this and let me know about the corrections.

the program is working fine no pro with it.

but just check whether i followed the same approach what you guided to me.

following is the code which i tried.

the interface is:




and main method goes like this


this is the way you wanted me to do or i did something wrong again
R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 370
    
    1

harshal deshpande wrote:
this is the way you wanted me to do or i did something wrong again

Great.. This is exactly the way we wanted you to approach it...

*NOTE: - Your method names should start with a lower-case alphabet (Just to follow coding-conventions)
Else everything is fine
harshal deshpande
Ranch Hand

Joined: Sep 06, 2012
Posts: 34
Thanks a ton sir.i did it yes........thanks.......

i will always follow your advice
 
 
subject: How To use varargs in java with methods in interface???
 
Similar Threads
A Quadrilateral extend program
Help with Value Returning Methods
Salutations. =]
Circle program errors
Problem on interface