Forums Register Login

Assigning value to Parameters

+Pie Number of slices to send: Send
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.

+Pie Number of slices to send: Send
Is point same as java.awt.Point?
If yes, then you should use point.x and point.y
+Pie Number of slices to send: Send
Not its not, basically all it is is:




Im not really sure how to use interfaces, so any help is appreciated.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
So should I create a other class?
+Pie Number of slices to send: Send
You should add methods I mentioned to the interface.

Please show us the code of the class implementing Point.
(1 cow)
+Pie Number of slices to send: Send
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.

+Pie Number of slices to send: Send
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:




(1 cow)
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Okay, so just.

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
I think that should look something like (I could be wrong):
and interface:

Let's see what more experienced guys think.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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.

Winston
(1 cow) 2
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
 

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,
Time flies like an arrow. Fruit flies like a banana. Steve flies like a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1048 times.
Similar Threads
constructor
Constructor Methods
Rotated Square from two co-ordinates
Java assignment help!
How to run my NetBeans project?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 20:19:11.