Lecture 13 – October 11, 2005
Here is a link to the adagide IDE if you are having trouble getting jGrasp to work.
http://www.martincarlisle.com/adagide.html
Here is a link to a list of free
http://www.adahome.com/Resources/Compilers/Free.html
I would download the one at
http://www.adahome.com/articles/1997-10/pr_objectadafree.html
Homework due Tuesday, October 18th :
Everything following – on a line is ignored by the compiler
The code below is an example of an
1 ÏÏÏwith ada.text_io;
2
3 ÏØÓìpackage usefulStuff is
4 Ïϧ
5 ÏϧÏïÏtype Mysuit is (clubs, diamonds, hearts,
spades);
6 ÏϧÏïÏtype Myrank is (two, three, four, five, six,
seven, eight, nine, ten, jack, queen, king, ace);
7 ÏϧÏïÏtype Player is (playerOne, playerTwo, tie);
8 Ïϧ
9 ÏϧÏïÏtype card is record
10 ÏϧÏÏÏÏÏÏÏÏÏsuit : Mysuit;
11 ÏϧÏÏÏÏÏÏÏÏÏrank: Myrank;
12 ÏϧÏÏÏÏÏÏend record;
13 Ïϧ
14 ÏϧÏïÏtype deck is array (1..52) of card;
15 Ïϧ
16 ÏÏ§ÏØÓìpackage suit_io is new ada.text_io.enumeration_io (Mysuit);
17 ÏÏ§ÏØÓìpackage
rank_io is new ada.text_io.enumeration_io (Myrank);
18 ÏÏ§ÏØÓìpackage
player_io is new ada.text_io.enumeration_io (Player);
19 ÏÏ©end usefulStuff;
20
The above package can be compiled although it has no executable code in it. It is also available with out the indentations an line numbers here.
You should download it and compile it.
Below you will find an example of an
1 generic -- specification
2
3 type
element is private;
4
5 package
queuesgeneric is
6
7 -- type definition
8 type queue (capacity: positive) is limited private;
9
10 -- exported
exceptions
11 QueueFull : exception;
12 QueueEmpty : exception;
13
14 -- constructors
15 procedure
MakeEmpty (Q : in out queue);
16 -- pre: Q is defined
17 -- post: Q is empty
18
19 procedure
Enqueue (Q: in out Queue; E : in element);
20 -- pre: Q and E are
defined
21 -- post: Q is returned
with E as the top element
22 -- raises: QueueFull if Q already contains Capacity Elements
23
24 procedure
dequeue (Q: in out Queue);
25 -- pre: Q is defined
26 -- post: Q is returned
with the top Element discarded
27 -- raises QueueEmpty if Q contains no elemelnts
28
29 -- selector
30 function
first (Q : in Queue) return Element;
31 -- pre: Q is defined
32 -- post: The first
element of Q is returned
33 -- raises: QueueEmpty if
Q contains no elements
34
35 -- inquiry operations
36 Function isEmpty (Q : in Queue) return Boolean;
37 -- pre: Q is defined
38 -- post: returns True if
Q is empty, False otherwise
39
40 Function
isFull (Q: in Queue) return boolean;
41 -- pre: Q is defined
42 -- post: returns True if
Q is full, False otherwise
43
44 Private
45 Type
List is Array (positive Range <> ) of element;
46 Type
Queue (Capacity : positive) is record
47 tail : natural := 0;
48 store : List (1..Capacity);
49 End record;
50 End QueuesGeneric;
51
1 package
body QueuesGeneric is
2
3 procedure makeEmpty (Q : in out queue ) is
4 begin
5 Q.tail := 0;
6 end makeEmpty;
7
8 procedure Enqueue (Q : in out Queue; E : in Element) is
9 begin
10 if
Q.tail = q.capacity then
11 raise
QueueFull;
12 else
13 Q.tail := q.tail + 1;
14 Q.store (Q.tail) := E;
15 end if;
16 end
Enqueue;
17
18 procedure
Dequeue (Q : in out Queue) is
19 begin
20 if
q.tail = 0 then
21 raise
QueueEmpty;
22 else
23 Q.store(1..Q.tail-1) := Q.store
(2..Q.tail); --
slice
24 Q.tail := Q.tail - 1;
25 end if;
26 end
Dequeue;
27
28 function
first (Q : in Queue) return Element is
29 begin
30 if
q.tail = 0 then
31 raise
QueueEmpty;
32 else
33 return
Q.Store(1);
34 end if;
35 end
first;
36
37 function
isEmpty (Q: in Queue) return boolean is
38 begin
39 return
Q.tail = 0;
40 end
IsEmpty;
41
42 function
isFull (Q : in Queue) return Boolean is
43 begin
44 return
Q.tail = Q.capacity;
45 end
isFull;
46
47 end
QueuesGeneric;
48
You
can download and
compile the body for queuesgeneric