Ada-An Introduction to
the Art and Science of Programming

Walter J. Savitch and Charles G. Petersen

Preface

Copyright (c) 1992 by Benjamin/Cummings Pub. Co., Inc.

Introduction

This  book is designed for use in  introductory  programming 
courses  that use the Ada programming language.   It can be  used 
for  courses  as short as one quarter or as long as one  academic 
year.   The book includes a thorough introduction to  programming 
techniques  and  covers  most  aspects  of  the  Ada  programming 
language.   The material in the book assumes no previous 
knowledge  of  computers  and no mathematics beyond  high  school 
algebra.
     While this  book  is  based  on the very successful third  
edition  of  "Pascal  An Introduction  to the Art and Science of 
Programming" by Walter  J. Savitch. it was  not  written  by  
simply  replacing   all occurrences  of  Pascal code with Ada 
code.  It is different text books based on two distinctly 
different programming languages.  However, most of the same well 
tested examples and exercises are used and the general writing 
style and level are the same.
     One  of  the reasons that Pascal has enjoyed such a success 
in recent years is that  it  allowed  professors  to  teach  the  
things  they  felt necessary  but  without  all  the trappings of  
a  large  complex language.  Compared to Pascal, Ada is a large 
complex language.  However, by starting small and building it is 
possible to use the same methods used in teaching Pascal to teach 
programming with Ada.  
     There are a number of approaches that can be used to  teach 
Ada  to beginning students.   The approach used in this  textbook 
might  be  called "The bottom-up approach".   "Bottom-up" means 
that we start with the simple things like variable declarations, 
simple arithmetic expressions, simplified input/output and build 
upward toward subprograms and packages.

Features

Early Introduction of Subprograms  

It has been the authors' 
experience in teaching introductory programming  that  the early 
introduction of subprograms is  sound pedagogy.  This approach is 
now widely accepted.
     The  early introduction of subprograms and parameters  
allows all  exercise  programs to be written  using  completely  
modular subprograms.  Programming  without parameters invariably 
violates the  generally  accepted principles of software  
engineering  and forces  students  to  practice  techniques  that  
must  later  be unlearned.   Introducing parameters early 
eliminates the need  to teach programming techniques that will 
later be rejected.  Some  instructors want subprograms before 
control  structures and  other  insist on subprograms after 
control structures.  After much consultation, we have taken the 
approach that the simplest of control  structures, If-Then-Else   
and  Loop-Exit-When,   will  be  introduced  before subprograms 
because  it allows more substantial examples to motivate the  use 
of subprograms. 

Packages As Soon As Possible  

How early should packages be 
introduced?  Our philosophy about early subprograms carries over 
into packages.  In order to make the use of packages meaningful 
it is necessary to have something meaningful to put into them. 
This means that packages must be introduced after subprograms 
have been studied to allow for more substantial examples to 
motivate the use of packages.

Object-Oriented Design and Abstract Data Types  

Two chapters are 
dedicated to arrays, covering both  one-dimensional and multi-
dimensional  arrays. Heavy emphasis  on  programming  techniques 
such as reading  data  into arrays,  manipulating partially 
filled arrays,  and the design of data structures using arrays. 
Array coverage is followed by a  chapter with complete coverage  
of  records.   Object-oriented design  and the use of abstract 
data types are introduced along  with the material  on  arrays  
and records.  These topics are a natural part of good program 
design and Ada is a language that encourages the use of software 
engineering principles.

Generic Packages  

The next step is to introduce generics.  The 
concept of generic packages is introduced during the discussion 
of the package TEXT_IO.  The major thrust in the case studies in 
the later chapters is using generic packages in the 
implementation of our abstract data types. 

Designed for Interactive Programming  

All  the programs  are  
designed  for use in  an  interactive  environment because  it  
fits  in  nicely with the  modern  approach  to  the development 
of programs.  However, batch processing and related topics are 
not ignored.  The book contains sections  on techniques for batch 
processing and two chapters  on programs using both text and 
nontext files.  

Case Studies  

Every chapter has at least one case study that 
begins with a discussion of the problem, moves on to the 
development of pseudocode algorithms, then to the writing of 
actual code segments, and finally to a complete fully tested 
program.  The Ada code in all of the complete programs in the 
text have been copied directly from text files that have been 
compiled and run using several different Ada compilers running 
under several different operating systems for PC's to large 
mainframes.

Programming Style  

