BETA

Contributed by Chris Rathman

Shape class (shape.bet)

ORIGIN '~beta/basiclib/v1.6/betaenv';
--- lib: Attributes ---

Shape: (#
   x: @Integer;
   y: @Integer;

   (* accessors for x & y coordinates *)
   getX: (# exit x #);
   getY: (# exit y #);
   setX: (#
      newx: @Integer;
      enter newx
      do newx -> x;
   #);
   setY: (#
      newy: @Integer;
      enter newy
      do newy -> y;
   #);

   (* move the x & y coordinates *)
   moveTo: (#
      newx: @Integer;
      newy: @Integer;
      enter (newx, newy)
      do
         newx -> setX;
         newy -> setY;
   #);
   rMoveTo: (#
      deltax: @Integer;
      deltay: @Integer;
      enter (deltax, deltay)
      do ((getX + deltax), (getY + deltay)) -> moveTo;
   #);

   (* virtual routine - draw the shape *)
   draw:< (#
      do
         'Drawing a ' -> putText;
         inner;
         newline;
   #)
#);

Rectangle class (rectangle.bet)

ORIGIN '~beta/basiclib/v1.6/betaenv';
--- INCLUDE './shape'
--- lib: Attributes ---

(* function to create rectangle instance *)
NewRectangle: (#
   x: @Integer;
   y: @Integer;
   width: @Integer;
   height: @Integer;
   newInstance: @Rectangle;
   enter (x, y, width, height)
   do
      (x, y) -> newInstance.moveTo;
      width -> newInstance.setWidth;
      height -> newInstance.setHeight;
   exit newInstance[]
#);

Rectangle: Shape (#
   width: @Integer;
   height: @Integer;

   (* accessors for width & height *)
   getWidth: (# exit width #);
   getHeight: (# exit height #);
   setWidth: (#
      newwidth: @Integer;
      enter newwidth
      do newwidth -> width;
   #);
   setHeight: (#
      newheight: @Integer;
      enter newheight
      do newheight -> height;
   #);

   (* draw the rectangle *)
   draw::< (#
      do
         'Rectangle at:(' -> putText;
         getX -> putInt;
         ',' -> putText;
         getY -> putInt;
         '), width ' -> putText;
         getWidth -> putInt;
         ', height ' -> putText;
         getHeight -> putInt;
   #);
#);

Circle class (circle.bet)

ORIGIN '~beta/basiclib/v1.6/betaenv';
--- INCLUDE './shape'
--- lib: Attributes ---

(* function to create circle instance *)
NewCircle: (#
   x: @Integer;
   y: @Integer;
   radius: @Integer;
   newInstance: @Circle;
   enter (x, y, radius)
   do
      (x, y) -> newInstance.moveTo;
      radius -> newInstance.setRadius;
   exit newInstance[]
#);

Circle: Shape (#
   radius: @Integer;

   (* accessors for the radius *)
   getRadius: (# exit radius #);
   setRadius: (#
      newradius: @Integer
      enter newradius
      do newradius -> radius;
   #);

   (* draw the circle *)
   draw::< (#
      do
         'Circle at:(' -> putText;
         getX -> putInt;
         ',' -> putText;
         getY -> putInt;
         '), radius ' -> putText;
         getRadius -> putInt;
   #);
#);

Try shapes function (polymorph.bet)

ORIGIN '~beta/basiclib/v1.6/betaenv';
--- INCLUDE './rectangle'
--- INCLUDE './circle'
--- PROGRAM: descriptor ---

(#
   i: @Integer;
   scribble: [2] ^Shape;
   rect: ^Rectangle;
   do
      (* create some shape instances *)
      (10, 20, 5, 6) -> NewRectangle -> scribble[1][];
      (15, 25, 8) -> NewCircle -> scribble[2][];

      (* iterate through the list and handle shapes polymorphically *)
      (for i:scribble.range repeat
         scribble[i].draw;
         (100, 100) -> scribble[i].rMoveTo;
         scribble[i].draw;
      for);

      (* call a rectangle specific function *)
      (0, 0, 15, 15) -> NewRectangle -> rect[];
      30 -> rect.setWidth;
      rect.draw;
#)

Output

Drawing a Rectangle at:(10,20), width 5, height 6
Drawing a Rectangle at:(110,120), width 5, height 6
Drawing a Circle at:(15,25), radius 8
Drawing a Circle at:(115,125), radius 8
Drawing a Rectangle at:(0,0), width 30, height 15

Chris Rathman / Chris.Rathman@tx.rr.com