Data consists of one or more data items. 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). The input data is the set of data items that are transformed in order to produce the desired results.
Data descriptions are necessary together with algorithm descriptions. The algorithm is decomposed into the operations that manipulate the data and produce the results for the problem. For every data item, its description is given by:
The data item type
A unique name to identify the data item
An optional initial value
The name of a data item is an identifier and is given by the programmer; it must be different from any keyword in KJP (or in Java). The type defines:
The set of possible values that the data item may have
The set of possible operations that can be applied to the data item
Text symbols are used in all algorithm descriptions and in the source program. The special symbols that indicate essential parts of an algorithm are called keywords. These are reserved words and cannot be used for any other purpose. The other symbols used in an algorithm are the ones for identifying the data items and are called identifiers. The identifiers are defined by the programmer.
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 used five data items, x, y, z, s, and area.
The data items usually change their values when they are manipulated by the various operations. For example, the following sequence of instructions first gets the value of x then adds the value x to y:
read x // read value of x from keyboard add x to y
The data items named x and y are called variables because their values change when operations are applied on them. Those data items that do not change their values are called constants, for example, Max_period, PI, and so on. These data items are given an initial value that will never change.
When a program 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.
There are two broad groups of data types:
Elementary (or primitive) data types
Classes
Elementary types are classified into the three following categories:
Numeric
Text
Boolean
The numeric types are further divided into three types, integer, float, and double. The noninteger types are also known as fractional, which means that the numerical values have a fractional part.
Values of integer type are those that are countable to a finite value, for example, age, number of automobiles, number of pages in a book, and so on. Values of type float have a decimal point; for example, cost of an item, the height of a building, current temperature in a room, a time interval (period). These values cannot be expressed as integers. Values of type double provide more precision than type float, for example, the value of the total assets of a corporation.
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 two types of data items are textual values.
A third type of variables is the one in which the values of the variables can take a truth-value (true or false); these variables are of type boolean.
Classes are more complex types that appear as types of object variables in all object-oriented programs. Data entities declared (and created) with classes are called objects.
The data descriptions are the data declarations. Each data description includes the name of every variable or constant with 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 variables (references)
Note | Object-oriented programming is mainly about defining classes as types for object variables (references), and then declaring and creating objects of these classes. The type of an object reference is a class. |
In KJP, the declaration of variables of elementary types has the following basic syntactic structure:
〈 elementary type 〉 〈 variable-name 〉
The following are examples of data declarations of elementary variables of type integer, float, and boolean:
variables integer count real salary boolean active
As mentioned previously, an object reference is a variable that can refer to (point to) an object. The KJP statement for declaration of object references has the following structure:
object 〈 object_ref_name 〉 of class 〈 class_name 〉
For example, consider a program that includes two class definitions, Employee and Ball. The declarations of an object reference called emp_obj of class Employee, and an object reference ball1 of class Ball are:
objects object emp_obj of class Employee object ball1 of class Ball
When identifying data items in software development, there are two important concepts to consider:
The scope of a data item is that portion of a program in which statements can reference that data item
The persistence of a data item is the interval of time that the data item exists—the lifetime of the data item