
\documentclass[11pt]{article}
\setlength{\textwidth}{5.5in}
%\setlength{\evensidemargin}{-0.5in}
\usepackage{TRmydraft} % title page report
\usepackage{wmff} % algorithm
\usepackage{yprob} % algorithm listing, code, report exercises

\usepackage{graphicx}

\pagestyle{myheadings}
\markright{Object Orientation Programs\hfill}

\begin{document}

\reptitle{\bf Object Orientation Programs}
\author{Dr. Jos\'e M. Garrido}

%\date{February, 2009}

\subtitle{{}}
\subsubtitle{}


\authoraffil{Department of Computer Science} 
 
%
%\halftitlepage
%

\titlepage

\mdraft
%\maketitle
%\newpage
%\tableofcontents
%\newpage

\section{Programs}

An object-oriented program is an implementation of  the classes that were modeled in the  analysis and design phase of the software development process. This is the static view\index{static view} of a program that describes the structure of the program composed of one or more modules\index{modules} known as {\em classes}.

When the program\index{program} executes, objects\index{object} of these classes are created and made to interact among them. This is the dynamic view\index{dynamic view} of a program, which describes the behavior\index{program behavior} of the program while it executes. This behavior consists of the set of {\em objects\/}, each one exhibiting individual behavior and interacting\index{object interaction} with other objects.

\section{Definition of Classes}

A class defines the structure and the behavior of the objects in that class. The software definition of a class consists of:

\begin{itemize}

\item
The attribute declarations of the class

\item
Descriptions of the operations, known as methods or functions of the class

\end{itemize}

\begin{figure}[h]
%\vspace{.2in}
\centereps{1.5in}{3.062in}{figs/classtruct.eps}
\caption{Structure of a typical class}
\label{fig:class_struct}
%\vskip\baselineskip
%\vspace{0.1in}
\end{figure}


The attributes in a class are defined as data\index{data} declarations declarations\index{declarations} of the class. The behavior\index{object behavior} of the objects of the class is represented by the descriptions of the operations of the class. 

The general structure of a class is shown in Figure~\ref{fig:class_struct} that illustrates the declarations of the attributes and the definitions of three operations: {\em OperationX}, {\em OperationY}, and {\em OperationZ}. Each of these operations\index{operations} consists of local data declarations\index{data declarations} and its instructions.


The smallest modular unit is an operation, also known as a function or method; it carries out a single task. It is not standalone unit because every function belongs to a class; a function is an internal decomposition unit.

\section{Implementation of Programs}

The software implementation of a program is carried out writing the code in a suitable programming language. Detailed design is often written in pseudo-code, which is a high-level notation at the level between the modeling diagrams and the programming language. 

\subsection{Programming Languages}

Programming languages\index{languages} have well-defined syntax and semantic rules. The syntax is defined by a set of grammar rules and a vocabulary (a set of words). The legal sentences are constructed using sentences in the form of {\em statements}\index{statements}. There are two groups of words: {\em reserved words}\index{reserved words} and {\em identifiers}\index{identifiers}. 

Reserved words are the keywords of the language and have a predefined purpose in the language. These are used in most statements with precise meaning, for example, {\bf class}, {\bf inherits}, {\bf variables}, {\bf while}, {\bf if}, and others. 


Identifiers\index{identifiers} are names for variables, constants, functions, and classes that the programmer chooses, for example, {\em emp\_number}, {\em employee\_name}, {\em salary}, {\em emp\_date}, and on.


The main variable declarations in a class are the attributes\index{attributes}. The other data declarations are the ones inside a function and are known as {\em local\/} variable declarations\index{local declarations}. The structure of object-oriented programs includes the following kinds of statements\index{statements}:



\begin{itemize}
 \item
   Class definitions
 \item
   Declaration of data constants
 \item 
   Declaration of simple variables
 \item 
   Declaration of object references
 \item
   Definition of methods or functions
\end{itemize}

Within the body of function definitions, language instructions are included to
create objects and to manipulate the objects created. 

\subsection{Implementing Classes}

A class definition is implemented with a class header and additional statements; these are: 

\begin{enumerate}
\item
The {\bf description} statement, which may be used to include a textual documentation or description of the class. This section ends with a start-slash (*/).

\item
The {\bf class} statement is used for the class header. This includes assigning a name to the class and other information related to the class. 

\item 
The {\bf private} keyword starts the section for the declarations of the private attributes of the class or definitions of the private operations.

\item
The {\bf protected} keyword starts the section for the declaration of protected attributes of the class or definitions of the projected operations.
\item 
The {\bf public} keyword starts section for the declarations of the private attributes or the definition of the public operations of the class. 

\item
The {\bf endclass} statement ends the class definition.

\end{enumerate}

With OOSimL, the general syntactic definition of a class is:

\newpage

\vspace{0.1in}
%\begin{quote}
{\tt %\footnotesize
\begin{algorithm}
{\bf description}
\ \ \ . . .
{\bf class} $\langle${\em class\_name} $\rangle$ {\bf is}
\ {\bf private}
\ \ \ \ . . . [data declarations]
\ {\bf protected}
\ \ \ \ . . . [data declarations]
\ {\bf public} 
\ \ \ \ . . . [data declarations]
\ {\bf private} 
\ \ [definitions of private operations]
\ {\bf protected} 
\ \ [definitions of private operations]
\ {\bf public}
\ \ [definitions of public operations] 
{\bf endclass} $\langle${\em class\_name} $\rangle$ 
\end{algorithm}
%\end{quote}
}
\vspace{0.1in}

%\newpage


\section{Data Declaration}

Data descriptions are the data declarations that define the class attributes. Three groups of data attributes can be defined in a class: constants, variables, and object references. 

\vspace{0.1in}
%\begin{quote}
{\tt %\footnotesize
\begin{algorithm}
\ \ {\bf constants}
\ \ \ \ . . .
\ \ {\bf variables}
\ \ \ \ . . .
\ \ {\bf object references}
\ \ \ \ . . .
\end{algorithm}
%\end{quote}
}
\vspace{0.1in} 

Each data declaration consists of:

\begin{itemize}
\item
A unique name to identify the data item
\item
An optional initial value
\item
The type of the data item

\end{itemize}

The name of a data item is an identifier and is given by the programmer; it must be different from any keyword in OOSimL (or in Java). 



%\vfill

\subsection{Data Names}

Text symbols are used in all algorithm description and in the source program. The special symbols that indicate essential parts of an algorithm are called {\em keywords}. These are reserved words and cannot be used for any other purpose. 
The symbols used for uniquely identifying the data items and are called {\em identifiers} and are defined by the programmer. For example, {\em x, y, z, s}, and {\em area}. 

The data items normally change their values when they are manipulated by the instructions in the various operations. These data items are known as {\em variables}\index{variables} because their values change when instructions are applied on them. Those data items that do not change their values are called {\em constants}\index{constants}. These data items are given an initial value that will never change.


%\vfill

\subsection{Data Types}

Data types can be of two groups: {\em elementary\/}\index{primitive types} (also known as primitive) data types and classes.

Elementary types are classified into the three following categories: 
\begin{itemize}
\item Numeric
\item Text
\item Boolean
\end{itemize}

There are three groups of numeric types: {\bf integer}, {\bf float}, and {\bf double}. The non-integer types are also known as real (or fractional), which means that the numerical values have a fractional part. 

Integer values are countable to a finite value, for example, age, number of automobiles, number of pages in a book, and so on. Real values (of type {\bf float}) have a decimal point; for example, cost amount 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 {\bf double} provide more precision than type {\bf float}. 

There are of two basic types of text data items: {\bf character} and type {\bf string}. Data items of type {\bf string} consist of a sequence of characters. The values for these two types of data items are textual values.

%\vfill

Another data type is the one in which the values of the variables can take a truth-value (true or false); these variables are of type {\bf boolean}.

Complex types are the classes and are used as types for object reference variables in all object-oriented programs. Values of these variables are references to objects. 

\subsection{Data Declarations in OOSimL}

Each data description includes the name or identifier for variable or constant and its type. The initial value of a variable or constant is optional. There are two general categories of variables:

\begin{itemize}
  \item Variables of elementary or primitive type
  \item Object reference variables 
\end{itemize}

In object-oriented programming languages, the programmer defines classes as types for object reference variables, then creating objects of these classes. 


\subsubsection{Variables of Elementary Types}

The OOSimL statement for the declaration of variables of elementary types has the following form:

\vspace{0.1in}
%\begin{quote}
{\tt %\small
\begin{algorithm}
{\bf define} $\langle$ variable\_name $\rangle$ {\bf of type} $\langle${\em elementary\_type} $\rangle$
\end{algorithm}
}
%\end{quote}
\vspace{0.1in}

Examples of data declarations in OOSimL of two constants of types {\bf integer} and {\bf double}, and three variables of type {\bf integer}, {\bf float}, and {\bf boolean}.
 

\vspace{0.1in}
%\begin{quote}
{\tt \small
\begin{verbatim}
 constants
    define MAX_PERIOD = 24 of type integer
    define PI = 3.1416 of type double
 variables
    define number_elements of type integer
    define salary of type float
    define act_flag of type boolean
\end{verbatim}
}
%\end{quote}
\vspace{0.1in}

\subsubsection{Object References}

An object reference variable can refer to an object. The following OOSimL statement declares object references variables and has the structure:

\vspace{0.1in}
%\begin{quote}
{\tt %\small
\begin{algorithm}
{\bf define} $\langle${\em object\_ref\_name} $\rangle${\bf of class} $\langle${\em class\_name} $\rangle$
\end{algorithm}
%\end{quote}
}
\vspace{0.1in}

The following statements declare an object reference variable called {\em server\_obj} of class {\em Server}, and an object reference variable {\em car1\/} of class {\em Car}.

\vspace{0.1in}
%\begin{quote}
{\tt \small
\begin{verbatim}
object references
   define serverobj of class Server
   define car1 of class Car
\end{verbatim}
%\end{quote}
}
\vspace{0.1in}

\subsection{Temporal and Spatial Properties}
 
Additional properties of data items declared are important to fully understand identifying data items in software development, these concepts are:

\begin{itemize}
 \item
  The {\em scope\/}\index{scope} of a data item is that portion of a program in which statements can reference that data item. Variables and constants declared as attributes of the class can be accessed from anywhere in the class. Instructions in any functioned of the class can use these data items. Local declarations define data items that can only be used by instructions in the function in which they have been declared.
 \item
  The {\em persistence\/}\index{persistence} of a data item is the interval of time that the data item exists---the lifetime of the data item. The lifetime of a variable declared as an attribute of a class, exists during the complete life of an object of the class. Data items declared locally will normally have a life time only during which the function executes.
\end{itemize}

%\newpage


\section{Functions}

Classes consist of attribute declarations and definitions of functions\index{functions} (or methods\index{methods}). 

A function carries out a specific task of a class. Figure~\ref{fig:funstruct} illustrates the general structure of a function. The definition of a function consists of two basic parts: 

\begin{itemize}
\item
  Local data declarations
\item
  The sequence of statements with instructions that manipulate the data
\end{itemize}

\begin{figure}[h]
\vspace{.05in}
\centereps{1.6in}{2.75in}{figs/funstruct.eps}
\caption{Structure of a function}
\label{fig:funstruct}
%\vskip\baselineskip
\vspace{0.05in}
\end{figure}

The body of a function is composed of data declaration and instructions that manipulate the data. The scope of the data declared in the function is {\em local\/} to the function. The data\index{data} in a function only exists during execution of the function; their persistence is limited to the lifetime of the function. 

The brief documentation about the purpose of the function is described in the paragraph that starts with the keyword {\bf description} and ends with a star-slash (*/).  Other comments can be included at any point in the function to clarify and/or document the function. Line comments start with a double forward slash (//) and end with the line.

The name of the function is declared in the {\bf function\/} statement. This name is used when the function is called or invoked by some other function. In OOSimL, the basic form of a function definition is as follows.

\vspace{0.1in}
%\begin{quote}
{\tt %\small
\begin{algorithm}
\ {\bf description}
\ \ \ \ . . .
\ {\bf function} $\langle${\em function\_name} $\rangle$ {\bf is}
\ \ {\bf constants}
\ \ \ \ . . .
\ \ {\bf variables}
\ \ \ \ . . .
\ \ {\bf object references}
\ \ \ \ . . .
\ \ {\bf begin}
\ \ \ \ . . . [instructions]
\ {\bf endfun} $\langle${\em function\_name} $\rangle$ 
\end{algorithm}
}
\vspace{0.1in}

The keyword {\bf function} starts the header followed by the name of the function; the body of the function starts with the keyword {\bf is}. The function definition ends with the {\bf endfun} keyword and the name of the function. 

Similarly to the data declarations in the class, the data declarations\index{declarations} are divided into constant, variable, and object references. Constant, variable, and object declarations are optional. The instructions in the body of the function appear between the keywords {\bf begin} and {\bf endfun}. 

The special function, {\em main}, starts and terminates the execution of the entire program; it is the control function of the program.  One of the classes of the program must include this function.
 
 
\end{document}
