Chapter 2 Data Design and Implementation Data The representation of information in a manner suitable for communication or analysis by humans or machines. Data are the nouns of the programming world:

The objects that are manipulated The information that is processed Data Type Used to characterize and manipulate a certain variety of data. Formally defined by describing

1. the collection of elements that it can represent (the domain) and 2. the operations that may be performed on those elements. Data Type Example Integer Atomic Data Types Scalar data type A data type in which the

values are ordered and each value is atomic Discrete (ordinal) data type A scalar data type in which each value (except the first) has a unique predecessor and each value (except the last) has a unique successor Composite Data Types A data type whose elements are composed of

multiple data items. For example, a calendar date is composed of a month value, a day value, and a year value UML Class Diagram showing composition Data Abstraction The separation of a data types logical properties from its implementation.

Logical Properties Implementation Possible values A collection of bits interpreted in a particular manner

Available operations Data Encapsulation The hiding of the representation of data from the applications that use the data at a logical level; a programming

language feature that enforces information hiding. Encapsulation of Adas Integer type Abstract Data Type A data type whose properties (domain and operations) are specified independently of any particular implementation.

Data Structure A collection of data elements whose logical organization reflects a structural relationship among the elements. A data structure is characterized by accessing operations that are used to store and retrieve the individual data elements.

Features of Data Structures They can be decomposed into their component elements. The organization of the elements is a feature of the structure that affects how each element is accessed. Both the arrangement of the elements and the way they are accessed can be encapsulated.

Relationships Between Data Type, Data Structure, and Abstract Data Type An abstract data type is a data type A data structure is composed of three

features An abstract data type encapsulates a data structure Classification of Data Structure Operations Constructor An operation used to create new values of a class

Observer An operation that returns an observation on the state of an object. Transformer (mutator) An operation that changes the state of one or more of the data values Iterator An operation that allows us to process all the components in a data structure in some sequence. Data From 3 Different Levels

Application (or user) level: modeling real-life data in a specific context. Logical (or ADT) level: abstract view of the domain and operations. Implementation level: specific What

representation of the structure to hold the data items, and the coding for operations. How Viewing A Library From 3 Different Levels Application (or user) level: Library of Congress, or Baltimore County Public Library. Logical (or ADT) level: domain is a collection of

books; operations include: check book out, check book in, pay fine, reserve a book. Implementation level: representation of the structure to hold the books, and the coding for operations. Communication between the Application Level and Implementation Level

Adas Built-In Types Scalar Types One research study on the nature of costly software faults indicates that poor models of scalar quantities were responsible for nearly 90% of the errors in the cases studied. My Hairiest Bug War Stories, M. Eisenstadt, Communications of the ACM, vol 40, no 4, 30-37, 1997.

Forum Letter, J. McCormick, Communications of the ACM, vol 40, no 8, 30,1997. Signed Integer Type Provides a range for modeling real-world whole signed numbers. type Car_Door_Type is range 2..6; Doors : Car_Door_Type; Modular (Unsigned) Integer

Type Provides a range for modeling real-world whole unsigned numbers. Uses modular arithmetic. Can use logical operators type Clock is mod 12; type Byte is mod 256; Time : Clock; Register : Byte;

-- Domain is 0 to 11 -- Domain is 0 to 255 Enumeration Type Domain is an ordered set of identifiers type Month_Type is (January, February, March, April, May, June, July, August, September, October,

November, December); Month : Month_Type; Real Types Not all real numbers can be stored exactly. Those real numbers that can be stored exactly are called Model Numbers. There are very few model numbers. Any real number that is not a model

number is stored as the closest model number. Errors in Storing Real Numbers The error in storing a real number may be expressed in two different ways. Absolute error The difference between the real number and the model number used to represent it.

Relative error The absolute error divided by the true value of the real number. Real Types in Ada Ada provides two types for storing real numbers. Floating point types Fixed point types Choose the type based on the kind of error

(absolute or relative) that is most relevant to your application. Floating Point Types A floating point type uses a fixed number of digits (mantissa) and a base raised to a power (exponent) to approximate a real number. The base is usually 2, but we illustrate with a base of 10. .71358 x 10+34

Exponent Base Mantissa Floating Point Types (cont.) The following equivalent numbers illustrate the origin of the term floating point .0512 x 109 .512 x 108 5.12 x 107 51.2 x 106 Declaration of Floating Point

Types Two examples type Inches is digits 4 range 0.00..100.00; type Feet is digits 6 range 0.00..1000.00; Digits specifies the minimum number of decimal digits in the numbers mantissa. Fixed Point Types A fixed point number is stored as a single

number with a fixed radix point. Ada provides two kinds of fixed point types Binary (ordinary) fixed point types Decimal fixed point types Binary (Ordinary) Fixed Point Type Declarations An ordinary fixed point number is stored as a single number with a fixed binary point. Two examples of binary fixed point types

