M.B. Feldman and E.B. Koffman, Ada 95 Problem Solving and Program Design, 3rd edition. Copyright 1999, Addison-Wesley Publishing Company. All Rights Reserved
Questions and comments to mfeldman@seas.gwu.edu
alternative steps | conditions |
Boolean expression | relational operators |
True | IF statement |
False |
decision step | pseudocode |
desk-check | trace |
extending a problem solution | solution by analogy |
multiple-alternative decision | nested IF statements |
decision table |
function specification | function body |
Also in this case study, discuss the advantages of using attributes like 'First and 'Last instead of "magic numbers" (explicit constant values) like Monday and Saturday. In this case study, we might want to "localize" the program by changing the names of the days to those of (for example) German. This could be done by changing exactly one statement, because we used attributes throughout the program. This is a good example of "designing for the future".
Not all students understand easily the difference between single- and two-alternative IF statements. Emphasize that in the latter, only one of the alternatives is executed and control then passes to the statement following the END IF.
Students with experience in other languages may be surprised that "not equal" is represented in Ada by the symbol /=. Those with Pascal experience are in for a rude awakening if they write <> instead, because <> is a valid symbol in Ada and the compiler will signal this with a "symbol out of context" message of some sort.
Perhaps someday language designers will reach agreement on a common symbol for "not equal."
You might point out as well that "greater than or equal" is written >=; the symbol => is usually called "arrow" in Ada circles, and is used for other purposes, such as associating the name of a parameter with its value. Writing => where >= is meant will lead to a "symbol out of context" message from the compiler.
Building on work that has already been done, instead of "re-inventing the wheel," is an admirable approach. You might wish to emphasize that, while adapting one's own work or the programs in the book is encouraged, the line must be drawn at copying programs written by classmates (unless you specifically authorize it)! The class environment imposes limits on reuse not usually present in industry! You might find it helpful to distribute or adapt my course guide on collaboration, which I adapted from one developed by my colleague Dianne Martin. If you have similar material you'd like to contribute to this website, please let me know.
The "dangling ELSE" problem so familiar to Pascal and C programmers does not occur in Ada because of this bracketing. Point out that there is no syntactic difference between a one-statement sequence in an IF, and a multi-statement sequence. BEGIN and END (or {} as in C) are not used in Ada to distinguish these.
Have your students practice translating decision tables into IF statements, making sure to get the order of conditions correct.
Students witth Fortran or Pascal backgrounds might be surprised that these basic math functions are in a package and not "intrinsic" to the language, but this is definitely Ada's style. Students with C experience will recognize this as a rough equivalent to math.h.
This is a good opportunity to open the discussion of local variables, which are created when the function is called and destroyed just before the function returns to its caller. Emphasize that a function must have a RETURN statement that returns a value of the proper type.
Our examples show function specifications and function bodies. The specification is usually optional; we include it in early examples to prepare the student for writing packages, in which a function specification appears in the package specification.
It is useful to demonstrate to students how a client of a package can be compiled if that package's specification exists; the package body need not exist. This emphasizes the "contract" idea--both the body and the client must be consistent with the spec.
I have found that students who know C or Turbo Pascal have expectations that are somewhat different from Ada's package structure. In Turbo Pascal, the interface (spec) and implementation (body) are placed in the same file; a C "header file" is usually copied in and compiled "on the fly". Also, students of C might think that an Ada file creates visibility, in the sense that everything "lower" in a file "sees" that which is defined "higher" in the file. This is not true in Ada, in which the source file is a rather unimportant concept. The important concept is the compilation unit, which for our purposes is a package spec, package body, or main program.