>
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:
{
/* initialization */
/* 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
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))
{
DCn XffTTftf D AT
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
^. ",
:,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();
} /* 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.
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:
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 */
/* 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
_'
"
"
.........
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;
int *ip;
// uninitialized pointer to
// integer
constant integer
cip = &ci;
cip = cicp;
// by pointer cp = &ci;
change value of constant pointer ip = cip;
// cannot
// this
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;
// cr = 7;
reference
r = 3;
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;
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 */ }
class Point {
int _x, _y;
public:
// point coordinates
// begin interface section
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;
>
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.
}
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.
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;
}
void setX(const int val); void setY(const int val); int getX()
{ return _x; ) int getY() { return _y; }
};
// 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()
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() { /* Nothing to do */ }
int getZ() { return _z; }
void setZ(const int val) { _z = val; }
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) {}
,
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:
public:
..,
};
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();
};
class LightRed : public Red {
public:
-LightRed();
);
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:
c = a + b;
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.
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.
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--*
'
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
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;
the middle nodes, which have exactly one predecessor and exactly one successor and
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;
}
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;
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:
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) :
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.
1
virtual ~ListBase() {}
// Force destructor to be
// virtual
public:
List() : _head(NULL), _tail(NULL) {}
List(const List &val) : _head(NULL), _tail(NULL) {
*this = val;
)
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) {
, .
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
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?
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,
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.
9.
10.
Fig. 3.3 Unstructured programming.
The main program directly operates on global data.
12.
Procedural Programming:
22.
23.
24. To sum up: Now we have a single program which is devided into small pieces
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.
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.
33.
You all know singly linked lists which use a very simple structure, consisting of
34.
35.
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.
39.
/*
MODULE Singly-Linked-
List-1 BOOL
list_initialize() ;
41.
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.
The
call
HstJ
two I
proc
The
54.
ANY data;
55.
56.
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.
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.
66.
List-2
MODULE Singly-LinkedDECLARE
TYPE
list_handle_t; list_handle_t
list_create();
67.
list_destroy(list_handle_t this);
BOOL
68.
69.
70.
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.
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.
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.
); 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.
88.
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.
93.
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.
96. iist_destroy(myList);
97.
END
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.
104.
106.
Object-Oriented Programming
3.
4.
5.
6.
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.
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
115.
e-Prior
Index
Next->
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.
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.
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.
129.
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.
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.
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
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 variable declared within a class, but outside the methods of the
class, is the entire class.
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.
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.
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 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.
163.
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.
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:
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.
176.
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.
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.
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.
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 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.
196.
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.
A class may extend only one other class, but it may implement any number
of interfaces. The syntax is:
203.
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.
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.
222. Every enum class creates a new namespace, so that the same name may be used
223. 3.6
What is Java? .
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.
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.
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.
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.
246.
class HelloWorld {
247.
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.
javac nofile.java
259.
260.
261.
262.
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.
Under Unix, compiling and running the code looks like this:
266.
$ javac HelloWorld.Java
267.
$ Java HelloWorld
268.
Hello World
269.
270.
271.
272.
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.
279.
$ Java HelloWorld
280.
281.
Make
282.
sure it includes the current directory as well as the location where the classes.zip
file
283.
284.
CLASSPATH=.:/usr/local/java-l. 1/lib
285.
286.
C:\JDK\JAVA\CLASSES;c:\java\lib\classes.zip
287.
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
be
291.
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.
312. }
313. Example
314. Addition
315. while loop
316.Relations
317. Variable declarations and assignments
318.
321.__________________________________________
324.
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
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.
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.
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.
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.
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.
343. Since I have very little experience with Windows, I don't have a recommendation
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.
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.
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.
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.
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.
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.
18.
20.
Static: Routes are based on static tables that are "wired into" the network and are
21. rarely changed.
22.
23.
24.
25.
26.
Routing:
27.
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.
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.
32.
33.
34. Network Layer does not guarantee that the packet will reach its intended
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.
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.__________________
__________________^
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.
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.
52. 2.
Depending on radius
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.
59.
61.
63.
64.
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.
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.
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.
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.
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.
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.
102.
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.
105.
106. 2. Nop-Adaptive Routing Algorithm: These algorithms do not base their routing
107.
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
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.
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.
123. The three-way handshake reduces the possibility of false connections. It is the
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.
3.
4.
LISTEN
--> SYN-RECEIVED
<-- SYN-RECEIVED
-> ESTABLISHED
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>
<-SYN-SENT
--> SYN-RECEIVED
--> ESTABLISHED
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.
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.
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.
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.
153.
UDP Headers:
154.
The UDP header consists of four (4) fields of two bytes each:
155.
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
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.
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.
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.
Class-, set to IN for internet information. For other information other codes may be
specified.
171.
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.______
__
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.
181.
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
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.
WATM-LAN- Not Feasible because WATM requires an infrastructure of the type i ATM
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.
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.
200.
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.
205.
3. Transmission at Radio Frequency: This is the one that is most familiar to us.
206.
The
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.
212.
7.
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.
10.
This protocol decides how to assign data slots to different users. The various
policies it uses are:
11.
1.
2.
3.
4.
5.
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.
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.
22.
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.
26.
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.
30.
31.
32. I. FDD (Frequency Division Duplex):This provides two separate bandwidths for
33. 2.
'.
Any mobile terminal can be in 3 states-, empty state, request state and readyto-transmit state.
36.
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.
42.
43.
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.
50.
Backbones:
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
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.
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.
66.
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.
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.
75.
. .
'
76.
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.
To accomodate multi-access networks like ethernet, OSPF allows every multiaccess network to have a designated router( designated gateway).
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.
81.
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.
Routing Update: This conveys routing information about reachable networks to its
EGP neighbour. The routing information is the distance vector of the reachable
networks.
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.
EGP does not support load sharing on routers between arbitrary autonomous systems.
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.