I want to make a simple program in Squeak that calculates the distance between a couple of points, but I would not like to use the class Point that is already defined. So far I have the following:
The getter and setter methods of x and y have been auto generated:
x ^ x
x: anObject x := anObject.
and the distance method I programmed is the following:
distance:aPoint
"comment stating purpose of message"
|dx dy |
dx:=aPoint x - x.
dy:=aPoint y - y.
^ (dx*dx+dy*dy) sqrt.
now I open a workspace and try to print out the results:
obj1:=PointS new.
obj1 x:3 y:4.
obj2:=PointS new.
obj2 x:5 y:6.
d:=obj1 distance obj2
UIManager default inform: 'Converted amount: ',d.
but the result I get is the following:
What am I missing?
Try replacing the following:
obj1 x:3 y:4.
With this:
obj1 x:3; y:4.
Basically, you are sending the message #x:y:
but you don't have a method with that name. Instead you have a method #x:
and another method #y:
, so you need to make two separate calls to the same object (which is done with the cascade operator (#;
).