• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 375
1
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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..
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 375
1
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this is the interface which i have created

 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 375
1
Python Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ohhhh yesssssss sir R jain sir
thanks a lot
its working fine sir.

great am happy.......
 
R. Jain
Ranch Hand
Posts: 375
1
Python Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 34
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 375
1
Python Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a ton sir.i did it yes........thanks.......

i will always follow your advice
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic