Anda di halaman 1dari 113

/* Here i is again 1 from the outer block */

>

Table 2.2 lists all flow control

statements-.
Table 2.2 Statements.

The for statement is the only statement which really differs from for statements
known from other languages. All other statements more or less only differ in their
syntax. What follows are two blocks which are totally equal in their functionality. One
uses the while loop while the other the for variant:
{

int ix, sum; -sum


= 0;
ix = 0;

/* initialization */

while (ix < 10} {

/* condition */'

sum = sum + 1; ix = ix + 1;

/*

step */ } } {
int ix, sum;
sum = 0;
for (ix = 0; ix < 10; ix = ix + 1) sum = sum + 1 ; } To understand this, you

have to know, that an assignment is an expression.

2.3 Expressions and Operators:


In C almost everything is an expression. For example, the assignment statement "="
returns the value of its righthand operand. As a "side effect" it also sets the value of
the lefthand operand. Thus,
ix = 12;
sets the value ofixto 12 (assuming that ix has an appropriate type). Now that the
assignment is also an expression, we can combine several of them; for example:
kx = jx = ix = 12;
What happens? The first assignment assigns kx the value of its righthand side. This is
the value of the assignment to jx. But this is the value of the assignment to ix. The
value of this latter is 12 which is returned to jx which is returned to kx. Thus we have
expressed
ix = 12;
/

jx = 12;
kx = 12;
in one line.
Truth in C is defined as follows. The value 0 (zero) stands for FALSE. Any other value
is TRUE. For example, the standard function strcmpO takes two strings as argument
and returns -Hf the first is lower than the second, 0 if they are equal and 1 if the first
is greater than the second one. To compare if two strings strl andstr2 are equal you
often see the following if construct:
if (!strcmp(strl, str2))

* A r>T-r^ T-I -\r< niiniir'rr /-\T- uniiHiiirTnATiAU

{
DCn XffTTftf D AT

../* strl equals str2 */


}
else {
/* strl does not equal str2 */
}
The exclamation mark indicates the boolean NOT. Thus the expression evaluates to
TRUE only if strcmpO returns 0.
Expressions are combined of both terms and operators. The first could be constansts,
variables or expressions. From the latter, C offers all operators known from other
languages. However, it offers some operators which could be viewed as abbreviations
to combinations of other operators. Table 2.3 lists available operators. The second
column shows their priority where smaller numbers indicate higher priority and same
numbers, same priority. The last column lists the order of evaluation.

Most of these operators are already known to you. However, some need some more
description. First of all notice that the binary boolean operators &, ~ and | are of lower
priority than the equality operators. = = andl=. Consequently, if you want to check for
bit patterns as in
if ((pattern & MASK) == MASK)

Withstry " .
is sometimes catted . . . : . . . .
you must enclose the binary operation into parenthesis v.
The increment operators ++ and can be explained by the following example. If
you have the following statement sequence
a = a + 1 *<
ine'oihes
b = a'
>thrDs

^. ",

you can use the preincrement operator


b= ++a; Similarly, if you have the following order of
statements:
b = a; it hour, minute, second;
a = a + i;
you can use the postincrement operator

-red prior "to


b = a++;
triable of this type.
Thus, the preincrement operator first increments its associated variable and then
returns the'new value, whereas the postincrement operator first "returns the value and
then increments its variable. The same rules apply to the pre- and postdecrement
oj^rator.
funclion calls, nested assignments and the increment/decrement operators cause side
effects when they are applied. This may introduce compiler dependencies as the
evaluation order in some situations is compiler dependent. Consider the following
example which demonstrates this:
a[i] = i++;
/* D
s giefc.
The question is, whether the old or new value of i is used as the subscript into the
array a depends on the order the compiler uses to evaluate the assignment.
The conditional operator ?:fs an abbreviation for a commonly used if statement. For
example to assign max the maximum of a and b we can use the following ifstatement:
if (a > b;);
max = a;

:,t vlu# to ? */

max = b;
These types of if statements can be shorter written as
max = (a > b) ? a : b;
The next unusual operator is the operator assignment. We are often using
assignments of the following form
exprl = (exprl) Op (expr2)
for example
i = i*(j+ 1);
In these assignments the lefthand value also appears on the right side. Using
informal speech we could express this as "set the value of i to the current value
of imultiplied by the sum of the value of j and 1". Using a more natural way, we
would rather say "Multiply i with the sum of the value of j and 1". C allows us to
abbreviate these types of assignments to
i *= j + 1;
We can do that with almost all binary operators. Note, that the above operator
assignment really implements the long form although "j + 1" is not in parenthesis. The
last unusal operator is the comma operator,. It is best explained by an example:

i _ o 3T#wi

j = (i += 1, i += 2, i + 3);1* *
This operator takes its arguments and evaluates them from left to right and returns
the value of the rightmost expression. Thus, in the above example, the operator first
evaluates "i += 1" which, as a side effect, increments the value ofi. Then the next
expression "i += 2" is evaluated which adds 2 to i leading to a value of 3. The third
expression is evaluated and its value returned as the operator's result. Thus, j is
assigned 6.
The comma operator introduces a particular pitfall when using ri-dimensional arrays
with n > 1. A frequent error is to use a comma separated list of indices to try to
access an element:
....
int matrixtlO][5];

// 2-dim matrix

..
.ihe only su*Md
taw* mm other language? M ottos sisleaisats n
-awt. What MMm% ar
lnti

i = matrix[l,2];

// WON'T WORK!!

i = matrix[l][2];

// OK

katen

*f2

What actually happens in the first case is, that the comma separated list is interpreted
as the comma operator. Consequently, the result is 2 which leads to an assignment of
the address to the third five elements of the matrix!

Some of you might wonder, what C does with values which are not used. For example
in the assignment statements we have seen before,
ix = 12;
jx = 12;
kx = 12;
we have three lines which each return 12. The answer is, that C ignores values which
are not used. This leads to some strange things. For example, you could write
something like this:
ix = 1;
471.1;
jx = 2;
But let's forget about these strange things. Let's come back to something more useful.
Let's talk about functions.
Functions:
As C is a procedural language it allows the definition of functions. Procedures are
"simulated" by functions returning "no value". This value is a special type calledvoid.
Functions are declared similar to variables, but they enclose their arguments in
parenthesis (even if there are no arguments, the parenthesis must be specified):
int sum(int to);
int bar();

/* Declaration of sum with one argument */


/* Declaration of bar with no arguments */

void foo(int ix, int jx);


/* Declaration of foo with two arguments */
To actually define a function, just add its body:
int sum(int to) {
int ix, ret;
ret = 0;
for (ix = 0; ix < to; ix = ix + 1)
ret = ret + ix;
return ret;

/* return function's value */

} /* sum */
C only allows you to pass function arguments by value. Consequently you cannot
change the value of one argument in the function, if you must pass an argument by
reference you must program it on your own. You therefore use pointers.

Pointers and Arrays:


One of the most common problems in programming in C (and sometimes C++) is the
understanding of pointers and arrays. In C (C++) both are highly related with some
small but essential differences. You declare a pointer by putting an asterisk between
the data type and the name of the variable or function:
char *strp;

/* strp is spoin.ter. to char' */

You access the content of a pointer by dereferencing it using again the asterisk:
*strp = 'a';

/* A single character */

As in other languages, you must provide some space for the value to which the
pointer points. A pointer to characters can be used to point to a sequence of
characters: the string. Strings in C are terminated by a special character NbL (0 or
as char '\0 ') Thus, you can have strings of any length. Strings are enclosed in double
quotes:
.
strp = "hello";
In this case, the compiler automatically adds the terminating NUL character. Now, strp
points to a sequence of 6 characters. The first character is vh', the second " e' and so
forth. We can access these characters by an index in strp:
strptO]

/* h */

strp[l]

./* e */

strp[2] ' / * . ! * /
strp[3]

/* 1 */

strp[4]

/* o *'/

strp[5]

/* \0 */

The first character also equals "*strp" which can be written as "*(strp + 0)". This leads
to something called pointer arithmetic and which is one of the powerful features of C.
Thus, we have the following equations:
*strp == *(strp + 0) == strp[0]
*(strp + 1) == strp[l]
*(strp + 2) == strp[2]
Note that these equations are true for any data type. The addition is not oriented to
bytes, it is oriented to the size of the corresponding pointer type!
The strp pointer can be set to other locations. Its destination may vary. In contrast to
that, arrays are fix pointers. They point to a predefined area of memory which is
specified in brackets:

char str[6];
You can view str to be a constant pointer pointing to an area of 6 characters. We are
not allowed to use it like this:
str = "hallo";

/* ERROR */

because this would mean, to change the pointer to,point to 'h'. We must copy the string
into the provided memory area. We therefore use a function called strcpyOwhich is
part of the standard C library.
strcpy(str, "hallo"); /* Ok */
Note however, that we can use str in any case where a pointer to a character is
expected, because it is a (fixed) pointer.
A First Program
Here we introduce the first program which is so often used: a program which prints
"Hello, world!" to your screen:
tinclude <stdio.h>
/* Global variables should be here */
/* Function definitions should be here */
int
main{) {
puts("Hello, world!");
return 0;
} /* main */
The first, line looks something strange. Its explanation requires some information
about how C (and C++) programs are handled by the compiler. The compilation is
roughly divided into two steps. The first step is called "preprocessing -'' and is used to
prepare raw C code, in this case this step takes the first line as an argumenl include a
file called stdic.h into the source. The angle brackets just indicate, that file is to be
searched in the standard search path configured for your compiler file itself provides
some declarations and definitions for standard input/output. example, it-declares a
function called put(). The preprocessing step also deletes comments.
In the second step the generated-raw C code is compiled to an executable. L
executable must define a function called main(). St is this function .which is callel
once the program is started. This function returns an integer which is returned a;
program's exit status.
Function main0 can take arguments which represent the command line parame! We
just introduce them here but do not explain them any further:

#include <stdio.h>
int
main(irit argc, char *argv[]) {
int ix;
for (ix = 0; ix < argc; ix++)
printf ("My %d." argument is %s\n", ix, argvfix]);
return 0;
/
} /* main */

The first argument argc just returns the number of arguments given on the command
line. The second argument argv is an array of strings. (Recall that strings are
represented by pointers to characters. Thus, argv is an array of pointers to characters.)
What Next?
This section is far from complete. We only want to give you an expression of what C
is. We also want to introduce some basic concepts which we will use in the following
section. Some concepts of C are improved in C++. For example, C++ introduces the
concept of references which allow something similar to call by reference in function
calls.
We suggest that you take your local compiler and start writing a few programs (if you
are not already familiar with C, of course). One problem for beginners often is that
existing" library functions are unknown. If you have a UNIX system try to use the
man command to get some descriptions. Especially you might want to try:
man gets
man printf
man puts
man scanf
man .strcpy

.We also suggest, that you get yourself a good book about C (or to find one of the online tutorials). We. try to explain 1 everything we introduce in the next sections.
However, there is nothing wrong with having some reference,at hand.
Basic Extensions:
The following sections present extensions to already introduced concepts of C.
C++ adds a new comment which is introduced by two slashes (If) and which lasts until
the end of line. You can use both comment styles, for example to comment out ;e blocks
of code:

/* C comment can include // and can span over


several lines. */
// /* This is the C++ style comment */ until end of line

In C you must define variables at the beginning of a block. C++ allows you to define
variables and objects at any position in a block. Thus, variables and objects should be
defined where they are used.
Data Types:
C++ introduces a new data type called reference. You can think of them as if they
were "aliases" to "real" variables or objects. As an alias cannot exist without its
corresponding real part, you cannot define single references. The ampersand (&) is
used to define a reference. For example:
int ix;

/* ix is "real" variable */

int &rx = ix;

/* rx is "alias" for ix */

ix = 1;

/* also rx == 1 */

rx = 2;

/* also ix == 2 */

References can be used as function arguments and return values. This allows to pass
parameters as reference or to return a "handle" to a calculated variable or object.
The table 2.4 is adopted from [1] and provides you with an overview of possible
declarations. It is not complete in that it shows not every possible combination and
some of them have not been introduced here, because we are not going to use them.
However, these are the ones which you will probably use very often.

In C and C+ + you can use the modifier const to declare particular aspects of a
variable (or object) to be constant. The next table 2.5 lists possible combinations and
describe their meaning. Subsequently, some examples are presented which
demonstrate the use of const.
Table 2.5 Constant declaration expressions.
fin i

_'

"

"

.........

" w'rmi II...........................

II

II

~i,,.......................in...................................... . i i , i .

i ..I.I ,.

Now let's investigate some examples of contant variables and how to use them.
Consider the following declarations (again from [1])-.
int i;

// just an ordinary integer

int *ip;

// uninitialized pointer to
// integer

int * const cp = Si;


const int ci = 7;

// constant pointer to integer


. 1 1

const int *cip;

constant integer

// pointer to constant integer

const int * const cicp = &ci; // constant pointer to constant


// integer
The following assignments are valid:
i = ci;
*cp = ci;

// assign constant integer to integer


// assign constant integer to variable
// which is referenced by constant pointer

cip = &ci;

// change pointer to constant integer

cip = cicp;

// set pointer to constant integer to


// reference variable of constant pointer to
// constant integer

The following assignments are invalid:


ci = 8;
*cip = 7;

// cannot change constant integer value


// cannot, change constant integer referenced

// by pointer cp = &ci;
change value of constant pointer ip = cip;

// cannot
// this

would allow to change value of


// constant integer *cip with *ip

When used with references some peculiarities must be considered. See the following
example program:
include <stdio.h>
int main() {
const int ci = 1;
const int &cr = ci;
int &r = ci;

// create temporary integer for reference

// cr = 7;

// cannot assign value to constant

reference
r = 3;

// change value of temporary integer

print("ci == %d, r == %d\n", ci, r) ;


return 0;
}

When compiled with GNU g++, the compiler issues the following warning:
conversion from ' const int' to " int &' discards const
What actually happens is, that the compiler automatically creates a temporay integer
variable with value of ci to which reference r is initialized. Consequently, when
changing r the value of the temporary integer is changed. This temporary variable
lives as long as reference r.
Reference cr is defined as read-only (constant reference). This disables its use on the
left side of assignments. You may want to remove the comment in front of the
particular line to check out the resulting error message of your compiler.
Functions-.
For example, we can define two different functions max(), one which returns the
maximum of two integers and one which returns the maximum of two strings:
include <stdio.h>
int max(int a, int b) {
if (a > b) return a;
return b;

char *max(char *a, char * b) {*


if (strcmp(a, b) > 0) return a;
return b;
}
int main() {
printf("max(19, 69) = %d\n", max(19, 69));
.
printf("max(abc, def) = %s\n", maxPabc", "def"));
return 0;
}

The above example program defines these two functions which differ in their
parameter list, hence, they define two different functions. The first printf() call in
functionmainO issues a call to the first version of max(), because it takes two integers
as its argument. Similarly, the second printf() call leads to a call of the second version
ofmax().
References can be used to provide a function with an alias of an actual function call
argument. This enables to change the value of the function call argument as it is
known from other languages with call-by-reference parameters:
void foo(int byValue, int fcbyReference) {
byValue = 42;
byReference = 42;
)

void bar() {
int ix, jx;
ix = jx = 1;
foo(ix, jx); ' /* ix
== 1, jx == 42 */ }

2.4 First Object-oriented Extensions:


In this section we present how the object-oriented concepts of section 4 are used in
C+ + .
Classes and Objects:
C++ allows the declaration and definition of classes. Instances of classes are
called objects. Recall the drawing program example of section 5 again. There we have
developed a class Point. In C+ + this would look like this:

class Point {
int _x, _y;
public:

// point coordinates
// begin interface section

void setX(const int val);


void setY(const int val);
int getX() { return _x; }
int getY() { return _y; }
};
Point apoint;

This declares a class Point and defines an object apoint. You can think of a class
definition as a structure definition with functions (or "methods"). Additionally, you
can specify the access rights in more detail. For example, _x and _y are private,
because elements of classes are private as default. Consequently, we explicitly must
"switch" the access rights to declare the following to be public. We do that by using
the keyword public followed by a colon: Every element following this keyword are
now accessible from outside of the class.
We can switch back to private access rights by starting a private section with private:.
This is possible as often as needed:
class Foo {
// private as default ...
public:
// what follows is public until ...
private:
// ... here, where we switch back to private ...
public:
// ... and back to public.
>'

Recall that a structure struct is a combination of various data elements which are
accessible from the outside. We are now able to express a structure with help of a
class, where all elements are declared to be public:
class Struct {
public:

,'.'.
// Structure elements are public by default

// elements, methods
};

This is exactly what C++ does with struct. Structures are handled like classes.
Whereas elements of classes (defined with class) are private by default, elements
of structures (defined with struct) are public. However, we can also use private:
to switch to a private section in structures.
Let's come back to our class Point. Its interface starts with the public section where we
define four methods. Two for each coordinate to set and get its value. The set methods
are only declared. Their actual functionality is still to be defined. The get methods
have a function body: They are defined within the class or, in other words, they are
inlined methods.
This type of method definition is useful for small and simple bodies. It also improve
performance, because bodies of inlined methods are "cooied" into the code wherever a
call to such a method takes place.
On the contrary, calls to the set methods would result in a "real" function call. We
define these methods outside of the class declaration. This makes it necessary, to
indicate to which class a method definition belongs to. For example, another class
might just define a method setX() which is quite different from that of Point. We must
be able to define the scope of the definition; we therefore use the scope operator "::":
void Point::setX(const int val) {
_x = val;
>

void Point :.:setY (const int val) { _y =


val;
}

Here we define method setX() (setY()) within the scope of class Point. The
object apoint can use these methods to set and get information about itself:
Point apoint;
apoint.setX(1);

// Initialization

apoint.setY (1);
//
// x is needed from here, hence, we define it here and
// initialize it to the x-coordinate of apoint
//
int x = apoint.getX();
The question arises about how the methods "know" from which object they are
invoked. This is done by implicitly passing a pointer to the invoking object to the
method. We can access this pointer within the methods as this. The definitions of
methods setX() and setY() make use of class members _x and __y, respectively.

If invoked by an object, these members are "automatically" mapped to the


correct object. We could use this to illustrate what actually happens:
void Point::setX(const int val) {
this->_x = val;

// Use this to reference invoking


- // object

}
void Point::setY(const int val) (
this->_y - val;
}

Here we explicitly use the pointer this to explicitly dereference the invoking object.
Fortunately, the compiler automatically "inserts" these dereferences for class
members, hence, we really can use the first definitions of setX() and setY(). However,
it sometimes make sense to know that there is a pointer this available which
indicates the invoking object. Currently, we need to call the set methods to initialize a
point object v. However, we would like to initialize the point when we define it. We
therefore use special methods called constructors.

2.5 Constructors & Deconstructors:


Constructors:
Constructors are methods which are used to initialize an object at its definition time.
We extend our class Point such that it initializes a point to coordinates (0, 0):
class Point {
int _x, _y;
public:
Point() {
__x = _y = 0;
}
void setX(const int val);
void setY(const int val);
int getX() { return _x;' }

int getY{) { return _y; ) };


Constructors have the same name of the class (thus they are identified to be
constructors). They have no return value. As other methods, they can take arguments.

For example, we may want to initialize a point to other coordinates than (0, 0). We therefore
define a second constructor taking two integer arguments within the class:
class Point {
int _x, _y;
public:
Point() {
_x = _y = 0;
}

Point(const int x, const int y) {


_x = x;
_y - y; }

void setX(const int val); void setY(const int val); int getX()
{ return _x; ) int getY() { return _y; }

};

Constructors are implicitly

called when we define objects of their classes:


Point apoint;
Point bpoint(12, 3 4) ;

// Point::Point()
// Point::Point(const int, const int)

With constructors we are able to initialize our objects at definition time as we have
requested it in section 2 for our singly linked list. We are now able to define a class
List where the constructors take care of correctly initializing its objects.
If we want to create a point from another point, hence, copying the properties of one
object to a newly created one, we sometimes have to take care of the copy process. For
example, consider the class List which allocates dynamically memory for its elements.
If we want to create a second list which is a copy of the first, we must allocate
memory and copy the individual elements. In our class Point we therefore add a third
constructor which takes care of correctly copying values from one object to the newly
created one:
class Point {
int _x, _y;
public:

________

Point() {
_x = _y = 0;
Point(const int x, const int y) {
_x = x;
_y = y; }
Point(const Point Sfrom) {
_x = from._x; _y =
from._y;
}
void setX(const int val);
* void setY(const int val) ;
int getX() { return _x; }
int getY() { return _y; }
}; The third constructor takes a constant reference to an object of class Point as an
argument and assigns _x and _y the corresponding values of the provided object. This
type of constructor is so important that it has its own name: copy constructor. It is
highly recommended that you provide for each of your classes such a constructor,
even if it is as simple as in our example. The copy constructor is called in the
following cases:
Point apoint;
// Point::Point()
Point bpoint(apoint);
// Point::Point(const Point &)
Point cpoint = apoint;
// Point:-.Point (const Point &) With help of
constructors we have fulfilled one of our requirements of implementation of
abstract data types: Initialization at definition time. We still need a mechanism which
automatically "destroys" an object when it gets invalid (for example, because of
leaving its scope). Therefore, classes can define destructors.

Destructors:
Consider a class List. Elements of the list are dynamically appended and removed.
The constructor helps us in creating an initial empty list. However, when we leave the
scope of the definition of a list object, we must ensure that the allocated memory is
released. We therefore define a special method called destructor which is called once
for each object at its destruction time:
RSD-MUM1

void foo() . {
List alist;

// List::List() initializes to
// empty list.

...

// add/remove elements

// Destructor call!

Destruction of objects take place when the object leaves its scope of definition or is
explicitly destroyed. The latter happens, when we dynamically allocate an object and
release it when it is no longer needed. Destructors are declared similar to constructors.
Thus, they also use the name prefixed by a tilde (~) of the defining class:
class Point {
int _x, _y;
public:
Point() {
_x = _y = 0;
}
Point(const int x, const int y) {
_x = xval;
_y = yval; } Point(const
Point &from) {
_x = from._x;
_y = from._y; }
-Point() { /* Nothing to do! */ }
void

setX(const

setY(const

int

int
val);

val)
int

void

getX()

{ return _x; } int getY() { return


_y; } } ;

Destructors take no arguments. It is even invalid to define one, because destructors are
implicitly called at destruction time: You have no chance to specify actual arguments.

Inheritance
In our pseudo language, we formulate inheritance with "inherits from". In C++
these words are replaced by a colon. As an. example let's design a class for 3D
points. Of course we want to reuse our already existing class Point. We start
designing our class as follows:
class Point3D : public Point {
int _z;
public:
Point3D() {
setX(O);
setY(O);

Point3D(const int x, const int y, const int


z) { setX(x); setY(y);

~Point3D() { /* Nothing to do */ }
int getZ() { return _z; }
void setZ(const int val) { _z = val; }

2.6 Types of Inheritance:


You might notice again the keyword public used in the first line of the class
definition {its signature). This is necessary because C++ distinguishes
two types of I inheritance: public and private. As a default, classes are
privately derived from each I other. Consequently, we must explicitly tell the
compiler to use public inheritance.
The type of inheritance influences, the access rights to elernents of the various I
superclasses. Using public inheritance, everything which is declared private in
a I superclass remains private in the subclass. Similarly, everything which
I is public remains public. When using private inheritance the things are quite
different I as is shown in table 2.6.
Table 2.6 Access rights and inheritance.

The leftmost column lists possible access rights for elements of classes. It also includes a
third type protected. This type is used for elements which should be directly usable in
subclasses but which should not be accessible from the outside. ' Thus, one could say
elements of this type are between private and publicelements in that they can be used
within the class hierarchy rooted by the corresponding class.
The second and third column show the resulting access right of the elements of a
superclass when the subclass is privately and publically derived, respectively.

Construction:
When we create an instance of class Point3D its constructor is called. Since Point3D is
derived from Point the constructor of class Point is also called. However, this
constructor is called before the body of the constructor of class Point3D is executed.
In general, prior to the execution of the particular constructor body, constructors of
every superclass are called to initialize their part of the created object.
When we create an object with
Point3D point(1, 2, 3);
the second constructor of Point3D is invoked. Prior to the execution of the constructor
body, the constructor Point() is invoked, to initialize the point part of objectpoint.
Fortunately, we have defined a constructor which takes no arguments. This constructor
initializes the 2D coordinates_x and _y to 0 (zero). AsPoint3Dis only derived from
Point there are no other constructor calls and the body ofPoint3D(const int, const int,
const int) is executed. Here we invoke methods setX()and setY() to explicitly override
the 2D coordinates. Subsequently, the value of the third coordinate _z is set.
This is very unsatisfactory as we have defined a constructor Point() which takes two
arguments to initialize its coordinates to them. Thus we must only be able to tell, that
instead of using the default constructor Point() the paramterized Point(const int, const
int) should be used. We can do that by specifying the desired constructors after a
single colon just before the body of constructor Point3D():
class Point3D : public Point {
public:
Point3D{) { ... }

Point3D(
const, int x,
const int y,
const int z) : Point (x, y) {
_z = z; }
};
If we would have more superclasses we simply provide their constructor calls as a
comma separated list. We also use this mechanism to create contained objects. For
example, suppose that class Part only defines a constructor with one argument. Then
to correctly create an object of class Compound we must invoke Part()with its
argument:
class Compound {
Part part;
public:
Compound(const int partParameter) : part(partParameter) {
}
m

' -

>;

This dynamic initialization can also be used with built-in data types. For example, the
constructors of class Point could be written as:
PointO : _ x ( 0 ) , _y(0) {}
,

Point(const int x, const int y) : _x(x), _y(y) O


You should use this initialization method as often as possible, because it allows the
compiler to create variables and objects correctly initialized instead of creating them
with* a default value and to use an additional assignment (or other mechanism) to set
its value.
Destruction:
If an object is destroyed, for example by leaving its definition scope, the destructor of
the corresponding class is invoked. If this class is derived from other classes their .
destructors are aiso called, leading to a recursive call chain.

Multiple Inheritance
C++ allows a class to be derived from more than one superclass, as was already
briefly mentioned in previous sections. You can easily derive from more than one
class by specifying the superclasses in a comma separated list:
class DrawableString : public Point, public DrawableObject {
public:
DrawableString(...) :
Point(...),
DrawableObject(...) {

}
~DrawableString() { ... }

};

We will not use this type of inheritance in the remainder of this tutorial. Therefore we
will not go into further detail here.
Polymorphism:
In our pseudo language we are able to declare methods of classes to be virtual, to
force their evaluation to be based on object content rather than object type. We can
also use this in C++:
class DrawableObject {
public:
virtual void print();
};

Class DrawableObject defines a method printQ which is virtual. We can derive from
this class other classes:

class Point : public DrawableObject {

public:

void print() { ... }

..,

};

Again, print() is a virtual method, because it inherits this property from


DrawableObject. The function display() which is able to display any kind of
drawable object, can then be defined as:
void display(const DrawableObject &ob j) {
// prepare anything necessary
'....

obj.print ();

When using virtual methods some compilers complain if the corresponding class
destructor is not declared virtual as well. This is necessary when using pointers to
(virtual) subclasses when it is time to destroy them. As the pointer is declared as
superclass normally its destructor would be called. If the destructor is virtual, the
destructor of the actual referenced object is called (and then, recursively, all
destructors of its superclasses). Here is an example adopted from [1]:
class Colour {
public:
virtual -Colour();
};
class Red : public Colour {
public:
-Red();

/-/ Virtuality inherited from Colour

};
class LightRed : public Red {
public:
-LightRed();
);

Using these classes, we can define a palette-as follows:


Colour *palette[31;
palette[0] new Red; // Dynamically create a new Red object^
palettejl] new LightRed;
palettef 2] = new Colour;
The newly introduced operator new creates a new object of the specified type in
dynamic memory and returns a pointer to it. Thus, the first new returns a pointer to
an allocated object of class Red and assigns it to the first element of array palette.

The elements of palette are pointers to Colour and, because Red is-a Colour the
assignment is valid.
The contrary operator to new is delete which explicitly destroys an object referenced
by the provided pointer. If we apply delete to the elements of palette the following
destructor calls happen:
delete palette[0] ;
// Call destructor ~Red() followed by -Colour()
delete palette [1];
// Call ~LightRed(), ~Red() and -Colour()
delete palette[2];
// Call -Colour()

The"various destructor calls only happen, because of the use of virtual destructors. If
we would have not declared them virtual, each delete would have only called
Colour() (because palette[i] is of type pointer to Colour).
Abstract Classes:
Abstract classes are defined just as ordinary classes. However, some of their methods
are designated to be necessarily defined by subclasses. We just mention
theirsignature including their return type, name and parameters but not a definition.
One could say, we omit the method body or, in other words, specify "nothing". This is
expressed by appending "= 0" after the method signatures:
class DrawableObject {
public:
virtual void print() = 0;

This class definition would force every derived class from which objects should be
created to define a method print(). These method declarations are also calledpure
methods.
,
Pure methods must also be declared virtual, because we only want to use objects
from derived classes. Classes which define pure methods are called abstract classes.
Operator Overloading:
If we recall the abstract data type for complex numbers, Complex, we could create a
C++ class as follows-.

class Complex {
double _real,
_imag; public:
Complex() : _real(0.0), _imag(0.0) {}
Complex(const double real, const double imag) :
_real(real), _imag(imag) {}
Complex add(const Complex op);
Complex mul(const Complex op) ;

};

We would then be able to use complex numbers and to "calculate" with them:
Complex a ( 1 . 0, 2 . 0 ) , b ( 3 . 5 , 1 . 2 ) , c;
c = a.add(b);
Here we assign c the sum of a and b. Although absolutely correct, it does not provide
a convenient way of expression. What we would rather like to use is the well-known
"+" to express addition of two complex numbers. Fortunately, C++ allows us to
overload almost all of its operators for newly created types. For example, we could
define a "+" operator for our class Complex:
class Complex {

public:

Complex operator +(const Complex Sop) {


double real = _real + op._real,
'

imag = _imag + op._imag;


return(Complex(real, imag)); }
};

In this case, we have made operator + a member of class Complex. An expression of


the form

c = a + b;

is translated into a method call


c = a.operator + ( b ) ;
Thus, the binary operator + only needs one argument. The first argument is implicitly
provided by the invoking object (in this case a).
However, an operator call can also be interpreted as a usual function call, as in
c = operator + ( a , b);
In this case, the overloaded operator is not a member of a class. It is rather defined
outside as a normal overloaded function. For example, we could define operator+ in
this way:
class Complex {
public:
double real() { return _real; }
double imag() { return _imag; }
// No need to define operator here! }; Complex
operator +(Complex &opl, Complex &op2) {
double real = opl.real() + op2.real(),
imag = opl.imagO + op2.imag();
return(Complex(real, imag));
}

In this case we must define access methods for the real and imaginary parts because
the operator is defined outside of the class's scope. However, the operator is so closely
related to the class, that it would make sense to allow the operator to access the private
members. This can be done by declaring it to be a friend of classComplex.
Friends:
We can define functions or classes to be friends of a class to allow them direct access
to its private data members. For example, in the previous section we would like to
have the function for operator + to have access to the private data members _real and
_imag of class Complex. Therefore we declare operator + to be a friend of class
Complex:

class Complex {
public:
friend Complex operator +(
const Complex &, const
Complex & ); };
Complex operator +(const Complex &opl, const Complex &op2)
{ double real = opl._real + op2._real, imag = opl._imag +
op2._imag; return(Complex(real, imag)); }

You should not use friends very often because they break the data hiding principle in
its fundamentals. If you have to use friends very often it is always a sign that it is time
to restructure your inheritance graph.

Chapter 3 How to Write a


Program
3.1 Introduction:
Until now, we have only presented parts of or very small programs which could easily
be handled in one file. However, greater projects, say, a calendar program, should be
split into manageable pieces, often called modules. Modules are implemented in
separate files and we will now briefly discuss how modularization is done in C and C+
+. This discussion is based on UNIX and the GNU C++ compiler. If you are using
other constellations the following might vary on your side. This is especially
important for those who are using integrated development environments (IDEs), for
example, Borland C++.
Roughly speaking, modules consist of two file types: interface descriptions and
implementation files. To distinguish these types, a set of suffixes are used when
compiling C and C++ programs. Table 3.1 shows some of them.
Table 3.1 Extensions and file types.

In this tutorial we will use .h for header files, .cc for C++ files and .tpl for template
definition files. Even if we are writing "only" C code, it makes sense to use.cc to force
the compiler to treat it as C++. This simplifies combination of both, since the internal
mechanism of how the compiler arrange names in the program differs between both
languages v.

3.2 Compilation Steps:


The compilation process takes .cc files, preprocess them (removing comments, add
header files) v and translates them into object files v. Typical suffixes for that file type
are .o or .obj.
After successful compilation the set of object files is processed by a linker. This
program combine the files, add necessary libraries v and creates an executable. Under
UNIX this file is called a.out if not other specified. These steps are illustrated in Figure
3.1.

Fig. 3.1 Compilation steps


With modern compilers both steps can be combined. For example, our small example
programs can be compiled and linked with the GNU C++ compiler as follows
("example.cc" is just an example name, of course):
gcc example.cc

A Note about Style:


Header files are used to describe the interface of implementation files. Consequently
they are included in each implementation file which uses the interface of the I
particular implementation file. As mentioned in previous sections this inclusion is I
achieved by a copy of the content of the header file at each preprocessc
#includestatement, leading to a "huge" raw C++ file.
To avoid the inclusion of multiple copies caused by mutual dependencies we use I
conditional coding. The preprocessor also defines conditional statements to cheda
for various aspects of its processing. For example, we can check if a macro is already]
defined:
#ifndef MACRO
#define MACRO /* define MACRO */
#endif

The lines between #ifndef and #endif are only included, if MACRO is not alreafl
defined. We can use this mechanism to prevent multiple copies:
MASTFR

np RTTC;INF<;C; OF ADMINISTRATION

' '

"

RSD-M--*

'

** Example for a header file which "checks' if it is


** already included. Assume, the name of the header file
** is "myheader.h'
*/
#ifndef __MYHEADER_H
#define __MYHEADER_H
/*
** Interface declarations go here
*/
#endif /* ____MYHEADER_H */
__MYHEADER_H is a unique name for each header file. You might want to follow the
convention of using the name of the file prefixed with two underbars. The first time
the file is included,____MYHEADER_H is not defined, thus every line is included and
processed. The first line just defines a macro called____MYHEADER_H. If accidentally
the file should be included a second time (while processing the same input file),
__MYHEADER_H is defined, thus everything leading up to the #endif is skipped.
Generic Types (Templates):
In C+ + generic data types arc called class templates v or just templates for short. A
class template looks like a normal class definition, where some aspects are
represented by placeholders, hi the forthcoming list example we use this mechanism
to generate lists for various data types:

We now useT at any place where normally the type of the actual objects are expected.
For example, each list provides a method to append an element' to it. We can now
define this method as shown above with use of T.
An actual list definition must now specify the type of the list. If we stick to. the class
expression used before, we have to create a class instance. From this class instance we
can then create "real" object instances:
List<int> integerList;
Here we create a class instance of a List which takes integers as its data elements. We
specify the type enclosed in angle brackets. The compiler now applies the provided
argument "int" and automatically generates a class definition where the placeholder T
is replaced by int, for example, it generates the following method declaration for
append():
i

void append(const int data);


Templates can take more than one argument to provide more placeholders. For I
example, to declare a dictionary class which provides access to its data elements by a
key, one can think of the following declaration:
template <class K, class T>
class Dictionary {
public:
K getKey(const T from);
T getData(const K key);
};

Here we use two placeholders to be able to use dictionaries for various key and data
types.
Template arguments can also be used to generate parameterized class definitions] For
example, a stack might be implemented by an array of data elements. The size dt the
array could be specified dynamically:
template <class T, int size>
class Stack {
T _store[size];
public:

};
Stack<int,128> mystack;

In this example, mystack is a stack of integers using an array of 128 elements.


However, in the following we will not use parameterized classes.
Shape and Traversal:
In the following discussion we distinguish between a data structure's shape and its
traversing strategies. The first is the "look", which already provides plenty
information about the building blocks of the data structure.
A traversing strategy defines the order in which elements of the data structure are to
be visited. It makes sense to separate the shape from traversing strategies, because
some data structures can be traversed using various strategies.
Traversing of a data structure is implemented using iterators. Iterators guarantee to
visit each data item of their associated data structure in a well defined order. They
must provide at least the following properties:
1. Current element. The iterator visits data elements one at a time. The element
which is currently visited is called "current element".
2. Successor function. The execution of the step to the next data element depends on
the traversing strategy implemented by the iterator. The "successor function" is
used to return the element which is next to be visited: It returns the successor of
the current element.
3. Termination condition. The iterator must provide a mechanism to check whether
all elements are visited or not.
Properties of Singly Linked Lists
When doing something object-oriented, the first question to ask is
What are the basic building blocks of the item to implement?
Have a look at Figure 3.2, which shows a list consisting of four rectangles. Each
rectangle has a bullet in its middle, the first three point to their right neighbour. Since
thelast rectangle have no right neighbour, there is no pointer.

Fig. 3.2 Basic building blocks of a singly linked list


First let's choose names for these building blocks. Talking of rectangles is not
appropriate, because one can think of a figure using circles or triangles.
Within the scope of graphs the name node is .used. A node contains a pointer to its
successor. Thus, the list in the figure consists of nodes, each of which hasexactly one
pointer associated with it.

Three types of nodes can be distinguished:

The first node (head), which has no predecessor,

the middle nodes, which have exactly one predecessor and exactly one successor and

the last node (tail), which has no successor.


Note that the nodes do not carry any content. This is because the bare data structure
list consists only of nodes, which are strung together. Of course real applications need
nodes, carrying some content. But in the sense of object-orientation this is a
specialization of the nodes.
From the figure we can see, that a list can only be used with one traversing strategy:
forward cursor. Initially, the head will be the first current element. The successor
function simply follows the pointer of the current node. The termination function
checks the current element to be the tail.
Note that it is not possible to go back nor to start in the middle of the list. The latter
would contradict the requirement, that each element must be visited.
The next question is, what are the operations offered by a list? A list only defines two
well known nodes head and tail. Let's have a deeper look to them.
A new node can be put-in-front of the list such that:

its pointer is set to the current head,

the new node becomes the new head.


Similarly, a new node can easily be appended to the tail:

the tail pointer is set to the new node,

. the new node becomes the new tail.


The inverse function to put in front is delete-from-front:

the successor node of the head becomes the new head,

the formerly head node is discarded.


You should be able to figure out why there \s no cheap Inverse append function.
Finally, there exist three other cheap primitives, whose meaning is straight forward.
Thus, we will not examine them any further. However, we present them here fo: .
completeness:

get-first: returns the (data of the) head node,

get-last: returns the (data of the) tail node and

is-empty: returns whether the list is empty or not.

MASTER OF BUSINESS OF ADMINISTRATION


J

RSD-MUf.'.:-

Shape Implementation:
Node Templates:
The basic building block of a list is the node. Thus, let's first declare a class for it. A
node has nothing more than a pointer to another node! Let's assume, that this
neighbour is always on the right side.
Have a look at the following declaration of class Node,
class Node {
Node *_right;
public:
Node(Node *right = NULL) : _right(right) {}
Node(const Node &val) : _right(val._right) {}
const Node *right() const { return _right; }
Node *&right() { return _right; }
Node Soperator =(const Node &val) {
_right = val._right;
return *this;
}

const int operator.==(const Node &val) const {


return _right == val._right; } const int
operator !=(const Node &val) const {
return !(*this == val); } };

A look to the first version of method righto contains a const just before the method
body. When used in this position, const declares the method to be constant regarding
the elements of the invoking object. Consequently, you are only allowed to use this
mechanism in method declarations or definitions, respectively.
This type of const modifier is also used to check for overloading. Thus,
class Foo {
int foo'O const;

int foo ();


};

declare two different methods. The former is used ,in constant contexts whereas the
second is used in variable contexts.
Although template class Node implements a simple node it seems to define plenty of
functionality. We do this, because it is good practice to Offer at least the following
functionality for each defined data type:

Copy Constructor. The copy constructor is needed to allow definition of objects


which are initialized from already existing ones.

operator =. Each object should know how to assign other objects (of the same type)
to itself. In our example class, this is simply the pointer assignment.

operator = = . Each object should know how to compare itself with another object.

The unequality operator "!=" is implemented by using the definition of the equality
operator. Recall, that this points to the invoking object, thus,
Node a, b;
if (a 1= b) ...
would result in a call to operator !=() with this set to the address of a. We dereference
this using the standard dereference operator "*". Now, *this is an object of class Node
which is compared to another object using operator = = (). Consequently, the
definition of operator = = () of class Node is used. Using the standard boolean NOT
operator "!" we negate the result and obtain the truth value of operator !=().
The above methods should be available for each class you define. This ensures that
you can use your objects as you would use any other objects, for example integers. If
some of these methods make no sense for whatever reason, you should declare them in
a private section of the class to explicitly mark them as not for public use. Otherwise
the C++ compiler would substitute standard operators.
Obviously, real applications require the nodes to carry data. As mentioned above, this
means to specialize the nodes. Data can be of any type, hence, we are using the
template construct.
template <class T>
class DataNode : public Node {
T _data;
public:
DataNode(const T data, DataNode *right = NULL) :

' Node (right)', _data(data) {)


DataNode(const DataNode &val) :
Node(val), _data(val._data) {}
const DataNode *right() const (
return( (DataNode *) Node:-.right ().) ;
}

DataNode *&right() I return! (DataNode *'&) Node:-.right ()) ; }


const T Sdata() const { return _data;

T &data() { return _data; }


DataNode Soperator =(const DataNode &val)- {
Node::operator =(val);
_data = val._data;
return *this;
)
const int operator ==(const DataNode &val) const {
return(
Node::operator ==(val) &&
_data == val._data);
}

const iht operator !=(const DataNode &val) const {


return !(*this == val);
}
};

The above template DataNode simply specializes class Node to carry data of any type.
It adds functionality to access its data element and also offers the same set of
standard functionality: Copy Constructor, operator =() and operator = = (). Note, how
we reuse functionality already defined by class Node.
List Templates:
Now we are able to declare the list template. We also use the template mechanism
here, because we want the list to carry data of arbitrary type. For example, we want to
be able to define a list of integers. We start with an abstract class template ListBase
which functions as the base class of all other lists. For example, doubly linked lists
obviously share the same properties like singly linked lists.

template <class T>


class ListBase {
public:

1
virtual ~ListBase() {}

// Force destructor to be
// virtual

virtual void flush() = 0;


virtual void putlnFront(const T data) = 0;
virtual void append(const T data) = 0;
virtual void delFromFront() =0;
virtual const T &getFirst() const = 0;
virtual T SgetFirstO = 0;
virtual const T &getLast() const = 0;
virtual T &getLast() = 0;
virtual const int isEmptyO const =
0; };

What we actually do is to describe the interface of every list by specifying the


prototypes of required methods. We do that for every operation we have identified in
section 10.3. Additionally, we also include a method flush() which allows us to delete
all elements of a list.
For operations get-first and get-last we have declared two versions. One is for use in
a constant context and the other in a variable context.
With this abstract class template we are able to actually define our list class template:
template <class T>
class List : public ListBase<T> {
DataNode<T> *_head, *_tail;
'

public:
List() : _head(NULL), _tail(NULL) {}
List(const List &val) : _head(NULL), _tail(NULL) {
*this = val;
)

virtual -List() { flush(); )


virtual void flush();
________________________________________________________________________

virtual void
putlnFront{const T data); virtual void
append(const T data); virtual void
delFromFront() ;
virtual const T &getFirst() const { return _head->data(); }
virtual T &getFirst() { return _head->data(); } virtual
const T &getLast() const { return _tail->data(); } virtual
T SgetLast() { return _tail->data(); } virtual const int
isEmptyO const { return _head == NULL; } List &operator
=(const List Sval) { flush();
DataNode<T> *walkp = val._head; while
(walkp) append(walkp->data()); return
*this; }
const int operator ==(const List &val) const {
if (isEmptyO && val. isEmpty () ) return 1;
DataNode<T> *thisp = _head,
*valp = val._head;
while (thisp && valp) {
if (thisp->data() != valp->data()) return 0;
thisp = thisp->right(); valp = valp>right() ;
}
return 1;
}
const int operator !=(const List &val) const {
return ! (*this == val) ;
\

. .
friend class ListIterator<T>;

}V

The constructors initialize the list's elements _head and _tail to NULL which is the
NUL pointer in C and C+ + . You should know how to implement the other methods
from your programming experience. Here we only present the implementation of
method putlnFront():
template <class T> void
List<T>::putInFront(const T data) {

, .

_head = new DataNode<T>(data, _head);


if (!_tail) _tail .= _head;
} /* putlnFront */

If we define methods of a class template outside of its declaration, we must also


specify the template keyword. Again we use the new operator to create a new data
node dynamically. This operator allows initialization of its created object with
arguments enclosed in parenthesis. In the above example, new creates a new instance
of class DataNode < T >. Consequently, the corresponding constructor is called.
Also notice how we use placeholder T. If we would create a class instance of class
template List, say, List <int> this would also cause creation of a class instance of class
template DataNode, viz DataNode<int>.
The last line of the class template declaration declares class template Listlterator to be
a friend of List. We want to separately define the list's iterator. However, it is closely
related, thus, we allow it to be a friend.
Iterator Implementation:
In section 10.2 we have introduced the concept of iterators to traverse through a data
structure. Iterators must implement three properties:
Current element.
Successor function.
Termination condition.
Generally speaking, the iterator successively returns data associated with the current
element. Obviously, there will be a method, say, current() which implements this
functionality. The return type of this method depends on the type of data stored in the
particular data structure. For example, when iterating over List <int> the return type
should be int.
The successor function, say, succQ, uses additional information which is stored in
structural elements of the data structure. In our list example, these are the nodes which
carry the data and a pointer to their right neighbour. The type of the structural
elements usually differs from that of the raw data. Consider again our List <int>
where succQ must use DataNode <int> as structural elements.

The termination condition is implemented by a method, say, terminate(), which


returns TRUE if (and only if) all data elements of the associated data structure have
been Visited. As long as succQ can find an element not yet visited, this method
returns FALSE.
Again we want to specify an abstract iterator class which defines properties of every
iterator. The thoughts above lead to the following declaration:
template <class Data, class Element>
class Iterator {
protected:
'
Element _start,
_current;
public:
Iterator(const Element start) :
_start(start), _current(start) {}
Iterator(const Iterator &val) :
_start(val._start), _current(val._current) {}

virtual ~Iterato*r () {}
virtual const Data current!) const = 0;
virtual void succ() =0 ;
virtual const int terminate!) const =.0;
virtual void rewind() { _current = _start; }
Iterator Soperator =(const Iterator &val) {
_start = val._start;
_current = val._current;
return *this;
}
const int operator ==(const Iterator &val) const {
return(_start == val._start && _current == val._current);
const int operator !=(const Iterator Sval) const {
return !(*this == val);
} );

Again we use the template mechanism to allow the use of the iterator for any data
structure which stores data of type Data and which uses structural elements of
typeElement. Each iterator "knows" a starting (structural) element and the current
element.

We make both accessible from derived classes because derived iterators need access
to them to implement the following iterator properties. You should already understand
how the constructors operate and why we force the destructor to be virtual.
Subsequently we specify three methods which should implement the three properties
of an iterator. We also add a method rewind() which simply sets the current element to
the start element. However, complex data structures (for example hash tables) might
require more sophisticated rewind algorithms. For that reason we also specify this
method to be virtual, allowing derived iterators to redefine it for their associated data
structure.
The last step in the iterator implementation process is the declaration of the list
iterator. This iterator is highly related to our class template List, for example, it is clear
that the structural elements are class templates DataNode. The only "open" type is the
one for the data. Once again, we use the template mechanism to provide list iterators
for the different list types:
template <class T>
class Listlterator : public Iterator<T, DataNode<T> *> {
public:
Listlterator(const List<T> Slist) :
Iterator<T, DataNode<T> *>(list._head) {}
Listlterator(const Listlterator &val) :
Iterator<T, DataNode<T> *>(val) {}
virtual const T current() const { return _current->data ();
virtual void succ() { __current = _current->right (); }
virtual const int terminate() const { return
_current == NULL;
T Soperator ++(int) {
T &tmp = _current->data() ;
succ(); return tmp;
Listlterator ^operator =(const Listlterator &val)
{ Iterator<T, DataNode<T> *>:^operator =(val);

return *this;
}
};

The class template Listlterator is derived from Iterator, The type of data is, of course,
the type for which the list iterator is declared, hence, we insert placeholder Tfor the
iterator's data type Data. The iteration process is achieved with help of the structural
elements of type DataNode. Obviously the starting element is the head of the list
_head which is of type DataNode <T>*. We choose this type for the element type
Element.
Note that the list iterator actually hides the details about the structural elements. This
type highly depends on the implementation of the list. For example, if we woufd have
chosen an array implementation, we may have used integers as structural elements
where the current element is indicated by an array index.
The first constructor takes the list to traverse as its argument and initializes its iterator
portion accordingly. As each Listlterator <T> is a friend of List <T> it has access to
the list's private members. We use this to initialize the iterator to point to the head of
the list.
We omit the destructor because we do not have any additional data members for the
list iterator. Consequently, we do nothing special for it. However, the destructor of
class template Iterator is called. Recall that we have to define this destructor to force
derived classes to also have a virtual one.
The next methods just define the required three properties. Now that we have
structural elements defined as DataNode <T> * we use them as follows:
the current element is the data carried by the current structural element,

the successor function is to set the current structural element to its right neighbour
and

the termination condition is to check the current structural element if it is the


NULL pointer. Note that this can happen only in two cases:

1. The list is empty. In this case the current element is already NULL because the
list's head _head is NULL.
2. The current element reached the last element. In this case the previous successor
function call set the current element to the right neighbour of the last element
which is NULL.
We have also included an overloaded postincrement operator "+ + ". To distinguish
this operator from the preincrement operator, it takes an additional (anonymous)
integer argument. As we only use this argument to declare a correct operator prototype
and because we do not use the value of the argument, we omit the name of the
argument. The last method is the overloaded assignment operator for list iterators,
Similar to previous assignment operators, we just reuse already defined assignments of
superclasses; Iterator <T>::operator =() in this case.

The other methods and operators, namely rewind(), operator =~Q and operator !=() are
all inherited from class template Iterator.
Example Usage:
The list template as introduced in previous sections can be used as follows: int
main() {
List<int> list;
int ix;
for (ix = 0; ix < 10; ix++) list.append(ix);
ListIterator<int> iter(list);
while (liter.terminate()) {
printf("%d ", iter.current()) ;
iter. s.ucc() ;
puts("u);
return 0;
As we have defined a postincrement operator for the list iterator, the loop can also be
written as:
while (!iter.terminate())
print("%d "\ iter++);
Discussion:
Separation of Shape and Access Strategies
The presented example focusses on an object-oriented view, In real applications singly
linked lists might offer more functionality. For example, insertion of new data I items
should be no problem due to the use of pointers;
1. Take the successor pointer of the new element and set it to the element which
should become its right neighbour,
2. Take the successor pointer of the element after which the new element should be I
inserted and set it to the new element.
Two simple operations. However, the problem is to designate the element after wl
the new element should be inserted.

Again, a mechanism is needed which traverse through the list. This time, however,
traversion stops at a particular element: It is the element where the list (or the data '
structure) is modified.
Similar to the existence of different traversing strategies, one can think of different
modification strategies. For example, to create a sorted list, where elements are sorted
in ascending order, use an ascending modifier.
These modifiers must have access to the list structural elements, and thus, they would
be declared as friends as well. This would lead to the necessity that everymodifier
must be a friend of its data structure. But who can guarantee, that no modifier is
forgotten?
A solution is, that modification strategies are not implemented by "external" classes as
iterators are. Instead, they are implemented by inheritance. If a sorted list is needed, it
is a specialization of the general list. This sorted list would add a method, say insert(),
which inserts a new element according to the modification strategy.
To make this possible, the presented list template must be changed. Because now,
derived classes must have access to the head and tail node to implement these
strategies, consequently, Jiead and Jail should be protected;
Iterators:
The presented iterator implementation assumes, that the data structure is not changed
during the use of an iterator. Consider the following example to illustrate this:
List<int> ilist;
int ix;
for (ix = 1; ix < 10; ix++)
ilist.append(ix);
ListIterator<int> iter(ilist);
while (!iter.terminate()) {
printf("%d ", iter,current());
iter.succ();
}
printf{"\n");
ilist.putlnFront(0);
iter.rewind();
while (! iter .terminate () ) {
printf("%d ", iter.current());

iter.succ();
}
printf("\n"); This code

fragment prints
123456789
1 2 3 4 5 6 7 8 9 instead
of 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 89
This is due to the fact, that our list iterator only stores pointers to the list structural
elements. Thus, the start element _start is initially set to point to the location where
the list's head node _head points to. This simply leads to two different pointers
referencing the same location. Consequently, when changing one pointer as it is
done by invoking putlnFront() the other pointer is not affected. For that reason, when
rewinding the iterator after putlnFront() the current element is set to the start element
which was set at the time the iterator constructor was called. NOW; the start element
actually references the second element of the list.
Exercises
1. Similar to the definition of the postincrement operator in class template
Listlterator, one could define a preincrement operator as: T &operator + + ( )
{ succ();
return _current->data() ; } What

'

problems occur?
2. Add the following method
int remove(const T &data);
to class template List. The method should delete the first occurrence of data in
the list. The method should return 1 if it removed an element or 0 (zero)
otherwise.
*
What functionality must data provide? Remember that it can be of any type,
especially user defined classes!
3. Derive a class template CountedList from List which counts its elements. Add a I
method count() of arbitrary type which returns the actual number of elements
stored in the list. Try to reuse as much of List as possible.
4. Regarding the iterator problem discussed in section 10.7. What are, possible
solutions to allow the list to be altered while an iterator of it is in use?

5. This chapter is a short survey of programming techniques. We use a simple example to

illustrate the particular properties and to point out their main ideas and problems.
Roughly speaking, we can distinguish the following learning curve of someone who
learns to program:

Unstructured programming,

procedural programming,

modular programming and

object-oriented programming.

This chapter is organized as follows. Sections 2.1 to 2.3 briefly describe the
first three programming techniques. Subsequently, we present a simple example of
how modular programming can be used to implement a singly linked list module
(section 2.4). Using this we state a few problems with this kind of technique in section
2.5. Finally, section 2.6 describes the fourth programming technique.
6.

7.

3.3 Unstructured Programming:

Usually, people start learning programming by writing small and simple


programs consisting only of one main program. Here ' "main program" stands for a
sequence of commands or statements which modify data which is global throughout
the whole program. We can illustrate this as shown in Fig. 3.3.
8.

9.

10.
Fig. 3.3 Unstructured programming.
The main program directly operates on global data.

As you should all know, this programming techniques provide tremendous


disadvantages once the program gets sufficiently large. For example, if the same
statement sequence is needed at different locations within the program, the sequence
must be copied. This has lead to the idea to extract these sequences, name them and
offering a technique to call and return from these procedures.
11.

12.

Procedural Programming:

With procedural programming you are able to combine returning sequences of


statements into one single place. A procedure call is used to invoke the procedure.
After the sequence is processed, flow of control proceeds right after the position
where the call was made (Fig. 3.4).
13.

14. Fig. 3.4 Execution of procedures.


After processing flow of controls proceed where the call was made.
16.
With introducing parameters as well as procedures of procedures ( subprocedures)
17.
programs can now be written more structured and error free. For example, if a
18.
procedure is correct, every time it is used it produces correct results. Consequently,
19.
in cases of errors you can narrow your search to those places which are not proven
20. to be correct. Now a program can be viewed as a sequence of procedure
calls"""'. The main program is responsible to pass data to the individual calls, the data
is processed by the procedures and, once the program has finished, the resulting data
is presented. Thus, the flow of data can be illustrated as a hierarchical graph, a tree, as
shown in Fig. 3.5 for a program with no subprocedures.
21.
Droeram
15.

22.

Fig. 3.*> Procedural programming. The main program coordinates


calls to procedures and hands over appropriate data as parameters.

23.

24. To sum up: Now we have a single program which is devided into small pieces

called procedures. To enable usage of general procedures or groups of procedures also


in j other programs, they must be separately available. For that reason, modular I
programming allows grouping of procedures into modules.
25. Modular Programming:
26.

With modular programming procedures of a common functionality are grouped


together into separate modules. A program therefore no longer consists of only one
single part. It is now devided into several smaller parts which interact through procedure
calls and which form the whole program (Fig. 3.6).
27.

Fig. 3.6 Modular programming. The main program coordinates calls to


procedures in separate modules and hands over appropriate data as parameters.
28.

Each module can have its own data. This allows each module to manage an
internal state which is modified by calls to procedures of this module. However, there
is only one state per module and each module exists at most once in the whole
program.
29.

30.

An Example with Data Structures

Programs use data structures to store data. Several data structures exist, for
example lists, trees, arrays, sets, bags or queues to name a few. Each of these data
structures can be characterized by their structure and their access methods.
31.

32.

Handling Single Lists

33.

You all know singly linked lists which use a very simple structure, consisting of

34.

35.

Fig. 3.7 Structure of a singly linked list

Singly linked lists just provides access methods to append a new element to
their end and to delete the element at the front. Complex data structures might use
already existing ones. For example a queue can be structured like a singly linked list.
However, queues provide access methods to put a data element at the end and to get
the first data element (first-in first-out (FIFO) behaviour).
36.

We will now present an example which we use to present some design


concepts. Since this example is just used to illustrate these concepts and problems it is
neither complete nor optimal. Refer to-chapter 10 for a complete object-oriented
discussion about the design of data structures.
37.

Suppose you want to program a list in a modular programming language such


as C or Modula-2. As you believe that lists are a common data structure, you decide to
implement it in a separate module. Typically, this requires you to write two files: the
interface definition and the implementation file. Within this chapter we will use a very
simple pseudo code which you should understand immediately. Let's assume, that
comments are enclosed in * V*... */". Our interface definition might then look similar
to that below:
38.

39.

/*

Interface definition for a module which implements

a singly linked list for storing data of any type. */


40.

MODULE Singly-Linked-

List-1 BOOL
list_initialize() ;
41.

BOOL list_append(ANY data);

42.

BOOL list_delete();
43.

list_end();

44.

ANY list_getFirst 0;

45.

ANY list_getNext();

46.

BOOL list_isEmpty();

47.

END -Singly-Linked-List-1

Interface definitions just describe what is available and not how it is made
available. You hide the information of the implementation in the implementation file.
This is a 1 fundamental principle in software engineering, so let's repeat it: You hide
information , of the actual implementation (information hiding). This enables you to
change the implementation, for example to use a faster but more memory consuming
algorithm for storing elements without the need to change other modules of your
program: The calls to provided procedures remain the same.
48.

49.

idea of this interface is as follows: Before using the list one has to
50.

nitializeO to initialize variables local to the module. The following


51.

edures implement the mentioned access methods append and delete.


append procedure needs a more detailed discussion.

The
call
HstJ
two I
proc
The

Function list_append() takes one argumentdata of arbitrary type. This is necessar


since you wish to use your list in several different environments, hence, the type of the
data elements to be stored in the list is not known beforehand. Consequently, you J
have to use a special type ANY which allows to assign data of any type to it^. The J
third procedure list_end() needs to be called when the program terminates to enable!
the module to clean up its internally used variables. For example you might want tol
release allocated memory.
52.

With the next two procedures list_getFirst() and Hst_getNext() a simple


mechanism to traverse through the list is offered. Traversing can be done using the
following loop:
53.

54.

ANY data;

55.

data <- list_getFirst();

56.

WHILE data IS VALID DO


57. doSomethinq(data\;
58. data <- list_getNext() ;

59.

END

Now you have a list module which allows you to use a list with any type of
data elements. But what, if you need more than one list in one of your programs?
60.

Handling Multiple Lists:

61.

You decide to redesign your list module to be able to manage more than one
list. You therefore create a new interface description which now includes a definition
for a list handle. This handle is used in every provided procedure to uniquely identify
the list in question. Your interface definition file of your new list module looks like
this:
62.

/*

63.
64.
65.

* A list module for more than one list.


*/

66.
List-2

MODULE Singly-LinkedDECLARE

TYPE

list_handle_t; list_handle_t
list_create();
67.

list_destroy(list_handle_t this);

BOOL

list_append(list_handle_t this, ANY data);

68.

ANY list_getFirst(list_handle_t this);

69.

ANY list_getNext(list_handle_t this);

70.

BOOL list_isEmpty(list_handle_t this);

71.

END Singly-Linked-List-2;

You use DECLARE TYPE to introduce a new type listhandlej which represents
your list handle. We do not specify, how this handle is actually represented or even
implemented. You also hide the implementation details of this type in your
implementation file. Note the difference to the previous version where you just hide
functions or procedures, respectively. Now you also hide information for an user
defined data type called list_handle_t.
72.

You use list_create() to obtain a handle to a new thus empty list. Every other
procedure now contains the special parameter this which just identifies the list in
question. All procedures now operate on this handle rather than a module global list.
Now you might say, that you can create list objects. Each such object can be uniquely
identified by its handle and only those methods are applicable which are defined to
operate on this handle.
73.

74.

3.4 Modular Programming Problems:

The previous section shows, that you already program with some objectoriented concepts in mind. However, the example implies some problems which we
will outline now.
75.

76.

Explicit Creation and Destruction:

In the example every time you want to use a list, you explicitly have to declare
a handle and perform a call to list_create() to obtain a valid one.
77.

After the use of the list you must explicitly call list_destroy() with the handle
of the list you want to be destroyed. If you want to use a list within a procedure, say,
foo() you use the following code frame:
78.

79.

PROCEDURE foo() BEGIN


80. list_handle_t myList;
81. myList <- list_create();
82. /* Do something with myList */
83. list_destroy(myList

); END
84.
Let's compare the list with other data types, for example an integer. Integers are
declared within a particular scope (for example within a procedure). Once you've
defined them, you can'use them. Once you leave the scope (for example the procedure
where the integer was defined) the integer is lost, it is automatically created and
destroyed. Some compilers even initialize newly created integers to a specific value,
typically 0 (zero).
Where is the difference to list " "objects"? The lifetime of a list is also defined
by its scope, hence, it must be created once the scope is entered and destroyed once it
is left. On creation time a list should be initialized to be empty. Therefore we would
like to be able to define a list similar to" the definition of an integer. A code frame for
this would look like this:
86.
PROCEDURE foo() BEGIN
85.

87.

list_handle_t myList; /* List is created and initialized


*/ i /* Do something with the myList */

88.

END /* myList is destroyed */

The advantage is, that now the compiler takes care of calling initialization and
termination procedures as appropriate. For example, this ensures that the list is
correctly deleted, returning resources to the program.
89.

90.

Decoupled Data and Operations:

Decoupling of data and operations leads usually to a structure based on the


operations rather than the data: Modules group common operations (such as those
list_...() operations) together. You then use these operations by providing explicitly the
data to them on which they should operate. The resulting module structure is therefore
oriented on the operations rather than the actual data. One could say that the defined
operations specify the data to be used.
91.

In object-orientation, structure is organized by the data. You choose the data


representations which best fit your requirements. Consequently, your programs get
structured by the data rather than operations. Thus, it is exactly the other way around:
Data specifies valid operations. Now modules group data representations together.
92.

93.

Missing Type Safety:

In our list example we have to use the special type ANY to allow the list to
carry any data we like. This implies, that the compiler cannot guarantee for type
safety. Consider the following example which the compiler cannot check for
correctness:
94.

95.

PROCEDURE foo() BEGIN SomeDataType


datal; SomeOtherType data2;
list_handle_t myList; myList <list_create(); list_append(myList,
datal); list_append(myList, data2); /*
Oops */

96. iist_destroy(myList);
97.

END

It is in your responsibility to ensure that your list is used consistently. A


possible solution is to additionally add information about the type to each list element.
However, this implies more overhead and does not prevent you from knowing what
you are doing.
98.

What we would like to have is a mechanism which allows us to specify on which


data type the list should be defined.
99.

100. The overall function of the list is always the same, whether we store apples,

numbers, cars or even lists. Therefore it would be nice to declare a new list with
something like:
101. list_handle_t<Apple> listl; /* a list of apples */
102. list_handle_t<Car> list2; /* a list of cars */
The corresponding list routines should then automatically return the correct
data types. The compiler should be able to check for type consistency.
103.

Strategies and Representation:


105.
The list example implies operations to traverse through the list. Typically a
cursor is used for that purpose which points to the current element. This implies
atraversing strategy which defines the order in which the elements of the data structure
are to be visited. For a simple data structure like the singly linked list one can think of
only one traversing strategy. Starting with the leftmost element one successively visits
the right neighbours until one reaches the last element. However, more complex data
structures such as trees can be traversed using different strategies. Even worse,
sometimes traversing strategies depend on the particular context in which a data
structure is used. Consequently, it makes sense to separate the actual representation or
shape of the data structure from its traversing strategy. What we have shown with the
traversing strategy applies to other strategies as well. For example insertion might be
done such that an order over the elements is achieved or not.

104.

106.

Object-Oriented Programming

Object-oriented programming solves some of the problems just mentioned,


in contrast to the other techniques, we now have a web of interacting objects, each
house-keeping its own state (Fig. 3.8).
107.

3.

4.

5.
6.

Fig. 3.8 Object-oriented programming. Objects of the program


interact by sending messages to each other.

Consider the multiple lists example again. The problem here with
modular programming is, that you must explicitly create and destroy your list
handles. Then you use the procedures of the module to modify each of your
handles.
108.

In contrast to that, in object-oriented programming we would have as many


list objects as needed. Instead of calling a procedure which we must provide with the
correct list handle, we would directly send a message to the list object in question.
Roughly speaking, each object implements its own module allowing for example
many lists to coexist.
109.

Each object is responsible to initialize and destroy itself correctly.


Consequently, there is no longer the need to explicitly call a creation or termination
procedure. You might ask: So what? Isn't this just a more fancier modular
programming technique? You were right, if this would be all about object-orientation.
Fortunately, it is not. Beginning with the next chapters.additional features of objectorientation are introduced which makes object-oriented programming to a new
programming technique.
110.

Exercises:

111.

1.
The list examples include the special type ANY to allow a list to carry
data
of
any
type. Suppose you want to write a module for a specialized list of integers which
provides type checking. All you have is the interface definition of module SinglyLinked-List-2.
(a) How does the interface definition for a module Integer-List look like?
(b) Discuss the problems which are introduced with using type ANY for list
elements in module Singly-Linked-List-2.
(c) What are possible solutions to these problems?
112.

2.
What are the main conceptual differences between object-oriented
programming
and the other programming techniques?

113.

114.3. If you are familiar with a modular programming language try to implement

module Singly-Linked-List-2. Subsequently, implement a list of integers and a list


of integer lists with help of this module.

115.

3.5 OOPs and Its Concepts in Java:


116.

e-Prior

Index

Next->

117. Brief Introduction to OOP:


118. Object Oriented Programming or OOP is the technique to create programs based

on the real world. Unlike procedural programming, here in the OOP programming
model programs are organized around objects and data rather than actions and logic.
Objects represent some concepts or things and like any other objects in the real
Objects in programming language have certain behavior, properties, type, and identity.
In OOP based language the principal aim is to find out the objects to manipulate andtheir relation between each other. OOP offers greater flexibility and compatibility and
is popular in developing larger application.

Another important work in OOP is to classify objects into different types


according to their properties and behavior. So OOP based software application
development includes the analysis of the problem, preparing a solution, coding and
finally its maintenance.
119.

Java is a object oriented programming and to understand the functionality of


OOP in Java, we first need to understand several fundamentals related to objects.
These include class, method, inheritance, encapsulation, abstraction, polymorphism
etc.
120.

Class - It is the central point of OOP and that contains data and codes with
behavior. In Java everything happens within class and it describes a set of objects with
common behavior. The class definition describes all the properties, behavior, and
identity of objects present within that class. As far as types of classes are concerned,
there are predefined classes in languages like C+ + and Pascal. But in Java one can
define his/her own types with data and code.
121.

Object - Objects are the basic unit of object orientation with behavior, identity.
As we mentioned above, these are part of a class but are not the same. An object is
expressed by the variable and methods within the objects. Again these variables and
methods are distinguished from each other as instant variables, instant methods and
class variable and class methods.
122.

Methods - We know that a class can define both attributes and behaviors.
Again attributes are defined by variables and behaviors are represented by methods. In
other words, methods define the abilities of an object.
123.

Inheritance - This is the mechanism of organizing and structuring software


program. Though objects are distinguished from each other by some additional
features but there are objects that share certain things common. In object oriented
programming classes can inherit some common behavior and state from others.
Inheritance in OOP allows to define a general class and later to organize some other
classes simply adding some details with the old class definition.
124.

This saves work as the special class inherits all the properties of the old general
class and as a programmer you only require the new features. This helps in a better
data analysis, accurate coding and reduces development time.
125.

Abstraction - The process of abstraction in Java is used to hide certain details


and only show the essential features of the object. In other words, it deals with the
outside view of an object (interface).
126.

Encapsulation - This is an important programming concept that assists in


separating an object's state from its behavior. This helps in hiding an object's data
describing its state from any further modification by external component. In Java there
are four J different terms used for hiding data constructs and these are public, private, I
protected and package. As we know an object can associated with data with I predefined
classes and in any application an object can know about the data it needs J to know
about. So any unnecessary data are not required by an object can be hidden J by this
process. It can also be termed as information hiding that prohibits outsiders in seeing the
inside of an object in which abstraction is implemented.
127.

Polymorphism - It describes the ability of the object in belonging to different


types with specific behavior of each type. So by using this, one object can be treated
like another and in this way it can create and define multiple level of interface, Here
the programmers need not have to know the exact type of object in advance and this
is being implemented at runtime.
128.

129.

Classes and objects:

A class describes the data and the methods of its objects {also called
instances). Every object belongs to some class.
130.

An object contains data (instance variables) representing its state, and instance
methods, which are the things it can do. Each object has its own copies of the instance
variables.
131.

A class may also contain its own data (class variables) and class methods. The
keyword static denotes such data and methods. There is only one copy of each static
variable. Methods should be static if they don't access any instance variables or
instance methods.
132.

Instance variables and static variables may be given an initial value when they
are declared. Uninitialized numeric types are set to zero, boolean variables are set
tofalse, char variables are set to '\0', and object variables are set to null.
133.

An object "knows" what class it belongs to, and can use class data and class
methods, but a class does not "know" about its objects.
134.

Classes form a hierarchy (tree), with Object at the root.. Every class, except
Object, has one and only one immediate superclass, but that class has its own
immediate superclass, and so on all the way up to Object at the root, and all of these
are superclasses of the class. The keyword extends denotes the immediate superclass.
Class B should extend class A if and only if the things represented by class B really
are a more specific kind of A.
135.

A class contains one or more constructors for making new objects of that class.
If (and only if) the programmer does not write a constructor, Java provides adefault
constructor with no arguments.
136.

The purpose of a constructor is to create an object in a valid state. No other


work should be done in a constructor.
137.

When a constructor executes, the very first thing it does is call the constructor
for its superclass. You can write this constructor call explicitly, with super(...); as the
first thing done in the constructor, or you can let it implicitly call the default
constructor. However, you must explicitly, call a superclass constructor if the
superclass has no default constructor and all its explicitly defined constructors require
arguments.
138.

A constructor for a class can call another constructor for the same class by
putting this(...); as the first thing done in the constructor. This helps you avoid
repeating code.
139.

Variables to hold objects are declared just like variables to hold primitives,
with the syntax Type variable;. Declaring an object variable allocates space for
areference to an object, but does not create an object; the reference is null until some
object is assigned to the variable. An object can be created by calling the
corresponding constructor (using the keyword new).
140.

Classes inherit (have available) all the data and all the methods of their
superclasses, but do not inherit constructors.
141.

You can assign an object to a variable (that is, variable = object) if the variable
is declared to be of that class or any of its superclasses (thus, you can assign any
object to a variable of type Object). If you have an object in a more general variable or
expression (for example, a String value in an Objectvariable), it retains all its
additional variables and methods, but they are not accessible. To make them
accessible, you can cast the object to the correct type with the syntax (type)variable or
(type)(expression).
142.

Casting an object to a more general type is called upcasting, and is always legal.
Casting an object to a more specific type is called downcasting, and Java inserts a runtime check to ensure that the cast is legal. Casting does not affect what the object is, it
only changes what fields and methods are available on the object at the position the
cast occurs.
143.

144.

A class that is declared as final may not be extended by subclasses.

145. The instanceof operator tests whether its left operand (an object) is an instance

of its right operand (a class or interface). The result will be true if the right operand is
the class or any superclass of the object, or any interface that it implements. Welldesigned programs have very little need for the instanceof operator.
146. A Java source file may contain only one public class, though it may contain

additional non-public classes. The name of the file must be the same as the name of
the class, but with the .Java extension.
147. Classes should be as self-contained and independent as it is reasonable to make .

them. The interface (the fields and methods it makes available outside the class)
should be kept small.

148. An object is responsible for keeping itself in a valid state. Therefore, it should

limit write access to essential fields.


149. Access:

150. Variables and methods are accessed by name.


151. There are three dimensions to accessing names: namespaces, scope, and access

modifiers. Java uses six different namespaces: package names, type names, field
(variable) names, method names, local variable names (including parameters), and
labels. In addition, each declared enum has its own namespace. Identical names c
different types do not conflict; for example, a method may be named the same as a j
local variable. However, it's best to avoid reusing names in this manner.

152. The scope of a name is the part of a class in which it can be seen.

The scope of a method declared anywhere in a class is the entire class.

The scope of a variable declared within a class, but outside the methods of the
class, is the entire class.

The scope of a method's formal parameters is the entire method.

The scope of a variable declared in a block (indicated by braces, { }) extends from


the declaration to the closing brace. The braces enclosing a complete class
description do not indicate a block; everywhere else, they do.

The scope of a variable declared in the initialization part of a for loop is the entire
for loop.

Class variables and class methods (denoted by the keyword static) can be used
anywhere within the class.
153.

Instance variables and instance methods can only be


object
(instance)
of
correct
type,
using
the
syntax
object.variableName
155.
orobject.methodName(arguments). Within an instance method (but not in a
static method), the keyword this refers to the object currently executing the method.
this.variableName and this.methodName(arguments) may be abbreviated to
variableName and methodName(arguments), respectively.

154.

used
the

by

When an instance variable and a formal parameter have the same name, the
name refers to the formal parameter; prefix the name with this, to refer to the instance
variable.
156.

To refer to a static variable or static method in the same class, just use its
name. To refer to a class (static) name in a different class, use the
syntaxOtherClass.name.
157.

If you have an object obj of a class, you can also refer to static names in that
class with the (confusing) syntax obj .name, which is an abbreviation for obj
.getClass().name.
158.

You can refer to a name (class or instance) in another class if and only if you
have access privileges. Possible access privileges are:
159.

public: You can access it from anywhere.

protected: You can access it from any other class in the same directory (folder), or
from any subclass.

package (default, no keyword): You can access it from any other class in the same
directory.

private: You cannot access it from outside the class. "Private" means private to the
class; an object can access the private variables of another object of the same
class..

160. You can refer to a name in a class in another package in either of two ways-.

You can use the fully-qualified name, for instance Java.awt.Color.RED

You can import a specific class or (with *) all classes in a given package, then use the
name with or without the package qualification, for instanceColor.RED.

161.

Methods:

162.

A method is a named executable chunk of code.

163.

All executable statements must be in methods (or initializer blocks).

An initializer block is a block (zero or more statements enclosed in braces)


within a class but not within a method. Initializer blocks are copied into every
constructor. However, if preceded by the word static, an initializer block is executed
only once, when the class is first loaded.
164.

A method has a signature consisting of its name and the number and types of
its parameters (also called arguments or actual parameters). The parameters in the
declaration of the method are its formal parameters.
165.

A method has a return type, which is not part of its signature. If the return type
is other than void, then the method must return a value of the specified type.
166.

A method may have local variables (also called method variables). These
follow the scope rules, and are never available anywhere outside the method. The
concepts static, public, protected, package, and private do not apply to local variables.
Local variables have undefined values upon method entry. Formal parameters are a
kind of local variable, but have initial values as supplied by the corresponding actual
parameters.
167.

Every method must have a signature that is unique within its class. Methods in
other classes (even superclasses and subclasses) may have the same signature.
168.

An instance method is executed by sending a message to an object. The


message consists of: a reference to the object (typically its name), a dot, the name of
the method, and zero or more actual parameters enclosed in parentheses. The object
will respond by executing the corresponding method in the actual class of the object,
which may be different from the type of the variable holding the object.
169.

A class method is executed by sending a message to the class. The message


consists of: the name of the class, a dot, the name of the method, and zero or moreactual
parameters enclosed in parentheses. The class will respond by executing the ]
corresponding static method in that class.
170.

171. When a message is sent, and before the method executes, the values of the actual

parameters are copied into the corresponding formal parameters. Then the method body
executes. Then the return value replaces the message, and all local names are I
forgotten.
172.

Polymorphism:

173. The two kinds of polymorphism are overloading and overriding.

You can use the fully-qualified name, for instance Java.awt.Color.RED

You can import a specific class or (with *) all classes in a given package, then use the
name with or without the package qualification, for instanceColor.RED.

174.

Methods:

175.

A method is a named executable chunk of code.

176.

All executable statements must be in methods (or initializer blocks).

An initializer block is a block (zero or more statements enclosed in braces)


within a class but not within a method. Initializer blocks are copied into every
constructor. However, if preceded by the word static, an initializer block is executed
only once, when the class is first loaded.
177.

A method has a signature consisting of its name and the number and types of
its parameters (also called arguments or actual parameters). The parameters in the
declaration of the method are its formal parameters.
178.

A method has a return type, which is not part of its signature. If the return type
is other than void, then the method must return a value of the specified type.
179.

A method may have local variables (also called method variables). These
follow the scope rules, and are never available anywhere outside the method. The
concepts static, public, protected, package, and private do not apply to local variables.
Local variables have undefined values upon method entry. Formal parameters are a
kind of local variable, but have initial values as supplied by the corresponding actual
parameters.
180.

Every method must have a signature that is unique within its class. Methods in
other classes (even superclasses and subclasses) may have the same signature.
181.

An instance method is executed by sending a message to an object. The


message consists of: a reference to the object (typically its name), a dot, the name of
the method, and zero or more actual parameters enclosed in parentheses. The object
will respond by executing the corresponding method in the actual class of the object,
which may be different from the type of the variable holding the object.
182.

A class method is executed by sending a message to the class. The message


consists of: the name of the class, a dot, the name of the method, and zero or moreactual
parameters enclosed in parentheses. The class will respond by executing the 1
corresponding static method in that class.
183.

184. When a message is sent, and before the method executes, the values of the actual

parameters are copied into the corresponding formal parameters. Then the method body
executes. Then the return value replaces the message, and all local names are I
forgotten.
185. ' Polymorphism:
186. The two kinds of polymorphism are overloading and overriding.

Overloading occurs when a class declares two or more methods with the same
name but different signatures. When a message is sent to an object or class with
overloaded methods, the method with the best matching signature is the one that is
used ("invoked").
187.

If the message and the method have a different number of parameters, no match is
possible.

If the message and the method have exactly the same types of parameters, that is the
best possible match.

Messages with specific actual parameter types can invoke methods with more '
general formal parameter types. For example if the formal parameter type isObject,
an actual parameter of type String is acceptable (since a String value can
188.
be assigned to an Object variable). If the formal parameter is typedouble,
an actual parameter of type int can be used (for similar reasons).

189.

If there is no clear best match, Java reports a syntax error.

Overriding
occurs when a class declares a method with the same signature as that of
an inherited method. When a message is sent to the object (or class, if it's a class
method), the locally-defined method is the one that is used.
. .
190.

Overriding is commonly used to make methods more specific.

When a method name is overridden, you can still invoke the superclass' method
(from inside the class) with the syntax super.name(parameters).

Restrictions:
Although the return type is not part of the signature, an overriding
method must have the' same return type as the method it overrides.
191.

The overriding method cannot be more private than the method it


Overides (public > protected > package > private).
192.

The overriding method may not throw any exception types in addition
to those thrown by the method it overrides (although it may throw fewer
exception types).
193.

A class can declare a variable with the same name as an inherited variable, thus
"hiding" or shadowing the inherited version. (This is like overriding, but for
variables.)
194.

195.

Shadowing should be avoided.

When shadowing does happen,


you
can
access
the
superclass
name
by
either
the
syntax super.name or by casting the object to its superclass, with the syntax
((Superclass)object).name.
..

196.

197. Interfaces and Abstract Classes:


198. The purpose of interfaces and abstract methods is to ensure that any classes

derived from them will share the same set of methods.

An abstract method is a method that is declared but not defined. It is


declared with the keyword abstract, and has a header like any other method, but ends
with a semicolon instead of a method body.
199.

An abstract class is one that contains one or more abstract methods; it must
itself be declared with the abstract keyword. A class may be declared abstract even if
it does not contain any abstract methods. A non-abstract class is sometimes called a
concrete class.
200.

An abstract class cannot be instantiated; that is, no objects of that class can
be created. Instead, you can create subclasses that define (in the usual way) the
inherited abstract methods, and these subclasses can be instantiated.
201.

An interface is declared with the keyword interface instead of the keyword


class. An interface may contain only public abstract methods and definitions of
constants (that is, final variables). The keywords public and abstract before each
method are optional.
202.

A class may extend only one other class, but it may implement any number
of interfaces. The syntax is:
203.

class Class extends Superclassimplements Interface 1, Interface2, .... When a


class extends an interface, it may implement (define) some or all of the inherited
abstract methods. A class must itself be declared abstract if it inherits abstract methods
that it j does not define.
204.

A variable may be declared to have a type that is an abstract class or an


interface; any object whose type implements or extends the variable's type may be
assigned to that variable. The instanceof operator may take a class, abstract class, or
interface as its right operand.
205.

206.

Inner Classes:

An inner class is a class declared within another class. The four kinds of
inner class are: (1) member class, (2) static member class, (3) local inner class, and
(4) anonymous inner class. Unlike "outer" classes, the usual scope rules apply to inner
classes.
207.

208. A member class is defined at the top level of the class, along with fields and methods

It may have the same access modifiers as variables (public, protected.package, static
/
final), and is accessed in much the same way as variables of that class.

A static member class is defined like a member class, but with the keyword
static Despite its position inside another class, a static member class is actually an
"outer* 1 classit has no special access to names in its containing class. To refer to the
stat -inner class from a class outside the containing class, use
syntaxOuterClassName.InnerClassName. A static member class may contain statin
fields and methods.
209.

A local inner class is defined within a method, and the usual scope rules apply
to it 1 is only accessible within that method, therefore access restrictions
(public.protectaJ package) do not apply.
210.

However, because objects (and their methods) created from this class
may persist after the method returns, a local inner class may not refer to
parameters or non-final local variables of the method.
211.

An anonymous inner class is one that is declared and used to create one object
{typically as a parameter to a method), all within a single statement. The anonymous
inner class may either extend a class or implement an interface; the syntax is similar'
for both: new Super(parameters){methods} for classes andnew SuperQ{methods} for
interfaces, where Super is the name of the extended class or implemented interface,
parameters are the parameters to the constructor for that class or interface (must be
just () for interfaces), and methods override any inherited methods.
212.

The keyword static may not be used within any inner class except a static
member class.
213.

214.

Generics:

A generic or parameterized class is one which takes one or more other classes
as type parameters, using the syntaxClassName<Typel,Type2,...,TypeN>, where each
of the Types is itself the name of a class. Such a genericized name can be used
anywhere the "plain" name of the class can be used.
215.

Any object type may be used as a type parameter, but primitive types cannot
be used as type parameters.
216.

Within a parameterized class, a type parameter name may be used almost


anywhere the "plain" name of a class may be used. In particular, if Type is a type
parameter, then variables may be declared to be of type Type, and methods may have
parameters of type Type and return values of type Type. One restriction, however, is
that new Type values cannot be created.
217.

Parameterized classes were introduced in Java 5, and a number of pre-existing


classes were parameterized. For backward compatibility, you can create and use
objects of a parameterized class as if it were not parameterized (each type parameter
is assumed to be Object), but the compiler will produce warning messages.
218.

219.

Enums:

An enum is a class with a fixed number of instances that are defined within the
class itself. Enums are used where there are a small, unchanging, predefined set of
values, such as" the days of the week. They have a number of advantages over using
small integers: They provide better type safety, better readability, and they can be read
in, printed out, and compared with = = (as well as equals).
220.

Every enum extends and inherits from java.lang.Enum. All constructors


declared within an enum are automatically private. Since enums are classes, they can
have methods, as well as constructors with parameters.
221.

222. Every enum class creates a new namespace, so that the same name may be used

as a value in different enum classes.

223. 3.6

What is Java? .

Java (with a capital J) is a high-level, third generation programming language,


like C, Fortran, Smalltalk, Perl, and many others. You can use Java to. write computer
applications that crunch numbers, process words, play games, store data or do any of
the thousands of other things computer software can do.
224.

Compared to other programming languages, Java is most similar to C.


However although Java shares much of C's syntax, it is not C. Knowing how to
program in C or, better yet, C++, will certainly help you to learn Java more quickly,
but you don't need to know C to learn Java. Unlike C++ Java is not a superset of C. A
Java compiler won't compile C code, and most large C programs need to be changed
substantially before they can become Java programs.
225.

What's most special about Java in relation to other programming languages is


that it lets you write special programs called applets that can be downloaded from the
Internet and played safely within a web browser. Traditional computer programs have
far too much access to your system to be downloaded and executed willy-nilly.
Although you generally trust the maintainers of various ftp archives and bulletin
boards to do basic virus checking and not to post destructive software, a lot still slips
through the cracks. Even more dangerous software would be promulgated if any web
page you visited could run programs on your system. You have no way of checking
these programs for bugs or for out-and-out malicious behavior before downloading
and running them.
226.

Java solves this problem by severely restricting what an applet can do. A Java
applet cannot write to your hard disk without your permission. It cannot write to
arbitrary addresses in memory and thereby introduce a virus into your computer. It
should not crash your system.
227.

Java (with a capital J) is a platform for application development. A platform is a


loosely defined computer industry buzzword that typically means some combination
of hardware and system software that will mostly run all the same software. For
instance PowerMacs running Mac OS 9.2 would be one platform. DEC Alphas running
Windows NT would be another.
228.

There's another problem with distributing executable programs from web pages.
Computer programs are very closely tied to the specific hardware and operating
system they run. A Windows program will not run on a computer that only runs DOS.
A Mac application can't run on a Unix workstation. VMS code can't be executed on an
IBM mainframe, and so on. Therefore major commercial applications like Microsoft
Word or Netscape have to be written almost independently for all the different
platforms they run on. Netscape is one of the most cross-platform of major
applications, and it still only runs on a minority of platforms.
229.

230. Java solves the problem of platform-independence by using byte code. The Java

compiler does not produce native executable code for a particular machine like a C
compiler would. Instead it produces a special format called byte code. Java byte code J
written in hexadecimal, byte by byte, looks like this-.
231. CA FE BA BE 00 03 00 2D 00 3E 08 00 3B 08 00 01 08 00 20 08

This looks a lot like machine language, but unlike machine language Java
byte code is exactly the same on every platform. This byte code fragment means the
same thing on a Solaris workstation as it does on a Macintosh PowerBook. Java
programs that have been compiled into byte code still need an interpreter to execute
them on any given platform. The interpreter reads the byte code and translates it into
the native language of the host machine on the fly. The most common such interpreter
is Sun's program Java (with a little j). Since the byte code is completely platform
independent, only the interpreter and a few native libraries need to be ported to get
Java to run on a new computer or operating system. The rest of the runtime
environment including the compiler and most of the class libraries are written in Java.
232.

All these pieces, the javac compiler, the Java interpreter, the Java
programming language, and more are collectively referred to as Java.
233.

Java
was designed to make it much easier to write bug free code. According to Sun's
Bill Joy, shipping C code has, on average, one bug per 55 lines of code. The most
important part of helping programmers write bug-free code is keeping the language
& 6
simple.
234.

Java has the bare bones functionality needed to implement its rich feature set
It does not add lots of syntactic sugar or unnecessary features. Despite its simplicity
Java has considerably more functionality than c
236.
Because Java is simple, it is easy to read and write. Obfuscated Java isn't
nearly as common as obfuscated C. There aren't a lot of special cases or tricks that
will confuse beginners.
235.

About half of the bugs in C and C+ + programs are related to memory


allocation and deallocation. Therefore the second important addition Java makes to
providing bug-free code is automatic memory allocation and deallocation. The C
library memory allocation functions malloc() and free() are gone as are C+ + 's
destructors.
237.

Java is an excellent teaching language, and an excellent choice with which to


learn programming. The language is small so it's easy to become fluent. The language
is interpreted so the compile-run-link cycle is much shorter. The runtime environment
provides automatic memory allocation and garbage collection so there's less for the
programmer to think about. Java is object-oriented unlike Basic so the beginning
programmer doesn't have to unlearn bad programming habits when moving into real
world projects. Finally, it's very difficult (if not quite impossible) to write a Java
program that will crash your system, something that you can't say about any other
language.
238.

239. Java was designed from the ground up to allow for secure execution of code
across a . network, even when the source of that code was untrusted and possibly
malicious.

This required the elimination of many features of C and C++. Most notably
there are no pointers in Java. Java programs cannot access arbitrary addresses in
memory. All memory access is handled behind the scenes by the (presumably) trusted
runtime environment. Furthermore Java has strong typing. Variables must be declared,
and variables do not change types when you aren't looking.
240.

Casts are strictly limited to casts between types that make sense. Thus you
can cast an int to a long or a byte to a short but not a long to a boolean or an int to a
String.
241.

Java implements a robust exception handling mechanism to deal with both


expected and unexpected errors. The worst that an applet can do to a host system is
bring down the runtime environment. It cannot bring down the entire system.
242.

Most importantly Java applets can be executed in an environment that


prohibits them from introducing viruses, deleting or modifying files, or otherwise
destroying data and crashing the host computer. A Java enabled web browser checks
the byte codes of an applet to verify that it doesn't do anything nasty before it will run
the applet.
243.

However the biggest security problem is not hackers. It's not viruses. It's
not even insiders erasing their hard drives and quitting your company to go to work
for your competitors. No, the biggest security issue in computing today is bugs.
Regular, ordinary, non-malicious unintended bugs are responsible for more data loss
and lost productivity than all other factors combined. Java, by making it easier to
write bug-free code, substantially improves the security of all kinds of programs.
244.

245.

3.7 The Hello World Application:

246.

class HelloWorld {

247.

public static void main (String args[]) {

248.

System.out.println("Hello

World!"); I }
Hello World is very close to the simplest program imaginable. When you
successfully compile and run it, it prints the words "Hello World!" on your display.
Although it doesn't teach very much programming, it gives you a chance to learn the
mechanics of. typing and compiling code. The goal of this program is not to learn how
to print words to the terminal. It's to learn how to type, save and compile a program.
This is often a non-trivial procedure, and there are a lot of things that can go wrong
even if vour source code is correct.
249.

250.
To write the code you need a text editor. You can use any text editor like
Notepad,
Brief, emacs or vi. Personally I use BBEdit on the Mac and TextPad on Windows.
t

You should not use a word processor like Microsoft Word or WordPerfect since
these sa\ie tV\e.\r i\\es \T\ a proprietary format ar\d wot u\ pure ASCU text. If vou
absolutely must use one of these, be sure to tell it to save your files as pure text.
Generally this H^rac^i-tre v^w^aue. f&... ratrver tKarv Save. If v,au have an integrated
development I environment like BlueJ 1.0 or Borland JBuilder, that will include a text
editor you can 1 use to edit Java source code. It will probably change your words various
colors and styles for no apparent reason. Don't worry about this yet. As long as the text is
correct you'll be fine. ".
251.

When you've chosen your text editor, type or copy the above program into a new
file For now type it exactlv as it appears here.
252.

253. Like C and unlike Fortran, Java is case sensitive so System.out.println is not

the same as system .out.println. CLASS is not the same as class, and so on.

However, white space is not significant except inside string literals. The exact
number of spaces or tabs you use doesn't matter.
254.

Save this code in a file called HelloWorldJava. Use exactly that name
including case. Congratulations! You've written your first Java program.
255.

256.

Compiling and Running Hello World:

To make sure your Java environment is correctly configured, bring up a


command-line prompt and type
257.
258.

javac nofile.java

259.

If your computer responds with

260.

error: Can't read: nofile.java

261.

you're ready to begin. If, on the other hand, it responds

262.

javac: Command not found

or something similar, then you need make sure you have the Java environment
properly installed and your PATH configured.
263.

Assuming that Java is properly installed on your system there are three steps to
creating a Java program:
264.

1. writing the code


2. compiling the code
3. running the code
265.

Under Unix, compiling and running the code looks like this:

266.

$ javac HelloWorld.Java

267.

$ Java HelloWorld

268.

Hello World

269.

270.

Under Windows, it's similar. You just do this in a DOS shell.

271.

C:> javac HelloWorld.Java

272.

C:> Java HelloWorld

273.

Hello World

274. C:>

Notice that you use the Java extension when compiling a file, but you do not use
the .class extension when running a file.
275.
276.

For IDEs, consuft your product documentation.

277. ClASSPATH Problems-.


278.

If you get any message like this,

279.

$ Java HelloWorld

280.
281.

Can't find class HelloWorld


it probably means your CLASSPATH environment variable isn't properly set up.

Make
282.

sure it includes the current directory as well as the location where the classes.zip

file
283.

was installed. On Unix it should look something like this:

284.

CLASSPATH=.:/usr/local/java-l. 1/lib

285.

Under Windows it will probably look something like this

286.

C:\JDK\JAVA\CLASSES;c:\java\lib\classes.zip

287.

Under Unix you set CLASSPATH variables like this-.

288.

289.
290.

*
sh: % CLASSPATH=my_class_path
You'll probably want to add one of these lines to your .login or .cshrc file so it will

csh: % setenv CLASSPATH my_class_path

be
291.

automatically set every time.

292. Under Windows you set the CLASSPATH environment variable with a DOS

command
293. like
294. C:\> SET CLASSPATH=C:\JDKMAVA\CLASSES;C:\java\lib\Classes.zip
295. You can also add this to your autoexec.bat file (Windows ME and earlier) or set
it in the environment variables tab of the System control panel (Windows NT and
later) You should of course point it at whichever directories actually contain your
classes. The CLASSPATH variable is also important when you run Java applets, not
just when you compile them. It tells the web browser or applet viewer where it should
look to find the referenced .class files. If the CLASSPATH is set improperly, you'll
probably see messages like "Applet could not start."
296. If the CLASSPATH environment variable has not been set, and you do not specify
one on the command line, then Java sets the CLASSPATH to the default:
297.

Unix: .:$JAVA/classes:$JAVA/lib/classes.zip

298.

Windows: .:$JAVA\classes-.$JAVA\lib\classes.zip

299.

Mac: ./$JAVA:classes/$JAVA:lib:classes.zip

300.
301.
302.
303.
304.

305.
306.
307.
308.
.

Here . is the current directory and $JAVA is the main Java directory where the
different tools like javac were installed.
309. Fibonacci

Numbers: -c"Xass
Fibonacci .
310.

public static void main (String

argsU) { int low = 0; int high = 1;


while (high < 50) {
311. System.out.println(high)

; int temp = high; high


= high + low; low = temp;

312. }
313. Example
314. Addition
315. while loop
316.Relations
317. Variable declarations and assignments
318.

Some Notes on Java Programming Environments

ANYONE WHO IS LEARNING to program has to choose a programming


environment that makes it possible to create and to run programs. Programming
environments can be divided into two very different types: integrated development
environments and command-line environments. All programming environments for
Java require some text editing capability, a Java compiler, and a way to run applets
and standalone applications. An integrated development environment, or IDE, is a
graphical user interface program that integrates all these aspects of programming and
probably others (such as a debugger, a visual interface builder, and project
management). A command-line environment is just a collection of commands that
can be typed in to edit files, compile source code, and run programs.
319.

I have programmed using both IDEs and command-line environments,


and I have taught programming using both types of environments. Based on my
experience, I recommend a command line environment for beginning programmers.
320.

321.__________________________________________

IDEs can simplify the management of large numbers of files in a complex


project, but they are themselves complex programs that add another level of
complications to the already difficult task of learning the fundamentals of
programming. Certainly, a serious programmer should have some experience with
IDEs, but I think that it's an experience that can be picked up later. This is, of course,
just my opinion.
322.

In the rest of this appendix, I'll make a few comments on programming


environments. No matter which type of environment you prefer, there is no need to
pay for it, so I'll limit my comments to software that is available at no charge. Please
note that I am not an expert on Java programming environments. I am including this
appendix because people occasionally write to me for help or advice on the matter. In
general, however, I cannot answer questions about specific programming
environments.
323.

324.

The Basics from Sun (and Apple):

Java was developed at Sun Microsystems, Inc., and the primary source for
information about Java is Sun's Java Web site, http://java.sun.com/. At this site, you
can read documentation on-line and you can download documentation and software.
You should find some obvious links on the main page. (As of July 1, 2002, they are
labeled "Download Now," and a page with various downloads can be found
athttp://java.sun.com/j2se/l .4/download.html.)
325.

The documentation includes the Java API reference and the Java tutorial.
These are not really directed at beginning programmers, but you will need them if you
are going to be serious about Java programming.
326.

As I write this, the current version of Java on the Sun site is version 1.4. It is
available for the Windows, Linux, and Solaris operating systems. You want to
download the "J2SE 1.4 SDK." This is the "Java 2 Platform Standard Edition Version
1.4 Software Development Kit." This package includes a Java compiler, a Java virtual
machine that can be used to run Java programs, and all the standard Java packages.
You want the "SDK", not the "JRE". The JRE is the "Java Runtime Environment."
327.

It only includes the parts of the system that are need to run Java programs. It
does not have a compiler. You'll also see the "J2EE SDK." This is the "Enterprise
Edition," which includes additional packages that are not needed on a personal
computer. Don't forget to read and follow the installation instructions.
328.

329. This textbook is based on Java Version 1.3. If you already have version 1.3, you

don't need to download version 1.4 just to use this book.


330. The Sun site does not have a Java Software Development Kit for Macintosh.

However, the Macintosh OS X operating system already includes Java (Version 1.3 as
of July 2002). A Java programming environment is available on the Development CD
that comes with OS X. Unfortunately, Java 1.3 is not and will never be available for
Macintosh OS 9 and earlier. Java 1.1 can be used on older Macintosh systems, and if
you are working on one of those, you might want to use the previous edition of this
book. Information about Java on Macintosh can be found at
http://www.apple.com/java.

331. For Java programming, see http://developer.apple.com/java.


332. Integrated Development Environments:

It is really quite remarkable that there are sophisticated IDEs for Java
programming that are available for free. Here are the ones that 1 know about.
333.

Eclipse IDE An increasingly popular professional development environment


that supports Java development, among other things. Eclipse is itself written in
Java. It is available from http://www.eclipse.org/.

NetBeans IDE A pure Java IDE that should run on any system with Java 1.3 or
later. NetBeans is a free, "open source" program. It is essentially the open source
version of the next IDE. It can be downloaded from www.netbeans.org.

Sun ONE Studio 4 for Java, Community Edition, for Linux, Solaris, Windows 2000,
Windows NT, and Windows 98SE. This was formerly known as "Forte for Java",
and you might see it referred under that name. Again, it requires a lot of resources,
with a 256 MB memory recommendation. Main site currently at
http://www.sun.com/software/sundev/jde/index.html. It is available from there and
on the J2SE download page, http://java.sun.eom/j2se/l .4/download.html. The
Community Edition is the free version.

Borland J Builder Personal Edition, for Linux, Solaris, MacOS X, Windows 2000,
Windows XP, and Windows NT. Requires a lot of disk space and memory (256 MB
memory recommended). Company Web page at http://www.borland.com. JBuilder
site athttp://www.borland.com/jbuilder/index.html. The Personal Edition, which is
free, has more than enough features for most programmers.
334.

Mac OS X Project Builder comes as a standard part of Mac OS X (on the


Developer CD). It supports Java as well as some other programming languages.

BlueJ is a Java IDE written in Java that is meant particularly for educational use. It
is available from http://www.bluej.org/.

JCreator, for Windows. I haven't tried it, but it looks like a nice lighter-weight IDE
that works on top of Sun's SDK. It was recommended to me by a reader. There is a
free version, as well as a shareware version. It is available at
http://www.jcreator.com.

There are other products similar to JCreator, for Windows and for other
operating systems, and you might want to look around if you want some of the
convenience of an IDE without all the complexity.
335.

If you want to use any of the sample source code from this book in any of these
environments, you will have to figure out how to get the code into the environment. In
general, IDEs work with "projects". A project contains the all the source code files
needed in the project as well as other information. All this is stored in a project
directory. To use a source code file from outside the project, you have to "import" it in
some way. Usually, you have to copy the file into the project directory or into a source
code directory inside the project directory. In addition to this, you have to use an "Add
File" command in the IDE to.tell it that the file is part of the project.
336.

Details vary from one IDE to another, if all else falls, try using a "New File"
command to create an empty window in the IDE, and then copy-and-paste the source
code from a web browser window into the IDE's window.
337.

Text Editors:

338.

If you decide to use a command-line environment for programming, make


sure that you have a good text editor. A programmer's text editor is a very different
thing from a word processor. Most important, it saves your work in plain text files and
it doesn't insert extra carriage returns beyond the ones you actually type. A good
programmer's text editor will do a lot more than this. Here are some features to look
for:
339.

Syntax coloring. Shows comments, strings, keywords, etc., in different colors to


make the program easier to read and to help you find certain kinds of errors.

Function menu. A pop-up menu that lists the functions in your source code.
Selecting a function from this will take you directly to that function in the code.

Auto-indentation. When you indent one line, the editor will indent following lines
to match, since that's what you want more often than not when you are typing a
program.

Parenthesis matching. When you type a closing parenthesis the cursor jumps back to
the matching parenthesis momentarily so you can see where it is. ] Alternatively,
there might be a command that will hilite all the text between matching parentheses.
The same thing works for brackets and braces.

Indent Block and Unindent Block commands. These commands apply to a hilited
block of text. They will insert or remove spaces at the beginning of each line to |
increase or decrease the indentation level of that block of text. When you make j
changes in your program, these commands can help you keep the indentation in line
with the structure of the program.

Control of tabs. My advice is, don't use tab characters for indentation. A good I
editor can be configured to insert multiple space characters when you press the tab
key.

There are many free text editors that have some or all of these features. Since
you are I using Java, you should certainly consider jedit, a programmer's text editor
writte- I entirely in Java. It requires Java 1.3 or better. It has many features listed
above, ar: there are plug-ins available to add additional features. Since it is written in
pure Jav: J it can be used on any operating system that supports Java 1.3. In addition to
being i nice text editor, it shows what can be done with the Swing GUI. Jedit is free and
caa be downloaded from http:/Avww.jedit.org.
341. In my own work on Macintosh, 1 have used BBEdit for Macintosh from Bare
Bonesl Software (http://www.barebones.com/). BBEdit is not free, but there is a free
versioJ called BBEdit Lite. On Linux, I generally use nedit. It has all the above featureJ
except a function menu. If you are using Linux, it is likely that nedit is included your
distribution, although it may not have been installed by default.
340.

It can be downloaded from http://www.nedit.org/ and is available for many UNIX


platforms in addition to Linux. Features such as syntax coloring and auto-indentation .
are not turned on by default. You can configure them in the Options menu. Use the
"Save Options" command to make the configuration permanent. Of course, as
alternatives to nedit, the Gnome and KDE desktops for Linux have their own text
editors.
342.

343. Since I have very little experience with Windows, I don't have a recommendation

for a programmer's editor for Windows, other than jedit.


344. Using the fova SDK:

345. If you have installed Sun's Software Development Kit for Java, you can use the

commands "javac", "Java", and "appletviewer" for compiling and running Java
programs and applets. These commands must be on the "path" where the operating
system searches for commands. (See the installation instructions on Sun's Java web
site.) the rest of this appendix contains some basic instructions for using these
commands with this textbook.
346. I suggest that you make a directory to hold your Java programs. (You might want

to have a different subdirectory for each program that you write.) Create your program
with a text editor, or copy the program you want to compile into your program
directory. If the program needs any extra files, don't forget to get them as well. For
example, most of the programs in the early chapters of this textbook require the file
TextlO.java. You should copy this file into the same directory with the main program
file that uses it. (Actually, you only need the compiled file, TextlO.class, to be in the
same directory as your program. So, once you have compiled TextlO.java, you can
just copy the class file to any directories where you need it.)
If you have downloaded a copy of this textbook, you can simply copy the
files you need from the source directory that is part of the download. If you haven't
downloaded the textbook, you can open the source file in a Web browser and the use
the Web browser's "Save" command to save a copy of the file.
347.

Another way to get Java source code off a Web browser page is to hilite
the code on the page, use the browser's "Copy" command to place the code on the
Clipboard, and then "Paste" the code into your text editor. You can use this last
method when you want to get a segment of code out of the middle of a Web page.
348.

To use the SDK, you will have to work in a command window, using a
command-line interface. In Windows, this means a DOS window. In Linux/UNIX, it
means an "xterm" or "console" or "terminal" window. Open a command Window and
change to the directory that contains your Java source code files. Use the "javac"
command for
349.

350.

COTOJ&\\wv \ava sowce code R\es. ^o* raampk, vo corsvpWe ScmTC&eue.ja-va. use

xYie

351.

command

352.

javac SourceFile.Java

You must be working in the directory that contains the file. If the source
code file does not contain any syntax errors, this command will produce one or more
compiled class files.
353.

If the compiler finds any syntax errors, it will list them. Note that not every
message from the javac compiler is an error. In some cases, it generates "warnings" that
will not stop it from compiling the program. If the compiler finds errors in the program,
you can edit the source code file and try to compile it again. Note that you can keep the
source code file open in a text editor in one window while you compile the program in
the command window. Then, it's easy to go back to the editor to edit the file. However,
when you do this, don't forget to save the modifications that you make to the file before
you try to compile it again! (Some text editors can be configured to . issue the compiler
command for you, so you don't even have to leave the text editor to run the compiler.)
354.

If your program contains more .nan a few errors, most of them will scroll
out of the window before you see them. In Linux and UNIX, a command window
usually has a scroll bar that you can use to review the errors. In Windows 2000/NT/XP
(but not Windows 95/98), you can save the errors in a file which you can view later in
a text editor. The command in Windows is
355.

356.

javac SourceFile.java >& errors.txt

The ">& errors.txt" redirects the output from the compiler to the file,
instead of to the DOS window. For Windows 95/98 I've written a little Java program
that will let you do much the same thing. See the source code for that program,
cef.java, for instructions.
357.

It is possible to compile all the Java files in a directory at one time. Use the
command "javac *.java".
358.

(By the way, all these compilation commands only work if the classes you
are compiling are in the "default package". This means that they will work for any
example from this textbook. But if you start defining classes in other packages, the
source files must be in subdirectories with corresponding names. For example, if a
class is in the package named utilities.drawing then the source code file should be in a
directory named drawing, which is in a directory named utilities, which is in the toplevel program directory.
359.

You should work in the top-level directory and compile the source code file with a
command such as javac utilities/drawing/sourcefile.java on Linux/UNIX or javac
utilities\drawing\sourcefile.java on Windows. If you don't do it like this, the compiler
might not be able to find other classes that your class uses.) Once you have your
'
compiled class files, you are ready to run your application or applet. If you are
361.
running a stand-alone application one that has amain() routine you can
use the "java" command from the SDK to run the application. If the class file that
contains the main() routine is namedMain.class, then you can run the program with
the command:
360.

362.

java Main

Note that this command uses the name of the class, "Main", not the full name
of the : class file, "Main.class". This command assumes that the file "Main.class" is in
the current directory, and that any other class files used by the main program are also in
that directory.
363.

You do not need the Java source code files to run the program, only the
compiled class files. (Again, all this assumes that the classes you are working with are
in the "default package". Classes in other packages should be in subdirectories.)
364.

If your program is an applet, then you need an HTML file to run it. See Section
6.2 for information about how to write an HTML file that includes an applet. As an
example, the following code could be used in an HTML.file to run the applet
"MyApplet.class":
365.

366.

Opplet code="MyApplet.class" width=300 height=200>

367.

</applet>

The "appletviewer" command from the SDK can then be used to view the
applet. If the file name is test.html, use the command
368.

369.

appletviewer test.html

This will only show the applet. It will ignore any text or images in the HTML
file. In fact, all you really need in the HTML file is a single applet tag, like the example
shown above. The applet will be run in a resizable window, but you should remember
that many of the applet examples in this textbook assume that the applet will not be
resized. Note also that your applet can use standard output, System.out, to write
messages to the command window. This can be useful for debugging your applet.
370.

You can use the appletviewer command on any file, or even on a web page
address. It will find all the applet tags in the file, and will open a window for each
applet. If you are using a Web browser that does not support Java 2, you could use
appletviewer to see the applets in this book. For example, to see the applets in Section
6.1, use the command
371.

372.

appletviewer http://math.hws.edu/javanotes/c6/sl.html

to view the applets directly off the web. Or, if you have downloaded the
textbook, you can change to the directory c6 and use the commandappletviewer
si.html to see the applets. Of course, it's also possible to view your own applets in a
Web browser. Just open the html file that contains the applet tag for your applet. One
problem with this is that if you make changes to the applet, you might have to actually
quit the browser and restart it in order to get the changes to take effect. The browser's
Reload command might not cause the modified applet to be loaded.
373.

374.

3.8 ISO-OSI 7-Laver Network Architecture:

375. This lecture introduces the ISO-OSI layered architecture of Networks.

According to the ISO standards, networks have been divided into 7 layers depending
on the complexity of the fucntionality each of these layers provide. The detailed
description of each of these layers is given in the notes below. We will first list the
layers as defined by the standard in the increasing order of function complexity:
1.Physical Layer
2.Data Link Layer
3.Network La'yer
4. Transport Layer

5. Session Layer
6. Presentation Layer
7. Application Layer
Physical Layer:

5.

This layer is the lowest layer in the OSI model. It helps in the transmission of
data between two machines that are communicating through a physical medium,
which can be optical fibres,copper wire or wireless etc. The following are the main
functions of the physical layer6.

7.

8. 1. Hardware Specification: The details of the physical cables, network interface

cards, wireless radios, etc are a part of this layer.

9.

10. 2. Encoding and Signalling: How are the bits encoded in the medium is also

decided by this layer. For example, on the coppar wire medium, we can use
differnet voltage levels for a certain time interval to represent '0' and''!'. We may
use +5mV for insec to represent T and -5mV for lnsec to represent '0'. All the
issues of modulation is dealt with in this layer, eg, we may use Binary phase shift
keying for the representation of '1' and '0' rather than using different volatage
levels if we have to transfer in RF waves.

11.

3. Data Transmission and Reception: The transfer of each bit of data is the
responsibility of this layer. This layer assures the transmissoin of each bit with
ahigh probability. The transmission of the bits is not completely reliable as their is
no error correction in this layer.
4. Topology and Network Design: The network design is the integral part of the physical
layer. Which part of the network is the router going to be placed, where the switches
will be used, where we will put the hubs, how many machines is each switch going to
handle, what server is going to be placed where, and many such concerns are to be
taken care of by the physical layer. The variosu kinds of netopdlogies that we decide
to use may be ring, bus, star or a hybrid of these ' topologies depending on our
requirements.

12.

13.

Data Link layer:

This layer provides reliable transmission of a packet by using the services of


the physical layer which transmits bits over the medium in an unreliable fashion. This
layer is concerned with:
14.

15. 1. Framing: Breaking input data into frames (typically a few hundred bytes) and

caring about the frame boundaries and the size of each frame.

2. Acknowledgment: Sent by the receiving end to inform the source that the frame was
received without any error.
3. Sequence Numbering: To acknowledge which frame was received.
4. Error Detection: The frames may be damaged, lost or duplicated leading to errors.
The error control is on link to link basis.
5. Retransmission: The packet is retransmitted if the source fails to receive
acknowledgment.
6. Flow Control: Necessary for a fast transmitter to keep pace with a slow receiver.

16.

17.

Network Layer: '

18.

Its basic functions are routing and congestion control.

Routing: This deals with determining how packets will be routed


(transferred) from source to destination. It can be of three types:
19.

20.

Static: Routes are based on static tables that are "wired into" the network and are
21. rarely changed.

22.

Dynamic: All packets of one application can follow different routes


depending
upon the topology of the network, the shortest path and the current network
load.

23.

Semi-Dynamic: A route is chosen at the start of each conversation and then


all
the packets of the application follow the same route.

24.

25.

26.

Routing:

27.

The services provided by the network can be of two types:

Connection less service: Each packet of an application is treated as an


independent entity. On each packet of the application the destination address is
provided and the packet is routed.

Connection oriented service: Here, first a connection is established and then all
packets of the application follow the same route. To understand the above concept,
we can.also draw an analogy from the real life. Connection oriented service is
modeled after the telephone system. All voice packets go on the same path after
the connection is established till the connection is hung up. It acts like a tube ; the
sender pushes the objects in at one end and the receiver takes them out in the same
order at the other end. Connection less service is modeled after the postal system.
Each letter carries the destination address and is routed independent of all the
others. Here, it is possible that the letter sent first is delayed so that the second
letter reaches the destination before the first letter.

Congestion Control: A router can be connected to 4-5 networks. If all the


networks send packet at the same time with maximum rate possible then the router
may not be able to handle all the packets and may drop some/all packets.
28.

29.
WITH IAVA

. RSn-MIIMRAI

OUIFrTnRIFMTFn PRnr.RAMMIMr,

In this context the dropping of the packets should be minimized and the source
whose packet was dropped should be informed. The control of such congestion is also
a function of the network layer. Other issues related with this layer are transmitting
time, delays, jittering.
30.

Internetworking: Internetworks are multiple networks that are connected in such


a way that they act as one large network, connecting multiple office or department i
networks. Internetworks are connected by networking hardware such as routers, switches,
and bridges. Internetworking is a solution born of three networking problems: isolated
LANs, duplication of resources, and the lack of a centralized network management
system. With connected LANs, companies no longer have to duplicate programs or
resources on each network. This in turn gives way to managing the network from one
central location instead of trying to manage each separate LAN. We should be able to
transmit any packet from one network to any other network even if they follow different
protocols or use different addressing modes.
31.

32.

33.

Fig. 3.13 Inter-Networking

34. Network Layer does not guarantee that the packet will reach its intended

destination, j There are no reliability guarantees.


35. Transport Layer:
36. Its functions are:

37. Multiplexing / Demultiplexing: Normally the transport layer will create distinct

network connection for each transport connection required by the session layer
The-transport layer may either create multiple network connections (to improve
throughput) or it may multiplex several transport connections onto the same
network connection (because creating and maintaining networks may bej
exDensiveL In the latter case. demultiDlexine will be reauired at the receivine end. I

38. A point to note here is that communication is always carried out between two

processes and not between two machines. This is also known as process-toprocess communication.

39. Fragmentation and Re-assembly: The data accepted by the transport layer from

the session layer is split up into smaller units (fragmentation) if needed and then
passed to the network layer. Correspondingly, the data provided by the network
layer to the transport layer on the receiving side is re-assembled.

40.

Types of service: The transport layer also decides the type of service that should
be provided to the session layer. The service may be perfectly reliable, or may be
reliable within certain tolerances or may not be reliable at all. The message may or
may not be received in the order in which it was sent. The decision regarding the
type of service to be provided is taken at the time when the connection is
established.

Error Control: If reliable service is provided then error detection and error
recovery operations are also performed. It provides error control mechanism on
end to end basis.

Flow Control: A fast host cannot keep pace with a slow one. Hence, this is a
mechanism to regulate the (low of information.

41.

Connection Establishment / Release: The transport layer also establishes and


releases the connection across the network. This requires some sort of naming
mechanism so that a process on one machine can indicate with whom it wants to
communicate.

42.

Session Layer:

It deals with the concept of Sessions i.e. when a user logins to a remote server he
should be authenticated before getting access to the files and application programs.
Another job of session layer is to establish and maintain sessions. If during the transfer of
data between two machines the session breaks down, it is the session layer which reestablishes the connection. It also ensures that the data transfer starts from where it breaks
keeping it transparent to the end user. e.g. In case of a session with a database server, this
layer introduces check points at various places so that in case the connection is broken
and reestablished, the transition running on the database is not lost even if the user has not
committed. This activity is called Synchronization. Another function of this layer is
Dialogue Control which determines whose turn is it to speak in a session. It is useful in
video conferencing. Presentation Layer
43.

This layer is concerned with the syntax and semantics of the information
transmitted. In order to make it possible for computers with different data
representations to communicate data structures to be exchanged can be defined in
abstract way along with standard encoding. It also manages these abstract data structures
and allows higher level of data structures to be defined an exchange. It encodes the data
in standard agreed way (network format). Suppose there are two machines A and B one
follows 'Big Indian' and other 'Little Indian' for data -representation. This layer ensures
that the data transmitted by one gets converted in the form compatible to other machine.
This layer is concerned with the syntax and semantics of the information transmitted. In
order to make it possible for computers with different data representations to
communicate data structures to be exchanged can be defined j in abstract way along
with standard encoding. It also manages these abstract data structures and allows higher
level of data structures to be defined an exchange. Other I functions include
compression, encryption etc.
44.

45.__________________
__________________^

46. Application Layer:

The seventh layer contains the application protocols with which the user gains
access to the network. The choice of which specific protocols and their associated
functions are to be used at the application level is up to the individual user. Thus the
boundary between the presentation layer and the application layer represents a
separation of the protocols imposed by the network designers from those being
selected and implemented by the network users. For example commonly used
protocols are HTTP(for web browsing), FTP(for file transfer) etc.
47.

48.

Network Layers as in Practice:

In most of the networks today, we do not follow the OSI model of seven layers.
What is actually implemented is as follows. The functionality of Application layer and
Presentation layer is merged into one and is called as the Application Layer.
Functionalities of Session Layer is not implemented in most networks today. Also, the
Data Link layer is split theoretically into MAC (Medium Access Control) Layer and LLC
(Link Layer Control). But again in practice, the LLC layer is not implemented by most
networks. So as of today, the network architecture is of 5 layers only.
49.

50.

51.

Fig. 3.14 Network Layers in Internet

Today Some Related Links on OSI Model and TCP Model


http://en.wikipedia.org/wiki/OSI_model

52. 2.

Depending on radius

Thin optical fiber

Thick optical fiber


53. 3.

Depending on light source

LED (for low bandwidth)

Injection lased diode (for high bandwidth)

54.

Wireless Transmission:

1. Radio: Radio is a general term that is used for any kind of frequency. But higher
frequencies are usually termed as microwave and the lower frequency band comes
under radio frequency. There are many application of radio- For eg. cordless
keyboard, wireless LAN, wireless ethernet. but it is limited in range to only a few
hundred meters. Depending on frequency radio offers different bandwidths.
2. Terrestrial microwave: In terrestrial microwave two antennas are used for
communication. A focused beam emerges from an antenna and is received by the
other antenna, provided that antennas should be facing each other with no obstacle
in between. For this reason antennas are situated on high towers. Due to curvature
of earth terrestrial microwave can be used for long distance communication with
high bandwidth. Telecom department is also using this for long distance
communication. An advantage of wireless communication is that it is not required
to lay down wires in the city hence no permissions are required.
55. 3. Satellite communication: Satellite acts as a switch in sky. On earth VSATfVery

Small Aperture Terminal) are used to transmit and receive data from satellite.
Generally one station on earth transmits signal to satellite and it is received by
many stations on earth. Satellite communication is generally used in those places
where it is very difficult to obtain line of sight i.e. in highly irregular terrestrial
regions. In terms of noise wireless media is not as good as the wired media.
There are frequency band in wireless communication and two stations should not
be allowed to transmit simultaneously in a frequency band. The most promising
advantage of satellite is broadcasting. If satellites are used for point to point
communication then they are expensive as compared to wired media.

56.

57.

CSMA with Collision Avoidance:

We have observed that CSMA/CD would break down in wireless networks


because of hidden node and exposed nodes problems. We will have a quick recap of
these two problems through examples.
58.

59.

Hidden Node Problem:

In the case of wireless network it is possible that A is sending a message to


B, but C is out of its range and hence while "listening" on the network it will find the
network to be free and might try to send packets to B at the same time as A. So, there
will be a collision at B. The problem can be looked upon as if A and C are hidden
from each other. Hence it is called the "hidden node problem".
60.

61.

Exposed Node Problem:

If C is transmitting a message to D and B wants to transmit a message to A,


B will find the network to be busy as B hears C transmitting. Even if B would have
transmitted to A, it would not have been a problem at A or D. CSMA/CD would not
allow it to transmit message to A, while the two transmissions could have gone in
parallel. .
62.

63.

64.

Addressing hidden node problem (CSMA/CA):

Consider the figure above. Suppose A wants to send a packet to B. Then it


will First send a small packet to B called "Request to Send" (RTS). In response, B
sends a small packet to A called "Clear to Send" (CTS). Only after A receives a CTS, it
transmits the actual data. Now, any of the nodes which can hear either CTS or RTS
assume the network to be busy. Hence even if some other node which is out of range
of both A and B sends an RTS to C (which can hear at least one of the RTS or CTS
between A and B), C would not send a CTS to it and hence the communication would
not be established between C and D.
66.
One issue that needs to be addressed is how long the rest of the nodes should
wait I before they can transmit data over the network. The answer is that the RTS and
CTS I would carry some information about the size of the data that B intends to transfer,
j So, they can calculate time that would be required for the transmission to be over I and
assume the network to be free after that. Another interesting issue is what a I node
should do if it hears RTS but not a corresponding CTS.
65.

One possibility is that it assumes the recipient node has not responded and hence
no transmission is going on, but there is a catch in this. It is possible that the node hearing
RTS is just on the boundary of the node sending CTS. Hence, it does hear CTS . but the
signal is so deteriorated that it fails to recognize it as a CTS. Hence to be on the safer side,
a node will not start transmission if it hears either of an RTS or a CTS.
67.

T
he assumption made in this whole discussion is that if a node X can send packets to
a node Y, it can also receive a packet from Y, which is a fair enough assumption given
the fact that we are talking of a local network where standard instruments would be
used. If that is not the case additional complexities would get introduced in the
System.
/
68.

69.

Does CSMA/CD work universally in the wired networks ?

The problem of range is there in wired networks as well in the form of


deterioration of signals. Normally to counter this, we use repeaters, which can
regenerate the original signal from a deteriorated one. But does that mean that we can
build as long networks as we want with repeaters. The answer, unfortunately, is NO!
The reason is the beyond a certain length CSMA/CD will break down.
70.

The mechanism of collision detection which CSMA/CD follows is through


listening while talking. What this means is so long as a node is transmitting the packet,
it is listening on the cable. If the data it listens to is different from the data it is
transmitting it assumes a collision. Once it has stopped transmitting the packet, and
has not detected collision while transmission was going on, it assumes that the
transmission was successful. The problem arises when the distance between the two
nodes is too large. Suppose A wants to transmit some packet to B which is at a very
large distance from B. Data can travel on cable only at a finite speed (usually 2/3c, c
being the speed of light). So, it is possible that the packet has been transmitted by A
onto the cable but the first bit of the packet has not yet reached B. In that case, if a
collision occurs, A would be unaware of it occurring. Therefore there is problem in too
long a network.
71.

Let us try to parametrize the above problem. Suppose "t" is the time taken for
the node A to transmit the packet on the cable and "T" is the time , the packet takes to
reach from A to B. Suppose transmission at A starts at time to. In the worst case the
collision takes place just when the first packet is to reach B. Say it is at tO+T-e (e
being very small). Then the collision information will take T-e time to propagate back
to A. So, at tO+2(T-e) A should still be transmitting. Hence, for the correct detection
73. of collision (ignoring e)
72.

74.

>2T

t increases with the number of bits to be transferred and decreases with the rate
of transfer (bits per second). T increases with the distance between the nodes and
decreases with the speed of the signal (usually 2/3c). We need to either keep t large
enough or T as small. We do not want to live with lower rate of bit transfer and hence
slow networks. We can not do anything about the speed of the signal. So what we can
rely on is the minimum size of the packet and the distance between the two nodes.
75.

76. Therefore, we fix some minimum size of the packet and if the size is smaller than

that, we put in some extra bits to make it reach the minimum size. Accordingly we fix
the maximum distance between the nodes. Here too, there is a tradeoff to be made. We
do not want the minimum size of the packets to be too large since that wastes lots of
resources on cable. At the same time we do not want the distance between the nodes
to be too small. Typical minimum packet size is 64 bytes and the corresponding
distance is 2-5 kilometers.
77. Collision Free Protocols:
78. Although collisions do not occur with CSMA/CD once a station has unambigously

seized the channel, they can stil! occur during the contention period. These collisions
adversely affect the efficiency of transmission. Hence some protocols have been
developed which are contention free.
79. Bit-Map Method:

80. In this method, there N slots. If node 0 has a frame to send, it transmit a 1 bit

during the first slot. No other node is allowed to transmit during this period. Next node
1 gets a chance to transmit 1 bit if it has something to send, regardless of what node 0
had transmitted. This is done for all the nodes. In general node j may declare the fact
that it'has a frsme to send by.inserting a 1 into slot j. Hence after all nodes have
passed, each node has complete knowledge of who wants to send a frame. Now they
begin transmitting in numerical order. Since everyone knows who is transmitting and
when, there could never be any collision. The basic problem with this protocol is its
inefficiency during low load. If a node has to transmit and no other node needs to do
so, even then it has to wait for the bitmap to finish. Hence the bitmap will be repeated
over and over again if very few nodes want to send wasting valuable bandwidth.
81.

Binary Countdown:

In this protocol, a node which wants to signal that it has a frame to send
does so by writing its address into the header as a binary number. The arbitration is
such that as soon as a node sees that a higher bit position that is 0 in its address has
beer, overwritten with a 1, it gives up. The final result is the address of the node which
is allowed to send. After the node has transmitted the whole process is repeated all
over again. Given below is an example situation.
82.

83.

84. Node C having higher priority gets to transmit. The problem with this protocol is that j
the nodes with higher address always wins. Hence this creates a priority which is
______________________________________________________________________
highly unfair and hence undesirable. \
85.

86.

Limited Contention Protocols:

Both the type of protocols described above - Contention based and Contention
- free has their own problems.,Under conditions of light load, contention is preferable
due to its low delay. As the load increases, contention becomes increasingly less
attractive, because the overload associated with channel arbitration becomes greater,
just the reverse is true for contention - free protocols. At low load, they have high
delay, but as the load increases, the channel efficiency improves rather than getting
worse as it does for contention protocols.
87.

Obviously it would be better if one could combine the best properties of the
contention and contention - free protocols, that is, protocol which used contention at
low loads to provide low delay, but used a cotention-free technique at high load to
provide good channel efficiency. Such protocols do exist and are called Limited
contention protocols.
88.

It is obvious that the probablity of some station aquiring the channel could
only be increased by decreasing the amount of competition. The limited contention
protocols do exactly that. They first divide the stations up into (not necessarily disjoint)
groups. Only the members of group 0 are permitted to compete for slot 0. The
competition for aquiring the slot within a group is contention based. If one of the
members of that group succeeds, it aquires the channel and transmits a frame. If there
is collision or no node of a particular group wants to send then the members of the
next group compete for the next slot. The probablity of a particular node is set to a
particular value (optimum).
89.

90.

Adaptive Tree Walk Protocol:

The following is the method of adaptive tree protocol. Initially all the nodes are
allowed to try to aquire the channel. If it is able to aquire the channel, it sends its
frame. If there is collision then the nodes are divided into two equal groups and only
one of these groups compete for slot 1. If one of its member aquires the channel then
the next slot is reserved for the other group. On the other hand, if there is a collision
then that group is again subdivided and the same process is followed. This can be
better understood if the nodes are thought of as being organised in a binary tree as
shown in the following figure.
91.

92.

Many improvements could be made to the algorithm. For example,


consider the case of nodes G and H being the only ones wanting to transmit. At
slot 1 a collision will be detected and so 2 will be tried and it will be found to
be idle. Hence it is pointless to probe 3 and one should directly go to 6,7.
93.

The network layer is concerned with getting packets from the source all the
way to the destnation. The packets may require to make many hops at the intermediate
routers while reaching the destination. This is the lowest layer that deals with end to
end transmission. In order to achieve its goals, the network later must know about the
topology of the communication network. It must also take care to choose routes to
avoid overloading of some of the communication lines while leaving others idle. The
main functions performed by the network layer are as follows:
94.

Routing

Congestion Control

Internetwokring
Routing:

95.

Routing is the process of forwarding of a packet in a network so that it reaches


its intended destination. The main goals of routing are:
96.

1. Correctness: The routing should be done properly and correctly so that the packets
may reach their proper destination.
2. Simplicity: The routing should be done in a simple manner so that the overhead is
as low as possible. With increasing complexity of the routing algorithms the
overhead also increases.
3. Robustness: Once a major network becomes operative, it may be expected to run
continuously for years without any failures. The algorithms designed for routing
should be robust enough to handle hardware and software failures and should be
able to cope with changes in the topology and traffic without requiring all jobs in
all hosts to be aborted and the network rebooted every time some router goes
down.
4. Stability: The routing algorithms should be stable under all possible
circumstances.
97. 5. Fairness: Every node connected to the network should get a fair chance c
98.
transmitting their packets. This is generally done on a first come first

serve basis.
99.

6.
Optimality: The routing algorithms should be optimal in terms of
throughput
an:
minimizing mean packet delays. Here there is a trade-off and one has to choose
depending on his suitability.

100.

101.

Classification of Routing Algorithms:

102.

The routing algorithms may be classified as follows:

1. Adaptive Routing Algorithm: These algorithms change their routing


decisions to reflect changes in the topology and in traffic as well. These get their
routing information from adjacent routers or from all routers. The optimization
parameters are the distance, number of hops and estimated transit time. This can
be further classified as follows:

103.

1. Centralized: In this type some central node in the network gets entire
information about the network, topology, about the traffic and about other
nodes. This then transmits this information to the respective routers. The
advantage of this is that only one node is required to keep the information. The
disadvantage is that if the central node goes down the entire network is down,
i.e. single point of failure.
2. Isolated: In this method the node decides the routing without seeking
information from other nodes. The sending node does not know about the
status of a particular link. The disadvantage is that the packet may be send
through a congested route resulting in a delay. Some examples of this type of
algorithm for routing are:
104.

Hot Potato: When a packet comes to a node, it tries to get rid of it as fast
as it can, by putting it on the shortest output queue without regard to
where that link leads. A variation of this algorithm is to combine static
routing with the hot potato algorithm. When a packet arrives, the routing
algorithm takes into account both the static weights of the links and the
queue lengths.

Backward Learning: In this method the routing tables at each node gets
modified by information from the incoming packets. One way to
implement backward learning is to include the identity of the source node
in each packet, together with a hop counter that is incremented on each
hop. When a node receives a packet in a particular line, it notes down the
number of hops it has taken to reach it from the source node. If the
previous v'alue of hop count stored in the node is better than the current
one then nothing is done but if the current value is better then the value is
updated lor future use. The problem with this is that when the best route
goes down then it cannot recall the second best route to a particular node
Hence all the nodes have to forget the stored informations periodically and
start all over again.

\ 3. Distributed: In this the node receives information from its neighbouring


nodes and then takes the decision about which way to send the packet. The \
disadvantage is that if in between the the interval it receives information and \
sends the paket something changes then the packet may be delayed.

105.

106. 2. Nop-Adaptive Routing Algorithm: These algorithms do not base their routing

deacons qn measurements and estimates of the current traffic and topology.


Instead the route to be taken in going from one node to the other is computed in
advances off line, and downloaded to the routers when the network is booted. This
is also known as static routing. This can be further classified as:

1. Flooding: Flooding adapts the technique in which every incoming


packet is sent on every outgoing line except the one on which it arrived. One
problem with this method is that packets may go in a loop, As a result of this a
node may receive several copies of a particular packet which is undesirable.
Some techniques adapted to overcome these problems are as follows:

107.

Sequence Numbers: Every packet is given a sequence number. When a


node receives the packet it sees its source address and sequence number.
If the node finds that it has sent the same packet earlier then it will not
transmit the packet and will just discard it.

Hop Count: Every packet has a hop count associated with it. This is
decremented (or incremented) by one by each node which sees it. When
the hop count becomes zero(or a maximum possible value) the packet is
dropped.

Spanning Tree: The packet is sent only on those links that lead to the
destination by constructing a spanning tree routed at the source. This
avoids loops in transmission but is possible only when all the intermediate
nodes have knowledge of the network topology. Flooding is not practical
for general kinds of applications. But in cases where high degree of
robustness is desired such as in military applications, flooding is of great
help.

108. 2. Random Walk: In this method a packet is sent by the node to one of its

neighbours randomly. This algorithm is highly robust. When the network is


highly interconnected, this algorithm has the property of making excellent use
of alternative routes. It is usually implemented by sending the packet onto the
least queued link.

109. Delta Routing:


110. Delta routing is a hybrid of the centralized and isolated routing algorithms. Here

each node computes the cost of each line (i.e some functions of the delay, queue
length, utilization, bandwidth etc) and periodically sends a packet to the central node
giving it these values which then computes the k best paths from node i to nodej. Let
Cijl be the cost of the best i-j path, Cij2 the cost of the next best path and so on.If Cijn
- Cijl < delta, (Cijn - cost of n'th best i-j path, delta is some constant) then path n is
regarded equivalent to the best i-j path since their cost differ by so little. When delta
-> 0 this algorithm becomes centralized routing and when delta -> infinity all the paths
become equivalent.
111. Multipath Routing:
112. In the above algorithms it has been assumed that there is a single best path

between any pair of nodes and that all traffic between them should use it. In many
networks however there.are several paths between pairs of nodes that are almost
equally good. Sometimes in order to improve the performance multiple paths
between single pair of nodes are used. This technique is called multipath routing or
bifurcated routing. In J this each node maintains a table with one row for each
possible destination node.

A row gives the best, second best, third best, etc outgoing line for that
destination, together with a relative weight. Before forwarding a packet, the node
generates a random number and then chooses among the alternatives, using the
weights as probabilities. The tables are worked out manually and loaded into the
nodes before the network is brought up and not changed thereafter.
113.

114.

Hierarchical Routing:

In this method of routing the nodes are divided into regions based on
hierarchy. A particular node can communicate with nodes at the same hierarchial level
or the nodes at a lower level and directly under it. Here, the path from any source to a
destination is fixed and is exactly one if the heirarchy is a tree.
115.

116.

Transport Layer Protocol

117.

What is TCP?

TCP was specifically designed to provide a reliable end to end byte stream
over an unreliable internetwork. Each machine supporting TCP has a TCP transport
entity either a user process or part of the kernel that manages TCP streams and
interface to IP layer. A TCP entity accepts user data streams from local processes,
breaks them up into pieces not exceeding 64KB and sends each piece as a separate IP
datagram. Client Server mechanism is not necessary for TCP to behave properly.
118.

The IP layer gives no guarantee that datagram will be delivered properly, so it is


up to TCP to timeout and retransmit, if needed. Duplicate, lost and out of sequence
packets are handled using the sequence number, acknowledgements, retransmission,
timers, etc to provide a reliable service. Connection is a must for this service.Bit errors
are taken care of by the CRC checksum. One difference from usual sequence
numbering is that each byte is given a number instead of each packet. This is done so
that at the time of transmission in case of loss, data of many small packets can be
combined together to get a larger packet, and hence smaller overhead.
119.

TCP connection is a duplex connection. That means there is no difference


between two sides once the connection is established.
120.
121.

TCP Connection establishment;

The "three-way handshake" is the procedure used to establish a connection. This


procedure normally is initiated by one TCP and responded to by another TCP. The
procedure also works if two TCP simultaneously initiate the procedure. When
simultaneous attempt occurs, each TCP receives a "SYN" segment which carries no
acknowledgment after it has sent a "SYN". Of course, the arrival of an old duplicate
"SYN" segment can potentially make it appear, to the recipient, that a simultaneous
connection initiation is in progress. Proper use of "reset" segments can disambiguate
these cases.
122.

123. The three-way handshake reduces the possibility of false connections. It is the

implementation of a trade-off between memory and messages to provide information


for this checking.

124.CHAPTER 3____________________________________
125. The simplest three-way handshake is shown in figure below. The figures should be
interpreted in the following way. Each line is numbered for reference purposes. Right anOW5
(>) indicate departure of a TCP segment from TCP A to TCP B, or arrival of a segment at B
from A. Left arrows (<-), indicate the reverse. Ellipsis (...) indicates a segment which is still in
the network (delayed). TCP states represent the state AFTER the departure or arrival of the
segment (whose contents are shown in the center of each line). Segment contents are shown in
abbreviated form, with sequence number, control flags, and ACK field. Other fields such as
window, addresses, lengths, and text have been left out in the interest of clarity.
126.

TCPA

TCP B

1.

CLOSED

2.

SYN-SENT --> <SEQ=100><CTL=SYN>

3.

ESTABLISHED <-- <SEQ=300><ACK=101 ><CTL=SYN,ACK>

4.

ESTABLISHED --> <SEQ=101><ACK=301><CTL=ACK>


127. 5.

LISTEN
--> SYN-RECEIVED
<-- SYN-RECEIVED
-> ESTABLISHED

ESTABLISHED --> <SEQ=10lxACK=301XCTL=ACKxDATA> -->

ESTABLISHED
128. Basic 3-Way Handshake for Connection Synchronisation
129. In line 2 of above figure, TCP A begins by sending a SYN segment indicating that it will
use sequence numbers starting with sequence number 100. In line 3, TCP B sends a SYN and
acknowledges the SYN it received from TCP A. Note that the acknowledgment field indicates
TCP B is now expecting to hear sequence 101, acknowledging the SYN which occupied
sequence 100. At line 4, TCP A responds with an empty segment containing an ACK for TCP
B's SYN; and in line 5, TCP A sends some data. Note that the sequence number of the segment
in line 5 is the same as in line 4 because the ACK does not occupy sequence number space (if it
did, we would wind up ACKing ACK'sl).

130.

131.
Simultaneous initiation is only slightly more complex, as is shown in figure below.
Each TCP cycles from CLOSED to SYN-SENT to SYN-RECEIVED to ESTABLISHED.
132.

TCPA

1. CLOSED

TCP B

CLOSED

2. SYN-SENT

-> <SEQ=100><CTL=SYN>

3. SYN-RECEIVED <-- <SEQ = 300><CTL='SYN>


4. ... <SEQ=100><CTL=SYN>

<-SYN-SENT

--> SYN-RECEIVED

5. SYN-RECEIVED-><SEQ= 100><ACK=301 ><CTL=SYN,ACK> ...


6. ESTABLISHED <-- <SEQ=300xACK=101 ><CTL=SYNACK> <-- SYN-RECEIVED
7. ... <SEQ=101XACK=301XCTL=ACK>
133.

--> ESTABLISHED

Simultaneous Connection Synchronisation:

134.
Question: Why is three-way handshake needed? What is the problem if we send only
two packets and consider the connection established? What will be. the problem from
application's point of view? Will the packets be delivered to the wrong application?
135.

Problem regarding 2-way handshake

136. The only real problem with a 2-way handshake is that duplicate packets from a
previous connection( which has been closed) between the two nodes might still be floating on
the network. After a SYN has been sent to the responder, it might receive a duplicate packet of
a previous connection and it would regard it as a packet from the current connection which
would be undesirable.
137. Again spoofing is another issue of concern if a two way handshake is used.Suppose
there is a node C which sends connection request to B saying that it is A. Now B sends an
ACK to A which it rejects & asks B to close connection.Beteween these two events C can send
a lot of packets which will be delievered to the application..

138.

'

139.

The first two figures show how a three way handshake deals with problems of
duplicate/delayed connection requests and duplicate/delayed connection
acknowledgements in the network/The third figure highlights the problem of spoofing
associated with a two way handshake. Some Conventions:
141. 1.
The ACK contains'x+1'if the sequence number received is'x'.
142. 2. If '1SN' is the sequence number of the connection packet then 1 st data packet
140.

has
the seq number '1SN +1'
3. Seq numbers are 32 bit.They are byte seq number(every byte has a seq
number) .With a packet 1st seq number and length of the packet is sent.
3. Acknowlegements are cummulative.
143.5.
Acknowledgements have a seq number of their own but with a length"
0.So the
next data packet have the seq number same as ACK.
144.

145.

146. Fig. 3.19


The sender sends a SYN packet with serquence numvber say 'x'.

The receiver on receiving SYN packet responds with SYN packet with sequence
number y and ACK with seq number 'x+1'

On receiving both SYN and ACK packet, the sender responds with ACK packet
with seq number 'y+1'

147.

The receiver when receives ACK packet, initiates the

connection.
Connection Release:

148.

The initiator sends a FIN with the current sequence and acknowledgement
number.

The responder on receiving this informs the application program that it will
receive no more data and sends an acknowledgement of the packet. The
connection is now closed from one side.

Now the responder will follow similar steps to close the connection from its side.
Once this is done the connection will be fully closed.

149.

UDP (User Datagram Protocol):

UDP like its.cousin the Transmission Control Protocol (TCP) sits


directly on top of the base Internet Protocol (IP).
150.

In general, UDP implements a fairly "lightweight".layer above the


Internet Protocol. It seems at first site that similar service is provided by both
UDP and IP, namely transfer of data. But we need UDP for
multiplexing/demultiplexing of addresses.
151.

UDP's main purpose is to abstract network traffic in the form of datagram;;. A


datagram comprises one single "unit" of binary d a t a ; the first eight (8) bytes of a
datagram contain the header information and the remaining bytes contain the data
itself.
152.

153.

UDP Headers:

154.

The UDP header consists of four (4) fields of two bytes each:

155.

source port number


destination port number
datagram size
checksum
UDP port numbers allow different applications to maintain their own
"channels" for data; both UDP and TCP use this mechanism to support multiple
applications sending and receiving data concurrently. The sending application (that
could be a client or a server) sends UDP datagrams through the source port, and the
recipient of the packet accepts this datagram through the destination port. Some
applications use static port numbers that are reserved for or registered to the
application. Other applications use dynamic (unregistered) port numbers. Because the
UDP port headers are two bytes long, valid port numbers range from 0 to 65535; by
convention, values above 49151 represent dynamic ports.
156.

The datagram size is a simple count of the number of bytes contained in the
header and data sections . Because the header length is a fixed size, this field essentialh
refers to the length of the variable-sized data portion (sometimes called the payload). I
The maximum size of a datagram varies depending on the operating environment. I
With a two-byte size field, the theoretical maximum size is 65535 bytes. However,!
some implementations of UDP restrict the datagram to a smaller number -I sometimes
as low as 8192 bytes.
158. UDP checksums work as a safety feature. The checksum value represents anl
encoding of the datagram data that is calculated first by the sender and later by thel
receiver. Should an individual datagram be tampered with (due to a hacker) or
corrupted during transmission (due to line noise, for example), the calculations of tr: i
sender and receiver will not match, and the UDP protocol will detect this error. ThJ
algorithm is not fool-proof, but it is effective in manv cases.
157.

159. In UDP, check summing is optional turning it off squeezes a little extra

performance from the system -- as opposed to TCP where checksums are


mandatory. It should be remembered that check summing is optional only for the
sender, not the receiver. If the sender has used checksum then it is mandatory for the
receiver to do so.

Usage of the Checksum in UDP is optional. In case the sender does not use it, it
sets the checksum field to all O's. Now if the sender computes the checksum then the
recipient must also compute the checksum ah set the field accordingly. If the checksum is
calculated and turns out to be all l's then the sender sends all l's instead of all O's. This is
since in the algorithm for checksum computation used by / UDP, a checksum of all 1 's if
equivalent to a checksum of all O's. Now the checksum field is unambiguous for the
recipient, if it is all O's then checksum has not been used, in any other case the checksum
has to be computed.
160.

161.

DNS (Domain Name Service):

The internet primarily uses IP addresses for locating nodes. However, its
humanly not possible for us to keep track of the many important nodes as numbers.
Alphabetical names as we see would be more convenient to remember than the
numbers as we are more familiar with words. Hence, in the chaotic organization of
numbers (IP addresses) we would be much relieved if we can use familiar sounding
names for nodes on the network.
162.

There is also another motivation for DNS. All the related information about a
particular network (generally maintained by an organization, firm or university)
should be available at one place. The organization should have complete control over
what it includes in its network and how does it "organize" its network. Meanwhile, all
this information should be available transparently to the outside world.
163.

Conceptually, the internet is divide into several hundred top level domains
where each domain covers many hosts. Each domain is partitioned in subdomains
which may be further partitioned into subsubdomains and so on... So the domain
space is partitioned in a tree like structure as shown below. It should be noted that this
tree hierarchy has nothing in common with the IP address hierarchy or organization.
164.

The internet uses a hierarchical tree structure of Domain Name Servers for IP
address resolution of a host name.
165.

The top level domains are either generic or names of countries, eg of generic
top level domains are .edu .mil .gov .org .net .com .int etc. For countries we have one
entry for each country as defined in IS03166. eg. .in (India) .uk (United Kingdom). The
leaf nodes of this tree are target machines. Obviously we would have to ensure that the
names in a row in a subdomain are unique. The max length of any name between two
dots can be 63 characters. The absolute address should not be more than 255
characters. Domain names are case insensitive. Also in a name only letters, digits and
hyphen are allowed. For eg. www.iitk.ac.in is a domain name corresponding to a
machine named www under the subsubdomain iitk.ac.in.
166.

167.

168. Resource Records:

Every domain whether it is a single host or a top level domain can have a set
of resource records associated with it. Whenever a resolver (this will be explained
later) gives the domain name to DNS it gets the resource record associated with it. So
DNS can be looked upon as a service which maps domain names to resource records.
Each resource record has five fields and looks as below:
169.

170.

Domain Name Class Type Time to Live Value

Domain name: the domain to which this record applies.

Class-, set to IN for internet information. For other information other codes may be
specified.
171.

Type-, tells what kind of record it is.


Time to live: Upper Limit on the time to reach the destination
Value: can be an IP address, a string or a number depending on the record type.
172. Wireless Networks:
173. As the need of communication became more and more demanding, new

technologies in the field of networks developed. One of them is the use of wireless
networks. It is the transmission of data from source to destination without the use of
wires as the physical media.
174.______

__

Why to use Wireless?:

175.

Three reasons may be stated for the over-growing use of wireless networks
across the world:
176.

1.
They are ubiquitous networks. As the do not require messy wires as a
medium
of
communication, they can be used to connect far-off places.

177.

2.
They are cheaper than wired networks specially in the case of longdistance
communication.

178.

3.
They are pretty effective and fast, especially with the modern
advancements
in
this field.

179.

180.

Some Terms and Technologies:

181.

ATM-Asynchronous Transfer Mode:

ATM is a connection-oriented switching technology. It was built to support


ISDN (Integrated Services Digital Network). ISDN required high speed cables for both
its narrow band (64 Kbps) and broad band (155 Mbps) transmission. There were two
technologies available for transmitting data182.

1.
Circuit Switching: In this technology when a user makes a call, the
resources
are
reserved for him. The advantage of this technology is that it prevents collisions
among various users. But the disadvantage is that it leads to inefficient utilization
of bandwidth if the user fails to send data or if the transmission speed is faster
than the speed of sending data, then most of the bandwidth is wasted.

183.

184.2. Packet Switching: In this technology, resources are never reserved for any

particular user. The advantage of this technology is that it leads to efficient


utilization of bandwidth i.e. the channel is never free until & unless there are no
users, But the disadvantage is that it causes many collision.

ATM was built as a combination of the best features of these two. Also ATM
provides QoS (Quality of Service) based on the following priority pattern:
185.

1.
CBR-Constant Bit Rate: Jobs that can tolerate no delay are assigned the
CBR
priority. These jobs are provided same number of bits every frame.. For
example, viewing a video reel definitely requires some blocks in every frame.

186.

187.2. VBR-Variable Bit Rate: Jobs that may produce different sized packets at

different
times are assigned VBR priority. They are provided with a variable number of bits
varying between a maximum and a minimum in different frames, e.g.. a
document may be compressed differently by different machines. Transmitting it
will be a variable transmission.

188. 3.

ABR-Available Bit Rate: This is the same as VBR except that it has
only
the
minimum fixed. If there are no CBR or VBR jobs left, it can use the entire frame,

189. 4.UBR-Unavailable Bit Rate: These jobs are the least priority jobs. The network

does not promise anything but simply tries its best to transmit it.

190.

191.WLAN-Wireless LAN:
192.
This is currently being used as dictated by the standards of IEEE 802.11. It can be
installed at the medium access layer and the data transmission can occur using a converter to
reach the wired LAN network.(IEEE 802.x)
193.

WATM-Wireless ATM :

194.
It is the wireless version of ATM. It provides QoS. It is not yet available in market,
because installing it will require the simultaneous installation of ATM infrastructure. It is
currently being tested thoroughly.
195.

Coupling of

Networks: The
alternatives are:
1. WLAN

LAN

2. WATM

LAN

3. WLAN

ATM

4. WATM .

ATM

196. 1. WLAN-LAN is the simplest of the above. According to the IEEE standards, the IEEE
802.11 (WLAN) can be used with IEEE 802.x (LAN) as follows:

197.

1. WLAN-ATM- Not Feasible.


2.

WATM-LAN- Not Feasible because WATM requires an infrastructure of the type i ATM

3. WATM-ATM-this is also a simple scheme because WATM can run on ATM.


198.

Issues involved in Wireless Networks:

Cost and Speed: As it is being considered as an alternative to wired networks, ;: should be


faster and cheaper.
Quality of Transmission: It gives a higher BER (Bit Error Rate). The BER is greater I than 10
-6. This is caused because transmission quality depends highly on the I physical media
including landscape, weather etc.

RayLeigh Fading: The data has to travel the distance through a medium like air.
Several rays of the same stream cause Rayleigh fading due to interference. This
causes poor transmission.

Multipath Propagation: Similarly, due to multipath propagation, the signal


received at the destination may be garbled.

Hand-Offs: If hand-offs are used i.e., hexagonal cells each having a base station
and many mobile terminals, two Mobile terminals that are far enough can use the
same bandwidth. This reuse of bandwidth is helpful.

Dynamic Physical Characteristics: The terminal may be mobile and constantly i


moving. Thus the distance between the base station and any active terminal may
199.
be constantly changing. This has to be taken into account while designing.

Practical Implementation: The practical implementation of any wireless


network
requires CSMA/CD for proper transmission. The range of any terminal is fixed. So,
there may be two terminals that are out of range of each other. These are called
Hidden Terminals. Collisions may be caused due to simultaneous sending of data
from any two hidden terminals. The Hidden Terminal Problem should be
overcome with the help of Base Station.

200.

Mobility and Network Topologies: Wireless networks should be effective enough


to overcome the problems caused by the topology of the area and the mobility of
the terminals

Frequency Allocation: Licensed & Unlicensed: For licensed network's, permission


has to be taken from the authorities that grant you a fixed bandwidth which is not
used by anybody else while unlicensed networking does not require any such
permissions. It just provides with some unlicensed bands which can be used by
anybody. Unlicensed bands may thus, cause collisions.

Capture Effect: If there are more than one terminals requiring the attention of the
Base Station, the one nearer to the base station may capture it. This unfair access
to the base station should be prevented.

Power Requirements and Battery: This problem arises for the Mobile Terminals
that run battery or cells. Much dissipation of power is caused when switching
"from receiving mode to sending mode and vice versa.

201.

Human Safety: Not all bandwidths can be used . Also, the intensity
should
not
be
very high as it may lead to several complications in human body e.g.. cataract.

202. Wireless Physical Media:


203. In the wireless physical media, three technologies are used:
204.

' 1. Transmission at Infrared frequency: This is easier to build and set-up. It is


mainly used for indoor purposes because the beam has to be focussed and can't
cross opaque media like walls etc.

2. Transmission through Microwave: This is preferred as it requires low


power consumption, (the bandwidth is fixed) But the basic problem is that it
requires UvAe-crt-S\gYAV.. Mso, \\.Tequ\ies\\cerAse.

205.

3. Transmission at Radio Frequency: This is the one that is most familiar to us.

206.

The

bandwidth is pretty large.

Vr\\efflW>} aftu Security oi foe s\ST\a\

207.

Spread Spectrum: To reduce the effect of noise signals, the bandwidth of the
signal is increased tremendously. This is costly but assures better transmission. This is
called SPREAD-SPECTRUM. This is used in two ways:
208.

FHSS (Frequency hopping rpread spectrum): The entire packet is not sen.t at the
same bandwidth. Say, it is sent at frequency range A for time Tl, frequency range
B for time T2, A for Tl, B for T2 and so on. The receiver also knows, this sequence
and so, looks at A for time Tl, then at B for time T2 and so on. Thus this sort of
understanding between the sender and receiver prevents the signal from being
completely garbled.

DSSS (Direct Sequence Spread Spectrum): This involves sending of coded data
instead of the actual data. This code is known to the destination only which can
decipher the data now.

The problem still left undealt is that of bursty errors. If there is lot of traffic,
interference may hinder the Base Station from receiving data for a burst of time. This
is called "Bursty Errors".
209.

210.

Such problem are looked at by MAC-Medium Access

Control. Medium Access Control:


To control the traffic, various techniques are used. MAC fulfills the folloyWng
requirements:
1. QoS Requirements: It provides Quality of Service according to the priority of jobs.
1. Error Control: Error handling is done using some codes.
2. Frame Size: To transmit maximum data, we want the frame-size to be maximum
but at the same time, large frame-size highly increases the probability of errors.
So, MAC provides a tradeoff between the above two factors determining the size
of the frame.
'
3. Secure Transmission: The data meant for a particular receiver is secured from
others.
211.

212.

5. Reasonable Transmission: If the number of users increases, each should get


reasonable service. MAC prevents unfair access to channel.
6. Efficient utilization of Power: If a transmitter is always on, it is continuously ,
using power even if there is no data on the channel for it. This is reduced by sending
the transmitter to "sleep mode" whenever the battery is going down. In 1 this mode,
the transmitter is unable to receive any data.

7.

Architecture for Wireless Network:


There are two types of architecture possible:

8.

1. AD-HOC NETWORK
2. INFRASTRUCTURE NETWORK
The Ad-Hoc network can be set up anytime. It does not require a Base Station.
It is generally used for indoor purposes. The Infrastructure network involves Base
Station and Mobile Terminals. It provides uplink facility (link from MT to BS) and
downlink facility (link from BS to MT).
9.

The Mac Protocol:

10.

This protocol decides how to assign data slots to different users. The various
policies it uses are:
11.

1.
2.
3.
4.
5.

Fixed Assignment Policy


Random Assignment Policy
Centrally Controlled Policy
Distributed Controlled Policy
Hybrid Controlled Policy
Fixed Assignment Policy:

12.

In this policy, each terminal is assigned some sort of data slot to speak. It
causes a fixed delay. It is done in 3 ways:
13.

14. 1. TDM A (Time Division Multiple Access): Each user is given a fixed time to

speak after which the chance goes to another user. This cycle continues
indefinitely.

15.

9.

16. 2. FDMA (Frequency Division Multiple Accesses): Each user is given a

fixed bandwidth in which he can speak at all times.


17.

18.

19. 3. CDMA (Codivision Multiple Access): Each user is given different frequencies at

different times. This ensures that each user gets a fair amount of channel each
time.

20.

Also, sometimes, statistical multiple access is used in which a slot is assigned


to a user only if it has data to send.
21.

22.

Random Assignment Policy:

In this policy, contention slots are provided to all the users. Problem may arise
if the numbers of users increase drastically. The number of contention slots should be
variable. This may cause some limiting of data slots but is necessary to prevent the
derailment of the service.
23.

24.

Centrally Controlled Policy:

This is used in infrastructure architecture. It involves the participation of a


Base Station which may assign slots and priorities (CNBR, VBR etc.) to all the users.
25.

26.

Distributed Controlled Policy:

This is used in Ad-Hoc architecture. The control is among the terminals which
decide among themselves about who is going to speak first.
27.

28.

Hybrid Controlled Policy:

This combines the best features of centrally controlled and distributed


controlled policies.
29.

30.

Kinds of MAC protocols:

31.

There are two kinds of Mac protocols:

32. I. FDD (Frequency Division Duplex):This provides two separate bandwidths for

uplink and downlink transmission. This leads to inefficient utilization of


bandwidth as there is more traffic on downlink than uplink

33. 2.

TDD {Time Division Duplex): This provides an adoptive boundary between


the uplink and downlink frequency which depends on the what is being used at
that
34.
particular time. It works as follows:
35.

'.

Any mobile terminal can be in 3 states-, empty state, request state and readyto-transmit state.
36.

1. uplink: MTl sends a random-access request to BS to communicate with MT2


2. Downlink: BS sends a b-bit access id to MT2
3. Uplink: MTl sends the packet
4. Downlink: BS sends the packet to .'.'.12 Routing
in Internet:
T

37.

he Origin of Internet:
'
The response of Internet to
the
issue
of
choosing
routing
tables
with
complete/par
tail information is shown by the following architecture There are a few nodes having
complete routing information and a large number of nodes with partial information.
The nodes with complete information, called core gateways i:t ell connected by a
Backbone Network. These nodes talk to each other to keep themselves updated. The
non-core gateways are connected to the core gal
Historically, this architecture
39.
comes from the ARPANET.)
38.

The original internet was


structured
around
a
backbone
of
ARPANET
with
several
core gateways connected to it These core g:
connected some Local Area
41.
Networks (LANs) to the rest of the network. These core gateways talked to
themselves and exchanged routing information's. Every core gateway contained
complete information about all possible destinations.
40.

42.

43.

How do you do routing?:

44. The usual IP routing algorithm employs an internet routing table (some times cali

an IP routing table) on each machine that Stores the information about the poss
destinations, and how to reach them.

Default Routes:

45.

This technique used to hide information and keep routing table size small
consolidates multiple entries into a default case. If no route appears in the routing
table, the routing routine sends the data gram to the default router.
46.

Default routing is especially useful when a site has a small set of local
addresses and only one connection to the rest of the internet.
47.

Host-Specific Routes-.

48.

Most IP routing software allows per-host routes to be specified as a special


case. Having per-host routes gives the local network administrator more control over
network use, permits testing, and can also be used to control access for security
purposes. When debugging network connections or routing tables, the ability to
specify a special route to one individual machine turns out to be especially useful.
49.

50.

Backbones:

Internet with Two


;'

51.

52. As long as there was just one single router connecting ARPANET with NSFNET

there was no problem. The core gateways of ARPANET had information aboul a\\
destinations and the routers inside NSFNET contained information about local
destinations and used a default route to send all non-NSFNET traffic to between

core gateways through the router between ARPANET and NSFNET.


However as multiple connections were made between the two backbones, problems
arise. Which route should a packet from netl to net2 take? Should it be Rl or R2 or R3
or R4 or R5? For this some exchange of routing information between the two
backbones was necessary. But, this was again a problem as how should we compare
information.
53.

Gateway-To-Gateway Protocol (GGP):


This was the protocol used by the core-routers to exchange routing
informatior.
56.
among them. This is based on Distance Vector Algorithm and uses number
of hops a
57.
the distance metric.

54.
55.

58. This is a very poor metric as this does not take into account the load on the links

and whether a link is slow or fast. A provision is made to manually increment the hop
count in case a link is particularly slow. A protocol based on Shortest Path First
Algorithm, known as SPREAD, was also used for the same purpose.
59.

10.

60. Added Complexity To The Architecture Model:


61. As the number of networks and routers increased, to reduce the load on the core

gateways because of the enormous amount of calculations, routing was done with
some core gateways keeping complete information and the non-core gateways
keeping partial information.

62.

In thisarchitecture, Gl ,G2 ,G3 are all core gateways and G4 and G5 are
non-core gateways. We must have a mechanism for someone to tell G2 that it is
connected to net2 , net3 and net4 , besides netl. Only G5 can tell this to G2 and so we
must provide for a mechanism for G2 to talk to G5 . A concept of one backbone with
-gateways connected to Autonomous Systems was developed. An Autonomous system
is a group of networks controlled by a single administrative author:*.;.
63.

Routers within an autonomous system are free to choose their own


mechanisms for discovering , propagating .validating , and checking the consistency
of routes. Each autonomous system must agree to advertise network reachability
information to other autonomous systems. Each advertisement propagates through a
core router. The assumption made is that most of the routers in the autonomous
system have
65.
complete information about the autonomous system. One such router will te
assigned the task of talking to the core gateway.
64.

Interior Gateway Protocols (IGP):

66.

1GP is a type of protocols used by the routers in an autonomous system to


exchange network reachability and routing information. Some of IGPs are given
below.
67.

Routing Information Protocol (RIP):

68.

This is one of the most widely used IGP. It was developed at Berkeley. This
is also known by the name of the program that implements it, routed .This implements
Distance Vector algorithm. Features of RIP:
69.

RIP uses a hop count metric to measure the distance to a destination. To


compensate for differences in technologies, many RIP implementations allow
managers to configure artificially high hop counts when advertising connections to
slow networks. Ali routing updates are broadcast. This allows all hosts c >i > ;
network to know about the routes.

To prevent routes from oscillating between two or more equal cost paths. RIP
70. specifies that existing routes should be retained until a new route has sh icily
71. lower cost. Since RIP does not explicitly detect routing loops, RIP must
either
72. assume participants can be trusted (being part of one autonomous system) or
73. take precautions to prevent such
loops.
74.
To prevent instabilities, RIP must use a low value for the maximum possible
distance. RIP uses 16 as the maximum hop count. This restricts the maximum
network diameter of the system to 16.

To solve the slow convergence problem arising due to slow propagation of routing
information, RIP uses Hold Down. If a particular link is down , any new
information about that link is not accepted till some time. This is because the
router must wait till the information aboutthe link being down propagates to
another router before accepting information from that router about that down link.

RIP runs on top of TCP/IP. RIP allows addresses to be of a maximum size of 14


Bytes. The Distance varies from 1 to 16 (where 16 is used to signify infinity). RIP
address 0.0.0.0 denotes a default route. There is no explicit size of the RIP
message and any number of routes can be advertized.

75.

. .

'

76.

12. The message format is as


shown: 11.

77. OSPF(Open Shortest Path First V.

This is an Interior Gateway Protocol designed by the Internet Engineering


Task Force (IETF). This algorithm scales better than the vector distance algorithms.
This Protocol tackles several goals:
78.

OSPF includes type of service(ToS) routing. So, you can install multiple routers to
a given destination, one for each type of service. When routing a datagram, a
router running OSPF uses both the destination address and type of service fields in
the IP Header to choose a route.

OSPF provides load balancing. If there are multiple routes to a given destination at
the same cost, OSPF distributes traffic over all the routes equally.

OSPF allows for creation of AREA-HIERARCHIES. This makes the growth of the
network easier and makes the network at a site easier to manage. Each area is self
contained, so, multiple groups within a site can cooperate in the use of OSPF for
routing.

OSPF protocol specifies that all exchanges between the routers be


authenticated. .OSPF allows variety of authentication schemes, and even allows
one area to
79. choose a different scheme from the other areas.

To accomodate multi-access networks like ethernet, OSPF allows every multiaccess network to have a designated router( designated gateway).

To permit maximum flexibility, OSPF allows the description of a virtual network


topology that abstracts away from details of physical connections.

80. OSPF also allows for routers to exchange routing information learned from

other sites. The message format distinguishes between information acquired from
external sources and information acquired from routers interior to the site, so
there is no ambiguity about the source or reliability of routes.

14.

It hastoo much overhead of sending LSPs but is gradually becoming


13.
popular.

81.

Two routers may be configured to think they are neighbours

82. Exterior Gateway Protocol (EGP):

If two routers belonging to two different autonomous systems exchange routing


information, the protocol used is called EGP. EGP consists of:
83.

Acquisition Request: A router sends a request to another neighbour router saying


'I want to talk'.

Acquisition Confirm: This is a positive reply to the Acquisition request.

Acquisition Refuse: This is a negative response to the Acquisition request.

Cease Request: This requests termination of neighbour relationship.

Cease Confirm: This is a confirmation response to the Cease Request.

Hello: This is used to find if the neighbour router is up or down. This requests
router to respond if alive.
84.

I Heard You: This is a response to the Hello message confirming that the router is
alive. Because it is possible for Hello or 1 Heard You messages to be lost in
transit, EGP uses a k-out-of-n rule to determine whether a network is down.At
least k of the last n messages must fail for the router to declare its neighbour
down.

Poll Request: This is a request for network routing update.

Routing Update: This conveys routing information about reachable networks to its
EGP neighbour. The routing information is the distance vector of the reachable
networks.

85. Error: This is a response to an incorrect message.

EGP is used only to find network reach ability and not for differentiating
between good and bad routes. We can only use distance metric to declare a route
plausible and not for comparing it with some other route (unless the two route form
part of a same autonomous system).
86.

87.

"'

Since there cannot be two dMfcitl -outes to the same network, EGP restricts
the topology of any internet to a tree structure in which a core system forms the root.
There are no loops among other autonomous systems connected to it. This lends to
several problems:
88.

Univerasal connectivity fails if the core gateway system fails.

EGP can advertise only one path to a given network.

EGP does not support load sharing on routers between arbitrary autonomous systems.

Multiple backbone networks with multiple connections between them cannot be


handled by EGP.

89.

Border Gateway

ProtocoKBGPV.
BGP is a distance-vector protocol used to communicate between different
Ashes. Instead of maintaining just the cost to each destination, each BGP router keeps
track of the exact path used. Similarly, instead of periodically giving each neighbor its
estimated cost to each destination, each BGP router tells its neighbors the path it is .
using. Every BGP router contains a module that examines routes to a given
destination and scores them returning a number for destination to each route. Any route
violating a policy constraint automatically gets a score of infinity. The router adapts a
route with shortest distance. The scoring function is not a part of the BGP protocol and
can be any function that the system managers want. BGP easily solves the count to
infinity problem that plagues other distance-vector algorithms as whole path is known.
90.

Anda mungkin juga menyukai