Instructor's Manual for

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


 

Chapter 8 Scalar Data Types; the CASE Stement

last revised March 1999.

Chapter Objectives

The student will

  1. be exposed informally to the internal representation of integer and float values
  2. learn the syntax of arithmetic expressions, including precedence and association rules
  3. learn how to use the random number package in the Ada math library
  4. learn the use of the Ada USE clause
  5. learn about the predefined types Character and Boolean,
  6. learn how to convert between integer and float values, and how to convert a string of numeric digits to an integer
  7. learn about type compatibility
  8. understand the workings of the spider package
  9. learn the CASE construct

New Terms

Section 8.1

binary string

mantissa

exponent

operator precedence

literal

operator associativity

scale factor

evaluation tree

Section 8.2

USE clause

approximation

representational error

cancellation error

arithmetic overflow

arithmetic underflow

Section 8.3

pseudorandom numbers

random number generator

Section 8.4

Boolean operator

logical complement

AND

short circuit evaluation

OR

program flag

NOT

debugging flag

Section 8.5

ASCII

printable character

control character

Succ attribute

Pred attribute

Pos attribute

Val attribute

collating sequence

Section 8.6

CASE

selector

choice

 

Notes and Suggestions

General Suggestions

This is a lengthy chapter with a lot of details. While most of it is straightforward, you should allow enough time to cover the concepts well.

Section 8.1

This section covers all the details of arithmetic expressions and evaluation trees. You will have to decide whether to make a major issue of operator precedence; we believe it is better to emphasize the use of parentheses for clarity. Students should, in any event, practice drawing evaluation trees.

The integer-to-string conversion in Program 8.2 will not be intuitively obvious to many students, because they are not yet understanding that a numeric string and an integer value are different representations. Try changing Base to some other number and tracing the program.

Emphasize that conversion among numeric types is allowed, but that using it to excess leads to confusing programs and an excess of type conversion is probably due to a poor program design.

Section 8.2

The standard transcendental functions will be well-understood by students with reasonable math background. It will, however, come as a surprise to many that a computer&emdash;supposedly the very model of precision&emdash;cannot represent real values exactly. Be sure they understand why this is so.

The USE statement is avoided for the most part by industry, because in a program that WITHs and USEs a number of packages, the USEs make it difficult for the reader to know from which package a given operation comes. The USE should be used advisedly and in moderation; we avoid it in general.

Teachers with experience in Ada 83 will probably know of the heated discussions about the Ada 83 Numeric_Error exception. This exception could not always be reliably raised, and so it is eliminated from Ada 95. If an overflow is detected, Constraint_Error is raised as in other out-of-bounds conditions.

Section 8.3

Briefly explain the difference between random and pseudorandom numbers. You might suggest that students run Program 8.6 a number of times, noting the sequence each time, then comment out the Reset call and repeat the runs to see the diffrence that Reset makes.

You might also mention that Drunken_Spider (Program 2.15) is a simple example of a random walk.

Section 8.4

Not all students take easily to Boolean expressions and variables. In particular, DeMorgan's laws are far from obvious to many students; this is one reason for the example that shows the truth of these laws by example.

Some teachers encourage the use of a global debugging flag. They point out that such things are often used by professionals, who deliver programs with debugging statements still embedded, because a program will almost always need to be debugged eventually.

Remind students that Boolean is an enumeration type and that they can easily instantiate Enumeration_IO to read and display truth values.

Discuss the prime-number algorithm. It is reasonably straightforward and easy to understand, but the less-mathematical of your students may have difficulty with it.

Section 8.5

Students with experience in other languages, especially C but also Pascal, often wish to know how to represent non-printable characters in Ada. The Pos and Val attributes are the way to do this, and correspond to Ord and Chr in Pascal. The attributes in Ada are more general because they are available for any integer or enumeration type, not just Character.

Not mentioned in this section are two more useful attribute functions: if X is Integer and S is a string of numeric digits, then Integer'Value(S) returns the corresponding integer, and Integer'Image(X) returns the corresponding string. These are equivalent to C's atoi and itoa functions, respectively.

Section 8.6

The CASE in Ada is safe and compiler-independent. Emphasize that all values of the selector expression must be covered by choices; an OTHERS clause can be used to cover values that would otherwise be missing. Emphasize that only an integer or enumeration type can be used for a case selector; students often try to use a Float variable for this. CASE is to IF as FOR is to WHILE: the first of each pair is very strictly controlled in Ada; the second is looser and more general.

Section 8.7

This section explores the implementation of the spider package. Most students can grasp the various operations here; they are individually quite simple. This is a good opportunity to discuss the idea of state variables--the spider is an abstract data object (ADO) whose state is embodied in the location, color, and direction variables at the beginning of Program 8.9.

Students often want to know when they will learn about "graphics;" you can explain to them that in this example, they have already learned some important principles of graphics. Spend some time discussing DrawSymbol, in which a simple coordinate transformation is used to get from the spider's 20-by-20 world to the terminal's 24-by-80 screen. More elaborate graphics packages for more sophisticated displays just use more complex transformations, but the idea is the same.

Some really advanced students might rise to the challenge of reimplementing the body of Spider for a more interesting display like a 16-color VGA display. Several Ada graphics packages are available for different color displays; these can be seen as just more advanced versions of Screen.