There is no official mandated standard Ada 
style for the writing of programs.  Various schemes have been 
proposed and some professors have developed their own style guide 
for student use.  After much debate, we have decided to use the 
style that is used in the Reference Manual for the Ada 
Programming Language (ANSI/MIL-STD-1815A).  It is a very 
simplistic style and that is probably best for beginning 
students.

TEXT_IO  

Many  professors  now  feel  that  there  are subjects  
programming  that should be taught but that are not available  in 
Pascal.  For example, run time error processing, information 
hiding, encapsulation, reuse, input and output for enumerations 
and generics are features that are available in Ada, which makes 
Ada a natural choice for updating a traditional Pascal course.  
The difficult part in getting started  in  Ada from the bottom-
up, is explaining the TEXT_IO package and  its enclosed generic 
packages.  Specific details of four approaches are provided as a 
trailing section to the preface.

1.  Use TEXT_IO as it is described in the language reference 
manual.  This requires the student of include instantiations of 
necessary generic package in their programs.

2.  Compile instantiations of INTEGER_IO and FLOAT_IO into a 
student library.  This is simpler an requires that the student 
with and use necessary packages.

3.  Compile package which is a subset of TEXT_IO into a student 
library.  This is the approach used in this book; we call our 
package simply I_O.  This is even simpler in that the student 
only has to with and use package I_O.

4.  Compile a procedure into a student library and make all 
student programs a separate unit of this procedure.  This is the 
simplest from the beginners point of view because the student 
only states that this is a separate compilation unit.
     Approach 2, 3, or 4 is only used until packages are 
introduced for then neither is required.

Self-Test and Summary Sections  

Self-Test  Exercises,  with 
answers in the back of the book, are  provided throughout each 
chapter.   Each chapter contains  a complete  Summary of Problem 
Solving and Programming  Techniques, as well as a complete 
Summary of Ada Constructs.  The Ada summary includes  templates  
and  typical examples  for  quick  and  easy reference.  

Flexibility  

The  order  in  which topics must be  covered  is  
extremely flexible.   The  more advanced topics -- such as 
recursion,  some software engineering topics, some numeric 
programming techniques, and  a substantial amount of data 
structures  including  records, files, and access types -- are 
packaged into chapters that can be covered  in almost any desired 
order.   Alternately,  a subset of the chapters can be chosen to 
form a shorter course.  The chapter on  text  files  is  divided 
into two  parts  to  allow  for  two possibilities:  either  
postponing the topic entirely until later in  a  course or 
briefly introducing them early and  giving  more detail  later 
on.   To add even more flexibility,  sections  with optional 
topics are included throughout the book.  The dependency chart, 
provided at the end of the preface, shows the possible orders in 
which  the chapters can be covered without losing continuity. 

Support Material  

A chapter-by-chapter instructor's guide is 
available directly from the publisher. All of the complete 
programs in this text are also available in machine-readable form 
on a program disk directly from the publisher. Thus, it is 
possible through the instructor to have all the programs 
available for students to run without having to type them in.
     Solutions to many of the programming assignments in the  
back of  each chapter will be made available in machine-readable  
form to  instructors  and  computer professionals  directly  from  
the authors for a nominal fee of $35.  Send your request on 
school or business letterhead along with $35 to:

     Charles G. Petersen
     Drawer CS
     Mississippi State, MS  39762

Please specify 3.5" or 5.25" disk.  Also, specify HD or DD 
format.

Acknowledgements

A special thanks goes to Alan Apt, the original editor on the 
project, whose idea it was to combine the talents of the authors 
to make this book possible.  Also, thanks to John Thompson who 
picked up the ball after Alan moved up the corporate ladder.
     As authors and professors, we are blessed with institutions 
(Mississippi State University and University of California, San 
Diego) that provide a conducive environment and ample facilities 
for this project. (Without email it would have taken much longer 
for authors at these two distant institutions to complete this 
project.)  Thanks also goes to Tuskegee University and Don Fuhr, 
the Computer Center Director for providing facilities and 
computer time so that programs could be tested on a VAX computer 
using VAX-ADA.  Eric W. Olsen at Meridian Software Systems helped 
us by providing a copy of their Open Ada compiling environment.
     Students in two different classes at two different 
institutions class tested much of the material in the book.  
Thanks goes to those students at Mississippi State University and 
California State University, Long Beach.
     The reviewers made numerous suggestions that helped improve 
