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
body | executable statements |
declaration | context clause (WITH) |
reserved word | identifier |
program | package |
input statement | output statement |
Integer I/O | Float I/O |
constant | variable |
comment | banner comment |
blank lines in programs |
case study |
assignment statement | arithmetic operator |
output formatting | prompting messages |
data type | predefined data type |
literal | subtype |
expression | token |
In discussing syntax and semantic errors, it is helpful to show students examples of compiler listings with mistakes such as forgetting the WITH statement or omitting a semicolon. Figure 3.7 is illustrative but specific to one compiler. It is instructive to show some listings from the compiler used for the class; the error messages are similar in content but different in form from one compiler to another.
Students with experience in Fortran, BASIC, or Pascal might be surprised that Ada's input/output commands are just library procedure calls. Should they ask why this is so, explain that many modern languages such as C and Modula-2 also use libraries, and that this is done to provide the flexibility to expand the I/O capabilities without changing the language syntax. You might illustrate the importance of the context clause (WITH) by showing a compiler listing for Program 3.1 with the WITH statement removed.
One useful feature is that all standard libraries are child packages of the package Ada. This is helpful in showing students how to distinguish between standard packages and others in the book, or indeed those you might provide. If the package name begins with Ada., it is a standard package; otherwise it is not.
This section also introduces some simple calculations using Float and Natural (nonnegative integer) quantities. It is important to emphasize that Ada does not allow float and integer quantities to be directly combined in an expression. Forced or explicit conversion between integers and floats is permitted, but taken up in detail in Chapter 8, and is best postponed until that time.
This is a good time to emphasize the idea of using subtypes such as Natural and Positive. A subtype variables with ranges appropriate to the calculation being done. As a first step, explain that Ada will raise an exception if a negative number is stored in a Positive or Natural variable, and that this is a good thing because it prevents meaningless calculations from being done. Point out to the students that they will be expected to define their own subtypes to specify the meaningful range of values of their variables.
I've found that subtypes come quitre naturally to students with no programming experience, and of course those with prior Pascal experience will have used subtypes (called subranges in Pascal) before. Students with C experience will not have seen range constraints subtypes before and might need help in understanding why such things are useful.
Explain that comments are to be used to explicate and document program statements, not to repeat them. Also explain that comments, blank lines, and spaces used for indentation are ignored by the compiler but are required out of concern for the reader of the program, and to make the program look aesthetically professional.
PROCEDURE Hello;
is valid, but not in this context, some compilers will accept it and proceed. This may give rise to multiple propagation errors because the parser treats nearly every successive statement as "out of context." You might illustrate this with a compiler listing from the class compiler. It is a good chance to discuss propagation errors and the fallibility of parsers, and advise the students not to panic when a simple program produces twenty diagnostics. They should correct the first error and try again; often the rest of the messages will disappear.
GNAT (the GNU Ada 95 compiler) is especially clever in diagnosing, and recovering from, errors of this kind. One of its heuristics uses the program statements' indentation to try to guess the programmer's intent. This is very unusual. If you are using GNAT, you might mention it to your students as yet another reason to indent their code consistently, right from the start. Students who write everything flush-left, like an old BASIC program (they always promise to clean it up before turning it in) are cheating themselves out of not just a better understanding of their own code, but also of GNAT's error recovery help!
The Software Development Method is a scaled-down version of the traditional industry development process and learning it is a student's first step toward professionalism. In my course, project submission and grading is broken down as follows:
At this early stage, of course, students cannot yet write programs that "stay alive" even if the input is poorly formed or out of range. They cannot learn everything at once. This does not prevent us from requiring that they predict, honestly, that a program will (for example) terminate on Constraint_Error if out of range input is entered, and that they provide a negative test value to demonstrate that their prediction was correct. The goal is to cause them to think about it, and to follow a "truth-in-advertising" principle. In my experience, fostering this attitude is well worth doing; students are genuinely proud of writing programs whose behavior they really understand.
The requirement for a full-blown Case Study often comes as a rude awakening to students with experience in other programming courses, especially in high school where the goal was just to pound out the code. To students who complain, I explain that my course is the foundation course for a professional career, and this is what professionals do in their daily work. Students who go on into summer or co-op employment in industry often tell me how much the Case Study approach really prepared them for their jobs, and how impressed their supervisors were that they actually learned about this in their first university course!
A student who thinks that the output is 4 has fallen into this trap. Students must learn that in procedural languages, statements are executed sequentially.B := 2; A := B + 1; B := 3; Ada.Integer_Text_IO.Put(Item => A);
Emphasize that reading a value into a memory location, or storing one with an assignment, destroys the previous value, but that examining the contents of a location leaves it unchanged.
Ada's I/O is cumbersome compared with Pascal's or C's, because each predefined Put displays only a single token and each Get reads only a single token. The advantage of it is that I/O procedures follow the same parameter-matching rules as do ordinary procedures. It does take getting used to, especially for students with experience in languages that have specialized I/O statements that allow multiple parameters. On the other hand, because the I/O packages are just packages providing ordinary procedures, knowledge about them is reusable in other package and procedure contexts.
It is often helpful for students to write a short program that displays the same value with many different combinations of formatting parameters, especially in the floating-point case. The language default is to display a Float value in E-notation; the exponent is suppressed by supplying Exp => 0 to the Put.
The section does not concentrate on reading strings. If you choose to emphasize this more, keep in mind that Ada requires that string lengths match exactly. In the case of input, this means that Get will attempt to read exactly the number of characters called for by the string length, and will ignore RETURN keypresses. A student may think the program is "hung" when it is merely waiting for more characters. A comprehensive and systematic discussion of strings and string I/O is provided in Chapter 10.
The introduction of one user-defined subtype, NonNegFloat, provides an opportunity for discussion about the appropriate ranges of variables, and for the fact that Ada lets programmers define their own ranges, a facility provided by Pascal but not by Fortran or C.
Too much is often made of operator precedence and association rules. We prefer to see these as defaults, used by the compiler when parentheses are omitted. Students should be encouraged to use parentheses liberally, at least until they have a thorough understanding of precedence. Precedence is taken up in detail in Chapter 8, but even there the emphasis is on using parentheses instead of trying to memorize a complicated table of precedences.