6.2 Describing Data


6.2 Describing Data

An introduction to data description was presented in Chapter 4. For a given problem, data consists of one or more data items. The input data is the set of data items that is transformed in order to produce the desired results.

Note

For every computation, there is one or more associated data items (or entities) that are to be manipulated or transformed by the computations (computer operations). Data descriptions are necessary, because the algorithm manipulates the data and produces the results of the problem.

For every data item, its description is given by:

  • A unique name to identify the data item

  • A type

  • An optional initial value

The software developer defines the name of a data item and it must be different than the keywords used in the programming language statements.

6.2.1 Names of Data Items

Text symbols are used in all algorithm descriptions and in the source program. The special symbols that indicate key parts of an algorithm are called keywords. These are reserved words that cannot be used for any other purpose. The other symbols used in an algorithm are for identifying the data items and are called identifiers. As mentioned previously, the programmer defines the identifiers.

A unique name or label is assigned to every data item; this name is an identifier. The problem for calculating the area of a triangle uses five data items, x, y, z, s, and area.

6.2.2 Data Types

There are two broad groups of data types:

  • Elementary (or primitive) data types

  • Classes

Elementary types are classified into the following three categories:

  • Numeric

  • Text

  • Boolean

The numeric types are further divided into three types integer, real, and double. The second type is also called fractional, which means that the numerical values have a fractional part. Type real is also called float. Type double provides more precision than type real.

Text data items are of two basic types: character and type string. Data items of type string consist of a sequence of characters. The values for these data items are textual values.

A third type of variables is type boolean, in which the values of the variables can take a truth-value (true or false).

Classes are more complex types that appear in all object-oriented programs. Data entities declared with classes are called object variables or object references.

Aggregates are more advanced types that define data structures as collections of data items of any other type, for example, an array of 20 integer values.

The data items usually change their values when they are manipulated by the various operations. For example, assume that there are two data items named x and y; the following sequence of instructions in pseudo-code first gets the value of x, and then adds the value x to y:

         read value of x from input device         add x to y 

The data items named x and y are called variables, because their values may change when operations are applied on them. Those data items that do not change their values are called constants, and their names are usually denoted in uppercase, for example, MAX_PERIOD, PI, so on. These data items are given an initial value that will never change.

In every program that executes, all the data items used by the various operations are stored in memory, each data item occupying a different memory location. The names of these data items represent symbolic memory locations.

6.2.3 Data Declarations

The data descriptions written in a programming language are called data declarations. This includes the name of every variable or constant and its type. The initial values, if any, for the data items are also included in the data declaration.

There are two general categories of variables:

  • Elementary

  • Object references

Elementary (or simple) variables are those whose type is elementary (also called primitive). Variables x and y defined previously are examples of elementary variables.

Object-oriented programming is mainly about defining classes as types for object references, and then declaring and creating objects of these classes. The type of an object is a class.

Aggregate variables are declared as arrays of elementary types or declared as arrays of object reference variables of some class type.

The following are examples of data declarations in KJP of two constants of types integer and double, followed by three elementary variables of type integer, float, and boolean.

      constants         integer MAX_PERIOD = 24         double PI = 3.1416      variables         integer count         real salary         boolean active 

The following is an example of a data declaration of an object reference. Assume that there is a class definition called Employee with two object references, one called emp_obj and the other called new_emp; the declaration is:

     objects          object emp_obj of class Employee          object new_emp of class Employee 




Object-Oriented Programming(c) From Problem Solving to Java
Object-Oriented Programming (From Problem Solving to JAVA) (Charles River Media Programming)
ISBN: 1584502878
EAN: 2147483647
Year: 2005
Pages: 184

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net