Class Notes 9/21/06 courtesy of Shawn

 

Exceptions

l Define exception

l Raise exception

l Handle exception

l Some are pre-defined

  l   Constraint error

 

We looked at the code Daniel shared with us  for version of stack.adb and discussed it and modified it.

-- Assignment : Homework due Sept. 21st, 2006
-- Programmer : Daniel 
-- Course     : CS 240
-- Date       : Sept. 20th, 2006
-- Compiler   : gcc with gnat
-- Professor  : Elizabeth Adams
-- Filename   : stack.adb
-- Executable :
-- Version    : 0.2
 
-- Purpose: To implement the operations of the stack data type  as dicussed in class.
 
-- Input : none
 
-- Output
--      To Screen    : Messages in the event of an error pushing or
--                     popping from a full or empty stack, respectively.
 
-- Modifications:
--
--  Version 0.2 :   A typo fixed in the peek declaration!
 
with ada.text_io;
-- use ada.text_io;
 
 
package body stack is
 
-- pre-Condition : theStack exists  and theValue exists
-- post-Condition : theValue has been added to the stack
-- raises error if theStack is full
 
   procedure push ( theStack : in out stackType;
                    theValue : in Integer ) is
   begin
      if not (theStack.top = theStack.MAXDATA ) then
                 theStack.top := theStack.top + 1;
          theStack.aStack(theStack.top) := theValue;
      else
        ada.text_io.put_line ( "A number could not be added to the stack, because it is full.");
      end if;
   end push;
 
 
 
   procedure pop ( theStack : in out stackType;
                   theValue : out Integer ) is
   begin
      if ( theStack.top > 0  ) then
         theValue := theStack.aStack(theStack.top);
         theStack.top := theStack.top - 1;
      else
         ada.text_io.put_line 
               ("A number could not be popped off of the stack, because it is empty.");
      end if;
   end pop;
 
   procedure peek ( theStack : in stackType;
                    theValue : in out Integer ) is
   begin
      if ( theStack.top /= 0 ) then
        theValue := theStack.aStack(theStack.top);
      else
       ada.text_io.put_line ("A number could not be peeked at, because the stack is empty.");
      end if;
   end peek;
 
   procedure makeEmpty ( theStack : in out stackType ) is
   begin
      theStack.top := 0;
   end makeEmpty;
 
   function isFull ( theStack : in stackType ) return boolean is
 
      result : boolean;
   begin
      result := false;
 
      if ( theStack.top > theStack.MAXDATA ) then
        result := true;
      end if;
 
      return result;
   end isFull;
 
   function isEmpty ( theStack : in stackType ) return boolean is
 
      result : boolean;
   begin
      result := false;
 
      if ( theStack.top = 0 ) then
         result := true;
      end if;
 
      return result;
 
   end isEmpty;
 
 
end stack;

 

procedure push

l Preconditions  - were added in class

l The stack exists

l The value exists

l The stack is NOT full

l Postconditions were added in class

l The value has been added to the stack

l        There is an error if the stack was full

l          Removed the USE clause and added fully qualified references

l          Corrected the test for truth of a boolean variable

l          Discussed whether to increment the stack top before or after pushing a value on the stack

l          Dan had modified the specification by adding a constant MAX value to the stack size instead of using a literal           .  We added the comments to each procedure and function specification.

 

-- Assignment : Homework due Sept. 21st, 2006
-- Programmer : Daniel 
-- Course     : CS 240
-- Date       : Sept. 20th, 2006
-- Compiler   : gcc with gnat
-- Professor  : Elizabeth Adams
-- Filename   : stack.ads
-- Executable :
-- Version    : 0.2
 
-- Purpose: To specify the stack data type as discussed in class with its data types and operations
 
-- Input : none
 
-- Output : none
 
