Chapter #9 Examples in Oz
%%%%%%%%%%%%%%%%%%% Chapter - 9 %%%%%%%%%%%%%%%%%%%%%%
% 9.1
class PointD
feat x y
meth init(X Y)
self.x = X
self.y = Y
end
meth closerTo0(P ?$)
{self distanceTo0($)} =< {P distanceTo0($)}
end
meth minus(P ?$)
{New CartesianPt init(self.x-P.x self.y-P.y)}
end
meth distanceTo0(?$) raise abstract() end end
end
class CartesianPt from PointD
meth init(X Y)
PointD,init(X Y)
end
meth distanceTo0(?$)
{FloatToInt {Sqrt {IntToFloat self.x*self.x + self.y*self.y}}}
end
end
class ManhattanPt from PointD
meth init(X Y)
PointD,init(X Y)
end
meth distanceTo0(?$)
self.x + self.y
end
end
% 9.2
class ShadowedManhattanPt from ManhattanPt
feat Dx Dy
meth init(X Y DX DY)
PointD,init(X Y)
self.Dx = DX
self.Dy = DY
end
meth distanceTo0(?$)
ManhattanPt,distanceTo0($) + self.Dx + self.Dy
end
end
% 9.5
{Browse 5#{New ShadowedManhattanPt init(2 3 1 0)}}
% 9.9
{Browse 9#{{New ShadowedManhattanPt init(2 3 1 0)} distanceTo0($)}}
% 9.17
class ShadowedCartesianPt from CartesianPt
feat Dx Dy
meth init(X Y DX DY)
PointD,init(X Y)
self.Dx = DX
self.Dy = DY
end
meth distanceTo0(?$)
% CartesianPt,distanceTo0($) + {FloatToInt {Sqrt {IntToFloat self.Dx*self.Dx + self.Dy*self.Dy}}}
{{New CartesianPt init(self.x+self.Dx self.y+self.Dy)} distanceTo0($)}
end
end
% 9.11
{Browse 11#{New ShadowedCartesianPt init(12 5 3 4)}}
% 9.12
{Browse 12#{{New ShadowedCartesianPt init(12 5 3 4)} distanceTo0($)}}
% 9.19
local P1 P2 in
P1 = {New CartesianPt init(3 4)}
P2 = {New ShadowedCartesianPt init(1 5 1 2)}
{Browse 19#{P1 closerTo0(P2 $)}}
end
% 9.25
class ShapeD
meth accept(Ask $) raise abstract() end end
end
class Circle from ShapeD
feat R
meth init(Ri) self.R = Ri end
meth accept(Ask $) {Ask forCircle(self.R $)} end
end
class Square from ShapeD
feat S
meth init(Si) self.S = Si end
meth accept(Ask $) {Ask forSquare(self.S $)} end
end
% 9.26
class ShapeVisitorI
meth forCircle(R $) raise abstract() end end
meth forSquare(S $) raise abstract() end end
meth forTranslation(Q S $) raise abstract() end end
end
% 9.27
class Translation from ShapeD
feat Q S
meth init(Qi Si)
self.Q = Qi
self.S = Si
end
meth accept(Ask $) {Ask forTranslation(self.Q self.S $)} end
end
% 9.28
{Browse 28#{New Circle init(10)}}
% 9.30
{Browse 30#{New Square init(10)}}
% 9.39
{Browse 38#{New Translation init(
{New CartesianPt init(5 6)}
{New Circle init(10)})}}
% 9.49
class HasPtV from ShapeVisitorI
feat P
meth init(Pi) self.P = Pi end
meth newHasPt(P ?$) {New HasPtV init(P)} end
meth forCircle(R ?$)
{self.P distanceTo0($)} =< R
end
meth forSquare(S ?$)
if self.P.x =< S
then self.P.y =< S
else false
end
end
meth forTranslation(Q S ?$)
{S accept({self newHasPt({self.P minus(Q $)} $)} $)}
end
meth forUnion(A B $) raise test end end
end
% 9.50
{Browse 50#{{New Circle init(10)} accept({New HasPtV init({New CartesianPt init(10 10)})} $)}}
% 9.51
{Browse 51#{{New Square init(10)} accept({New HasPtV init({New CartesianPt init(10 10)})} $)}}
% 9.52
{Browse 52#{{New Translation init(
{New CartesianPt init(5 6)}
{New Circle init(10)})}
accept({New HasPtV init({New CartesianPt init(10 10)})} $)}}
% 9.53
{Browse 53#{{New Translation init(
{New CartesianPt init(5 4)}
{New Translation init(
{New CartesianPt init(5 6)}
{New Circle init(10)})})}
accept({New HasPtV init({New CartesianPt init(10 10)})} $)}}
% 9.67
class UnionVisitorI from ShapeVisitorI
meth forUnion(S T $) raise abstract() end end
end
% 9.62
class Union from ShapeD
feat S T
meth init(Si Ti)
self.S = Si
self.T = Ti
end
meth accept(Ask $) {Ask forUnion(self.S self.T $)} end
end
% 9.71
{Browse 71#{New Translation init(
{New CartesianPt init(12 2)}
{New Union init(
{New Square init(10)}
{New Translation init(
{New CartesianPt init(4 4)}
{New Circle init(5)})})})}}
% 9.74
class UnionHasPtV from HasPtV UnionVisitorI
meth init(P) HasPtV,init(P) end
meth newHasPt(P ?$) {New UnionHasPtV init(P)} end
meth forUnion(S T ?$)
if {S accept(self $)}
then true
else {T accept(self $)}
end
end
meth forCircle(R $) HasPtV,forCircle(R $) end
meth forSquare(S $) HasPtV,forSquare(S $) end
meth forTranslation(Q S $) HasPtV,forTranslation(Q S $) end
end
% 9.78 | 9.112
{Browse 78#{{New Translation init(
{New CartesianPt init(3 7)}
{New Union init(
{New Square init(10)}
{New Circle init(5)})})}
accept({New UnionHasPtV init({New CartesianPt init(13 17)})} $)}}
% 9.119
{Browse 119#{{New Union init(
{New Square init(10)}
{New Circle init(10)})}
accept({New UnionHasPtV init({New CartesianPt init(10 10)})} $)}}
|