the book.  Thanks to Susan Richman at Pennsylvania State 
University, Harrisburg for her intuitive insight; to Major John 
J. (Jay) Spegele at the United States Naval Academy for his 
practical approach to teaching; to Charles (Chuck) Engle at the 
Florida Institute of Technology who kept reminding us when our 
Pascal was showing; to Joel Carissimo at California State 
University, Long Beach for class testing material while working 
from a draft manuscript; to Bob Kitzberger of TeleSoft for his 
real-world prespective; to Frances Van Scoy at West Virginia 
Univeristy; and to Joseph Lang at the University of Dayton.

Specific Information of Different Input-Output Approaches


1.   The first of these approaches is to simply tell the student 
to put the following few lines at the beginning of every program.  

with TEXT_IO;  
procedure MAIN is
    package INTEGER_IO is new TEXT_IO.INTEGER_IO ( INTEGER );  
    package FLOAT_IO is new TEXT_IO.FLOAT_IO ( FLOAT );
    package BOOLEAN_IO is new TEXT_IO.ENUMERATION_IO ( BOOLEAN ); 
    use INTEGER_IO;
    use FLOAT_IO;
    use BOOLEAN_IO;

a)   You can try to explain what is going on but in the authors' 
opinion, this just complicates what may be an already complicated 
and confusing situation for beginning students.
b)   or you can simply tell them to just do it and that they will 
learn what is going on in due time.
     The remaining three approaches require that something be 
compiled into an Ada library to which the student has access.
     All   Ada  compilers  must  maintain  a  library  system  of 
previously  compiled programs.  The way that this is accomplished 
depends on the compiler.  Some compilers have a special program 
that must be run before any program units can be compiled in a 
directory.  For instance, an Ada library is created on the 
Meridian AdaVantage system by doing a Newlib.   This Newlib is 
used once and  only once in any given directory in which the user 
wishes to compile. On the PC's the Newlib is a BAT or command 
file, the code from any of the three schemes that follow can be 
compiled as part of the stream of commands in the command file.     
All of this helps students get started with as little encumbering 
as possible. When packages are introduced in the text, Text_IO is 
discussed and these approaches can be and should be abandoned.  
Programs beyond the chapter on packages use Text_IO as intended 
in the design of the language.

2.   The second approach requires that the following lines be 
compiled into the student library.

    with TEXT_IO;
    package INTEGER_IO is new TEXT_IO.INTEGER_IO ( INTEGER );  
    package BOOLEAN_IO is new TEXT_IO.ENUMERATION_IO ( BOOLEAN ); 

Once this is done, the student need add only the following two 
lines to the beginning of every program.

     with TEXT_IO, INTEGER_IO, FLOAT_IO, BOOLEAN_IO;
     use TEXT_IO, INTEGER_IO, FLOAT_IO, BOOLEAN_IO;

3.   The third approach is the one that we have chosen to present 
in this text.  It requires that the following package be compiled 
into the student library or a system library to which the student 
has access.  This package is included as part of the program disk 
that will accompany the instructor's guide.

with IO_EXCEPTIONS;
with TEXT_IO;
use TEXT_IO;
package I_O is

    package INTEGER_IO is new TEXT_IO.INTEGER_IO ( INTEGER );
    use INTEGER_IO;
    package FLOAT_IO is new TEXT_IO.FLOAT_IO ( FLOAT );
    use FLOAT_IO;
    package BOOLEAN_IO is new TEXT_IO.ENUMERATION_IO ( BOOLEAN );
    use BOOLEAN_IO;

    procedure GET ( ITEM : out INTEGER; WIDTH : in FIELD := 0 )
        renames INTEGER_IO.GET;
    procedure GET ( ITEM : out FLOAT; WIDTH : in FIELD := 0 )
        renames FLOAT_IO.GET;
    procedure GET ( ITEM : out BOOLEAN )
        renames BOOLEAN_IO.GET;
    procedure GET ( ITEM : out CHARACTER )
        renames TEXT_IO.GET;
    procedure GET ( ITEM : out STRING )
        renames TEXT_IO.GET;
    procedure GET_LINE ( ITEM : out STRING; LAST : out NATURAL )
        renames TEXT_IO.GET_LINE;
    procedure SKIP_LINE ( SPACING : in POSITIVE_COUNT := 1 )
        renames TEXT_IO.SKIP_LINE;
    procedure PUT ( ITEM  : in INTEGER;
                    WIDTH : in FIELD := INTEGER_IO.DEFAULT_WIDTH;
                    BASE  : in NUMBER_BASE := DEFAULT_BASE )
        renames INTEGER_IO.PUT;
    procedure PUT ( ITEM : in FLOAT;
                    FORE : in FIELD := DEFAULT_FORE;
                    AFT  : in FIELD := DEFAULT_AFT;
                    EXP  : in FIELD := DEFAULT_EXP )
        renames FLOAT_IO.PUT;
    procedure PUT ( ITEM  : in BOOLEAN;
                    WIDTH : in FIELD    := BOOLEAN_IO.DEFAULT_WIDTH;
                    SET   : in TYPE_SET := DEFAULT_SETTING )
        renames BOOLEAN_IO.PUT;
    procedure PUT ( ITEM : in CHARACTER )
        renames TEXT_IO.PUT;
    procedure PUT ( ITEM : in STRING )
        renames TEXT_IO.PUT;
    procedure PUT_LINE ( ITEM : STRING )
        renames TEXT_IO.PUT_LINE;
    procedure NEW_LINE ( SPACING : in POSITIVE_COUNT := 1 )
        renames TEXT_IO.NEW_LINE;

    function END_OF_LINE return BOOLEAN
        renames TEXT_IO.END_OF_LINE;
    function END_OF_FILE return BOOLEAN
        renames TEXT_IO.END_OF_FILE;

    procedure SET_COL ( TO : in POSITIVE_COUNT )
        renames TEXT_IO.SET_COL;
    procedure SET_LINE ( TO : in POSITIVE_COUNT )
        renames TEXT_IO.SET_LINE;

    function COL return POSITIVE_COUNT
        renames TEXT_IO.COL;
    function LINE return POSITIVE_COUNT
        renames TEXT_IO.LINE;

    DATA_ERROR : exception
        renames IO_EXCEPTIONS.DATA_ERROR;
    END_ERROR : exception
        renames IO_EXCEPTIONS.END_ERROR;