-- Modifications: The MAX constant determines the maximum size of a given
--                stack.
--
--  Version 0.2 : An off-by-one error fixed in the type declaration of
--                my array, and a typo fixed in the peek declaration.
package stack is
 
   MAX : constant Integer := 20;
 
   type myArray is array (1..MAX) of Integer;
 
   type stackType is record
      top         : Integer;   -- alternate choice is        top := 0 : Integer;   
      aStack      : myArray;
      MAXDATA     : Integer := MAX;  -- added to original specification 
   end record;
 
 
   procedure push ( theStack : in out stackType; -- procedure to put an item on the stack 
                    theValue : in Integer);      -- modifies stack
                                                
   procedure pop  ( theStack : in out stackType; -- procedure to pop an item off the stack
                    theValue : out Integer);          -- modifies stack by removing the top value
                                                
   procedure peek ( theStack : in stackType;          -- procedure to look at top of stack
                    theValue : in out Integer);  -- leaves stack unchanged
                                                
   procedure makeEmpty ( theStack : in out stackType ); -- creates an empty stack
                                                                                                                                     -- alters stack by setting top pointer
                                                                                                                                     
   function isFull  ( theStack : in stackType ) return boolean; -- tests the stack for fullness
                                                                                                                                                           -- doesn't alter stack
                                                                                                                                                           
   function isEmpty ( theStack : in stackType ) return boolean; -- tests the stack for emptiness
                                                                                                                                                           -- doesn't alter stack
 
end stack;

 

 

Designing the Test Program

            First, before writing any code DESIGN THE TEST.

 

            Things to test:

            1.         isEmpty

            2.         peek

            3.         push

            4.         pop

            5.         isFull

            6.         makeEmpty

 
Note:  lines in red show the declaration and instantiation of an enumerated type.  They are not part of this assignment.  They are just an illustration.
-- Assignment : inClass program to test stack package
-- Programmer : entire class
-- Course     : CS 240
-- Date       : Sept. 21th, 2006
-- Compiler   : gcc with gnat
-- Professor  : Elizabeth Adams
-- Filename   : stackTest.adb
-- Executable :
-- Version    : 0.1
 
-- Purpose: test all of the methods in the stack package to see if they
--                        work as desired
 
-- Input : integers to be pushed onto the stack
 
-- Output
--     messages telling what procedure/function is being tested (labels)
--     data values being pushed onto and popped from stack
--     data values at top of stack
--     status of stack
 
-- Modifications:
--
--  Version 0.1 :  
 
WITH ada.text_io;
WITH ada.integer_text_io;
WITH stack;
 
PROCEDURE stackTest is
 status: boolean;
 testStack : stack.stackType;
 
-- the following two lines do not belong in THIS program and should be removed
 type fruit is (apple, berry, cherry, banana);
 myfruit : fruit;
 
-- the following line is an instantiation for an enumerated type which allows us to read and
-- write values of that type
package boolean_io is new Ada.Text_io.Enumeration_io (boolean);
 
-- the following line desn't belong in THIS program : it should be removed
package fruit_io is new Ada.Text_io.Enumeration_io (fruit);
 
BEGIN
-- test the status of the stack to see if is empty
ada.text_io.put 
   ( " testing the status of the stack - expect it to be empty output should be TRUE ");
 
-- call function
status := stack.isEmpty (testStack);
 
boolean_io.put (status);
 
ada.text_io.new_line(1);
 
-- the following lines don’t belong in THIS program : they should be removed
myfruit := apple;
fruit_io.put (myfruit);
 
END stackTest;

 

Other Notes

l In general, you don't need to worry as much about adding headings to .ads files.

l Comments should be added to a package specification file to explain what each procedure does, i.e.

procedure push (theStack : in out stackType; -- procedure to put and item ...

l Use IF NOT when doing a boolean test. Never ever ever in any programming language compare a boolean variable to the actual boolean value.

l DO NOT change specifications when working on a project unless discussing it with someone.

·         Reserved words should be in uppercase.

·         Ada is not case sensitive.

·         A boolean is an enumerated type: {false true} (false is less than true.)

·         Instantiation for boolean type: package boolean_io is new Ada.ext_io.Enumeration_io (boolean);

·         You can either define in your record that the top of the  stackType has a starting value of 0, or you can require users of the stackType to call makeEmpty for each new stack which will do it.

 

boolean variable done

done : boolean;

 

if not (done) then    -- shows test of boolean variable

 -- do what you want to do here

end if;

 

if (done) then  -- use this if you dont remember how to use NOT

null;

else

  -- do what you want to do here

end if;

 

DeMorgan's Laws

If NOT(A and B) == If (NOT A or NOT B)

If NOT (A or B)  == if (NOT A  AND NOT B)

 

Homework:   re-write your code so it works for floating point numbers.   Bring printouts of integer version and floating point versions to class on Tuesday.