home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / CPROG / DDJ1289.ZIP / MEYER.LST < prev    next >
File List  |  1989-10-30  |  5KB  |  202 lines

  1. _WRITING CORRECT SOFTWARE WITH EIFFEL_
  2. by Bertrand Meyer
  3.  
  4.  
  5. [LISTING ONE]
  6.  
  7. class CIRCLE export
  8.       center, radius, intersect1, intersect2,
  9.       on, inside, outside,
  10.       translate, scale, rotate ...
  11. feature
  12.       center: POINT;
  13.       radius: REAL;
  14.  
  15.       intersect1(other: CIRCLE): POINT is
  16.       -- One of the intersections
  17.       -- of current circle with other
  18.             require
  19.                   not other.Void;
  20.                   center.distance(other.center)
  21.                         <=radius + other.radius
  22.             do
  23.                   ... Computation of intersection ...
  24.             ensure
  25.                   on(Result);
  26.                   other.on(Result);
  27.             end; -- intersect1
  28.  
  29.       intersect2(other:CIRCLE): POINT is
  30.                   ...
  31.  
  32.       on(p:POINT) is
  33.                   -- IS p on circle?
  34.             require
  35.                   not p.Void
  36.             do ...
  37.             end; -- on
  38.  
  39.       inside(p:POINT) is
  40.                   -- Is p inside circle?
  41.             require
  42.                   not p.Void
  43.             do ...
  44.             end; -- inside
  45.  
  46.        outside (p:POINT) is
  47.                   -- Is p outside circle?
  48.             require
  49.                   not p.Void
  50.             do ...
  51.             end; -- outside
  52.  
  53.        Create (c:POINT; r:REAL) is
  54.                   --Create circle with center c
  55.                   --and radius r
  56.             require
  57.                   not c.Void;
  58.                   r>=0
  59.             do
  60.                   center:=c; radius :=r
  61.             end; -- Create
  62.  
  63.        ... Other features (translate, scale, ...) ...
  64.  
  65.       invariant
  66.             ... See below ...
  67.       end -- class CIRCLE
  68.  
  69.  
  70.  
  71. [LISTING TWO]
  72.  
  73. require
  74.        other_not_void:not other.Void;
  75.        circles_intersect:
  76.                center.distance (other.center)
  77.                       <=radius + other.radius
  78.  
  79. [LISTING THREE]
  80.  
  81. class interface CIRCLE
  82. exported features
  83.       center, radius, intersect1, intersect2,
  84.       on, inside, outside,
  85.       translate, scale, rotate ...
  86. feature specification
  87.       center: POINT;
  88.       radius: REAL;
  89.  
  90.       intersect1(other: CIRCLE): POINT
  91.                   -- One of the intersections
  92.                   -- of current circle with other
  93.  
  94.             require
  95.                   not other.Void;
  96.                   center.distance(other.center)
  97.                         <=radius + other.radius
  98.             ensure
  99.                   on(Result)
  100.                   other.on(Result)
  101.  
  102.       intersect(other:CIRCLE): POINT
  103.             ...
  104.  
  105.       on(p:POINT)
  106.                   -- Is p on circle?
  107.             require
  108.                   not p.Void
  109.       
  110.       inside(p:POINT)
  111.                   -- Is p inside circle?
  112.             require
  113.                   not p.Void
  114.             ensure
  115.                   Result=(center.distance(p)<radius)
  116.  
  117.       outside(p:POINT)
  118.                   ...
  119.  
  120.       ...Specification of other features
  121.             (translate, scale, ...) ...
  122.  
  123. invariant
  124.       ... See below ...
  125.  
  126. end -- class interface CIRCLE
  127.  
  128.  
  129.  
  130.  
  131. [LISTING FOUR]
  132.  
  133. r is
  134.       require
  135.             p
  136.       do
  137.             if not p then
  138.                   ... Deal with erroneous case ...
  139.             else
  140.                   ... Proceed with normal execution ...
  141.             end
  142.       end; -- p
  143.  
  144.  
  145.  
  146.  
  147. [LISTING FIVE]
  148.  
  149. class C [T] export
  150.             write, write_successful,...
  151.       feature
  152.  
  153.             write_successful: BOOLEAN;
  154.                   -- An attribute
  155.  
  156.             write (x: T) is
  157.             -- Write x, if possible;
  158.                   -- make at most five attempts.
  159.                   -- Record result in write_successful
  160.       local
  161.             attempts: INTEGER
  162.             external
  163.                   attempt_to_write (x:T)
  164.                         language "..."
  165.             do
  166.                   if attempts <5 then
  167.                         attempt_to_write (x);
  168.                         write_successful:=true
  169.                   else
  170.                         write_successful:=false
  171.                   end
  172.             rescue
  173.                         attempts:=attempts+1;
  174.                   retry
  175.       end -- write
  176.             ...
  177. end -- class C
  178.  
  179.  
  180.  
  181. [LISTING SIX]
  182.  
  183. write(x: T) is
  184.             -- Write x;
  185.                   -- make at most five attempts.
  186.       local
  187.             attempts:INTEGER
  188.             external
  189.                   ... As before ...
  190.  
  191.       do
  192.             attempts:=attempts+1;
  193.             attempt_to_write(x);
  194.       rescue
  195.             if attempts < 5 then
  196.                   retry
  197.             end
  198.       end -- write
  199.  
  200.  
  201.  
  202.