Chapter 2mccormic/1520/solutions/Chapter02.pdf · Chapter 2. Data Design and Implementation. Data...
Transcript of Chapter 2mccormic/1520/solutions/Chapter02.pdf · Chapter 2. Data Design and Implementation. Data...
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
2
Data Type
• Used to characterize and manipulate a certain variety of data.
• Formally defined by describing1. the collection of elements that it can
represent (the domain) and2. the operations that may be performed on
those elements.
3
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
5
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
6
Data Abstraction
The separation of a data type’s logical properties from its implementation.
Logical PropertiesPossible values
Available operations
ImplementationA collection of bits interpreted in a particular manner
7
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 Ada’s Integer type 8
Abstract Data Type
• A data type whose properties (domain and operations) are specified independently of any particular implementation.
9
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.
10
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.
11
Relationships Between Data Type, Data Structure, and
Abstract Data Type
An abstractdata type is adata type
An abstract data type encapsulates a data structure
A data structure is composed of three features
12
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.
13
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 representation of the structure to hold the data items, and the coding for operations.
What
How
14
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.
15
Scalar Types
• One research study on the nature of costlysoftware 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.
17
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;
18
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; -- Domain is 0 to 11type Byte is mod 256; -- Domain is 0 to 255
Time : Clock;Register : Byte;
19
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;
20
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.
21
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.
22
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.
23
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
Mantissa24
Floating Point Types (cont.)
• The following equivalent numbers illustrate the origin of the term floating point
.0512x109 .512x108 5.12x107 51.2x106
25
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.
26
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
27
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.
28
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).
29
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.
30
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.
31
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.
32
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.
33
Packages
• Packages are Ada’s main mechanism for– Information Hiding– Encapsulation
• Packages are written in two parts– Package Specification– Package Body
WhatHow
34
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).
35
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 37
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.
38
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.
39
Operation Terminology
• Parameter profile The distinguishing features of a subprogram—whether 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.
40
UML Class Diagram Illustrating Inheritance
A tank car is a specialized railroad car. The tank car class overrides the railroad car’s Construct_Car and Put operations and uses the other three railroad car operations. The tank car class has four additional operations.
41
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.
42
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.
43
UML Class HierarchyRooted 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.
44
Specification of Bingo Numbers
package Bingo_Numbers is-- This package defines Bingo numbers and their associated letters
- - The range of numbers on a Bingo Cardtype Bingo_Number is range 0..75;- - 0 can't be called, it is only for the Free Play squaresubtype Callable_Number is Bingo_Number range 1..75;
- - Associations between Bingo numbers and letterssubtype 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 letterstype Bingo_Letter is (B, I, N, G, O);
end Bingo_Numbers;
A Definition Package
49
Class Specifications
• Bingo Basket Singleton page 139• Bingo Card page 140• Bingo Caller page 141
53