type Meters is delta 0.001 range -1000.0..1000.0; type Degrees is delta 0.25 range 0.0..100.0; Delta specifies the maximum distance between model numbers. The Ada compiler may use a smaller delta. Decimal Fixed Point Type Declarations A decimal fixed point number is stored as a

single number with a fixed decimal point. Two examples of decimal fixed point types type Euro is delta 0.01 digits 8 range 0.0 .. 100_000.0; type Peso is delta 0.1 digits 9 range 0.0 ..10_000_000.0; Delta specifies the distance between model numbers. It must be a power of 10. Digits specifies the number of decimal digits in the number (to the left and right of the decimal point).

Floating Point Error The distance between model floating point numbers depends on the value of the exponent. The absolute error depends on the distance between model numbers. When the exponent is small, the distance between model numbers is small and when the exponent is large, the distance between model numbers is large.

While absolute error changes with exponent, the relative error for a floating point number does not. Fixed Point Error The distance between model floating point numbers is constant throughout the range. Therefore the absolute error is constant throughout the range.

While absolute error is constant throughout the range, the relative error for a fixed point number increases as the number gets smaller. Fixed or Floating Point? Choose floating point when relative error is more important than absolute error for the real numbers in your application. Choose fixed point when absolute error is more important than relative error for the

real numbers in your application. Composite Types Ada provides two kinds of composite types Array types (homogeneous components) Components are accessed by their position in the collection. Record types (heterogeneous components) Components are accessed by their name.

Tagged records provide the mechanism for inheritance in Ada. Packages Packages are Adas main mechanism for Information Hiding Encapsulation Packages are written in two parts

Package Specification Package Body How What Kinds of Packages (a simple taxonomy) Definition packages group together related

constants and types. Service packages group together the constants, types, subtypes, and subprograms necessary to provide some particular service. Data Abstraction Packages are used to construct abstract data types (ADTs). Private Types Are used to encapsulate the data of an

abstract data type Class A class is a specialized abstract data type (it has Inheritance) In Ada, a class is implemented with a

package (specification and body). In Ada, a class is declared in the package specification as a tagged, private type Inheritance The tagged (record) type is the basis of inheritance in Ada.

A tagged type may be extended with additional data fields. The primitive operations defined for a tagged type are inherited. Primitive operations may be overridden. Additional operations may be provided. Primitive Operations Operations for a type that are declared in the same package specification as the

type and has a parameter or a return value of the type. Only primitive operations for a class may be inherited by a subclass. Operation Terminology Parameter profile The distinguishing features of a subprogramwhether the subprogram is a procedure or function, the number of parameters, the type of each parameter, and , if

it is a function, the type of the result. Sometimes called the subprogram's signature. Overloading The repeated use of a subprogram name with different parameter profiles. Overriding The replacement of a superclass's operation with one defined for the subclass. UML Class Diagram Illustrating Inheritance

A tank car is a specialized railroad car. The tank car class overrides the railroad cars Construct_Car and Put operations and uses the other three railroad car operations. The tank car class has three additional operations.

Singleton Classes A class for which there is only one object. Also called an abstract data object (ADO). As with all classes in Ada, a singleton class is implemented with a package The package specification contains operations for the class but no tagged record that defines the class. The package body contains the declarations for the class and one variable holding the data

for the one object. Abstract Classes Instance An individual entity with its own identity. An object is an instance of a class. Abstract class A class that may have no direct instances. You cannot create an object of an abstract class. Concrete class A class that may have

instances. UML Class Hierarchy Rooted at an Abstract Class Locomotive is an abstract class. This distinction is indicated by the use of italics. The other five

subclasses are concrete classes. Bingo Numbers Column Number Range B 1 to 15

I 16 to 30 N 31 to 45 G

46 to 60 O 61 to 75 Specification of Bingo Numbers package Bingo_Numbers is -- This package defines Bingo numbers and their associated letters

- - The range of numbers on a Bingo Card type Bingo_Number is range 0..75; - - 0 can't be called, it is only for the Free Play square subtype Callable_Number is Bingo_Number range 1..75; - - Associations between Bingo numbers and letters subtype B_Range is Bingo_Number range 1..15; subtype I_Range is Bingo_Number range 16..30; subtype N_Range is Bingo_Number range 31..45; subtype G_Range is Bingo_Number range 46..60; subtype O_Range is Bingo_Number range 61..75;

- - The 5 Bingo letters type Bingo_Letter is (B, I, N, G, O); end Bingo_Numbers; Initial Class Diagram for Bingo Simulation Revised Class Diagram for Bingo Simulation

CRC Card for the Bingo Basket CRC Card for a Bingo Card CRC Card for a Bingo Caller