• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Assigning value to Parameters

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello!

I have a few questions regarding parameters. Basically I am trying to get the distance between a point and "other". I believe that "other" will be given a value when the driver file is ran. However, I can not compile my file, because the variable xCoordinate (and yCoordinate) can not be found [symbol can not be found]. I am assuming it is the other. that is making the variable unfindable. Any suggestions on how to fix this would be greatly appreciated. Thank you.

 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is point same as java.awt.Point?
If yes, then you should use point.x and point.y
 
Jackson Daniels
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not its not, basically all it is is:




Im not really sure how to use interfaces, so any help is appreciated.
 
Bartender
Posts: 2237
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The variable other is of type Point. Point is an interface.
That means you can only invoke methods defined in the interface on other (plus all methods defined in Object class).

You can try adding getX() and getY() methods to the interface.
 
Jackson Daniels
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So should I create a other class?
 
Paweł Baczyński
Bartender
Posts: 2237
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should add methods I mentioned to the interface.

Please show us the code of the class implementing Point.
 
Maneesh Godbole
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Think of interfaces as entities which define the capability (or offers a contract) of some functionality.

Consider a GraveDigger interface, who defines the capability (or offers a contract) to dig a grave.
You would write it as

As you can see, there is no mention of how the grave will be actually dug.

Now consider two businesses who offer to dig graves.
One is a bit short on capital, so they dig graves by shovels.
Another is an established business, so they use blackhoes.
You can represent them as


As you can see, the end result is a freshly dug grave. However, the way the grave was dug, was different.
To summarize, an interface promises you some functionality. An implementation of that functionality can differ. From the outside, the consumer of the functionality is not aware (nor rarely cares) about the implementation.

 
Jackson Daniels
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So should the implementation be done in the interface (say math equations) and then the outcome of those equations be returned in the class. For example:




 
Paweł Baczyński
Bartender
Posts: 2237
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, you should not declare variables in interfaces.

Declare methods in the interface. Name them (for example) getX() and getY().

You can't provide implementations in interfaces (except for default methods introduced in Java 8).

What is Dist? Is dist(ance?) a point? I'd say it is not.

It looks to me that you don't need a Point to be interface at all. Just make it a class.
 
Jackson Daniels
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, so just.

 
Sheriff
Posts: 9019
655
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jack,

Are you sure you're not doing it wrong way round? If it is an assignment, better give us some text from there (just to make sure you moving correct way).
I have a feeling that you should be writing concrete Point class and implementing Distance interface or very similar, but not other way round.
 
Liutauras Vilda
Sheriff
Posts: 9019
655
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that should look something like (I could be wrong):
and interface:

Let's see what more experienced guys think.
 
Maneesh Godbole
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
IMO the OP needs to share some more information on why he wants an interface. Based on the discussion till now I do not see any need.
Point represents co-ordinates. Concrete implementation is the correct way.
Distance calculations can either be done by
1) The point instance can calculate the distance between it and another point
2) Have a helper class which will do it.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Maneesh Godbole wrote:IMO the OP needs to share some more information on why he wants an interface. Based on the discussion till now I do not see any need.


I'm not sure I agree (about the second bit).

One advantage of making Point an interface is that it can then be implemented in more than one way (eg, mutable/immutable) or by a class that shares its characteristics, eg:

Jack Daniels wrote:So should the implementation be done in the interface...


No. Interfaces - at least before version 8 - are about defining the behaviour of a type - ie, what methods it provides.

But it should be complete. Presumably distance() isn't the only thing a Point can do, so you need to think about exactly what you do want it to do.

There's also absolutely nothing wrong with doing both - writing an interface and a class - and it has many advantages.

For example: does everyone who uses a Point object need to know what its 'x' and 'y' values are? Maybe in some cases, but certainly not in all.

So you could write something like:which provides "getter" methods for designers, but not for the coderanch.

BTW, the above is called a "skeleton implementation", and you'll find it a lot in the Java Collections Framework. Have a look at AbstractList (←click→) and AbstractMap.

Hope it's not too much information for you.

Winston
 
Bartender
Posts: 732
10
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You shouldn't call your interface "Point" since it will conflict with Java's Point class. And consider using Java's Point2D.Double class (instead of writing your own class and/or interface ), as it already has a distance() method, among others.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Fred Kleinschmidt wrote:You shouldn't call your interface "Point" since it will conflict with Java's Point class. And consider using Java's Point2D.Double class (instead of writing your own class and/or interface ), as it already has a distance() method, among others.


Not sure I agree with either of those points (no pun intended), but the fact that Java already has a Point class is definitely worth knowing; so have a cow.

My problem with Point2D.Double (and actually with most Java "Point" implementations) is that it's mutable, when there's really no reason for it to be.

But that's probably just my prejudice.

Winston
 
Fred Kleinschmidt
Bartender
Posts: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

My problem with Point2D.Double (and actually with most Java "Point" implementations) is that it's mutable


I agree here. When I want an immutable one, I create my own class with a private Point2D.Double variable, and pass everything along to that. For example,
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic