-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.sim
63 lines (55 loc) · 1.77 KB
/
example.sim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
! Snarfed from http://people.cs.uchicago.edu/~mark/51050/Simula.Code.Example.pdf
BEGIN
CLASS Shape;
BEGIN
procedure talkToMe;
BEGIN
OutText("But I am a Shape first and foremost");
OutImage
END of getDefault;
END;
Shape CLASS Rectangle(recName,width,height);! class with 3 parameters;
Text recName;
Real width, height;! Specification of parameters;
BEGIN
Real area, perimeter; ! these are our attributes;
PROCEDURE initialize; ! method definition;
BEGIN
area := width * height;
perimeter := 2*(width + height);
END of initialize;
PROCEDURE displayYourself; ! a Method;
BEGIN
OutText(" I am a rectangle: "); OutText(recName);
OutImage;
OutText(" width: "); OutFix(width,2,4);
OutText(" height: "); OutFix(height,2,4);
OutText(" area: "); OutFix(area,2,6);
OutText(" perimeter: "); OutFix(perimeter,2,6);
OutImage;
END of displayYourself;
initialize; ! body of Rectangle;
OutText("A Rectangle has been created and initialized.");
OutImage;
displayYourself;
END of Rectangle;
Rectangle CLASS Square;
BEGIN
height := width;!body of Square;
initialize;
OutText("A Square has been created and initialized");
OutImage;
END of Square;
!Variables declared in the prefixed block: ;
ref(Rectangle) R1;
ref(Square) S1;
!Block body - here the program starts: ;
OutText("Creating a new Rectangle"); OutImage;
r1 :- New Rectangle("Rectangle r1",5,4); OutText("Creating a new Square"); OutImage;
s1 :- New Square("Square s1",6,6);
OutText("Calling displayYourself on the new Square"); OutImage;
s1.displayYourself;
OutText("Calling talkToMe on the new Square"); OutImage;
s1.talkToMe;
OutImage;
END;