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.
If yes, then you should use point.x and point.y
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.
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.
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.
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.
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 public.
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.
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.