end I_O;

     Once this has been done, all programs begin with the 
following line:

     with I_O; use I_O;

4.   The fourth approach is the one that is favored by the 
authors' for a number of pedagogical reason.  Unfortunately, the 
feedback on this approach has not been overwhelming, but it is 
included as part of the program disk in the case that like minded 
instructors might want to use it.
     This approach requires that the following code be compiled 
into the student's library or into the system library.

with TEXT_IO;  
procedure RUN is
    package INTEGER_IO is new TEXT_IO.INTEGER_IO ( INTEGER );  
    package FLOAT_IO is new TEXT_IO.FLOAT_IO ( FLOAT );
    package BOOLEAN_IO is new TEXT_IO.ENUMERATION_IO ( BOOLEAN ); 
    use TEXT_IO;
    use INTEGER_IO;
    use FLOAT_IO;
    use BOOLEAN_IO;
    procedure MAIN is separate;
begin  
    MAIN;  
end RUN;

As part of the Newlib (or whatever it is called),  compile this 
program unit into the  newly established  library.   Once  it is 
done the user never sees  it and doesn't  even know it exists; 
the user just  begins  writing Ada programs.  The following are 
two examples:

Example 1  

separate ( RUN )
procedure MAIN is  
begin  
    PUT ( "Congratulations, Alan, " );  
    PUT ( "on your first Ada program." );
    NEW_LINE;  
end MAIN;  

Assume  the  above program is in file  'prog1.ada'.  To compile 
simply type:

     ada (or whatever the compiler is called ) prog1.ada 

then to prepare the program for execution, type:

     bamp (or whatever the linker is called ) run  

to link the program and then to execute the program type:

     run

Example 2  

separate ( RUN )
procedure MAIN is
    MONTHS,
    PAYMENT : INTEGER;
begin
    PUT ( "Congratulations, Alan, " );
    PUT ( "on your $10,000 win!" );
    NEW_LINE;
    PUT_LINE ( "Spread payments over how many month?" );
    GET ( MONTHS );
    PAYMENT := 10_000 / MONTHS;
    PUT ( "Your monthly payment will be $" );
    PUT ( PAYMENT, 1 );
    NEW_LINE;
end MAIN;  

Note  that  the only overhead necessary to make this work is  the 
one line 'separate ( RUN )' and the fact that the main  procedure 
must be called 'MAIN'. 
     A very  similar  thing  can be done  using  the  Janus  Ada 
compiler  by  compiling  the same Ada program  unit  given  above 
directly  into  the JanusAda directory;  the directory where  the 
compiler resides.   The VAX Ada compiler and the Verdix UNIX 
compiler require the creation of a  library  in a manner very 
similar to the  Meridian  AdaVantage system.  The point here is 
that these three methods are easy to accomplish regardless of 
what Ada system is used. 


wsavitch@ucsd.edu
December 21, 1995