October 27, 2013

Interface and Implementation in OOP

Interface is a set of functions of an object that he wants to expose to other objects. Interface of an object provides us the list of available functions.
As we discussed previously that data and behavior of each object is hidden in that object itself so we have to use the concept of interface of the object to expose its behavior to outer word objects.
· Different objects may need different functions of an object so interface of an object may be different for different objects.
· Interfaces are necessary for object communication. Each object provides interface/s (operations) to other objects through these interfaces other objects communicate with this object.
Example – Interface of a Car
· Steer Wheels
· Accelerate
· Change Gear
· Apply Brakes
· Turn Lights On/Off
Example – Interface of a Phone
· Input Number
· Place Call
· Disconnect Call
· Add number to address book
· Remove number
· Update number

It is actual implementation of the behavior of the object in any Object Oriented language.
It has two parts,
· Internal data structures to hold an object state that will be hidden from us it will store values for an object data members.
· Functionality in the form of member functions to provide required behavior.
Examples of Implementation
  1. Gear Box in car system
Consider object Gear Box in car system it has a certain structure and functionality. When this object will be implemented it will have two things,
· Physical structure of the gear box
· Functionality implemented in this structure to change gear.
Both these things are part of implementation.
So it has,
· Data Structure in the form of Mechanical structure of gear box
· Functionality mechanism to change gear

  1. Address Book in a Phone
Similarly take the example of contact details saved in the SIM of a phone, In that case we can say physical structure of SIM card as Data Structure
And Read/write operations provided by the phone as Functionality.
 
As discussed earlier we only show interface of an object to outside world and hide actual implementation from outside world. The benefit of using this approach is that our object interface to outside word becomes independent from inside implementation of that interface. An object may have more than one interface.
This is achieved through the concepts of encapsulation and information hiding. Interface and implementation are separated from each other to achieve Information Hiding.
 
Real Life example of separation of interface and implementations
Driver has a standard interface to drive a car and using that interface he drive can drive any car regardless of its model or type whatever engine type it has or whatever type of fuel it is using.
There is another important phenomena in OOP , i.e. ‘messages’.  
Objects communicate through messages they send messages (stimuli) by invoking appropriate operations on the target object. The number and kind of messages that can be sent to an object depends upon its interface. Objects communicate with each other using messages.
Examples – Messages
A Person sends message (stimulus) “stop” to a Car by applying brakes
A Person sends message “place call” to a Phone by pressing appropriate button



Object-Orientation

Object oriented programming is an essential part of our today’s programming methodology. It is being extensively used in all programming languages due to its key aspects and benefits which will be discussed later on. It is a technique in which we visualize our programming problems in the form of objects and their interactions as happens in real life.

Examples:
We have different objects around us in our real life that interact with each other to perform different operations For example, Human beings, cars, tree, house etc.
Take an example of a person lives in a house. He drives a car. All these are objects and have their own unique characteristics. Take another example of a School; the objects in a school are student, teacher, books, pen ,school bag, classroom, parents, playground etc.

Suppose we want to develop a fee collection system for a school for this we will need to find out related objects and their interactions as happens in real life. In this way we can say that object orientation makes it easier for us to solve our real world problems by thinking solution of the problem in terms of real world objects.
So we can say that in our daily life everything can be taken as an object that behaves in a certain way and has certain attributes. In object orientation we move our concentration to objects in contrast to procedural paradigm in which we simply write our code in functions and call them in our main program.

What is a Model?
A model is an abstraction of something real or conceptual. We need models to understand an aspect of reality.

Model Examples
Highway maps
Architectural models
Mechanical models

OO Models:

In the context of programming models are used to understand the problem before starting developing it. We make Object Oriented models showing several interacting objects to understand a system given to us for implementation.
Object Oriented Model

Objects
Ali, Car, House, Tree


Interactions
Ali lives in the house
Ali drives the car

clip_image025
Object Oriented Model (A School Model)
Objects
Teacher, Student, School Bag, Pen, Book Playground
 
Interactions
Teacher teaches Student.
Student has School Bag, Book and Pen

clip_image029

Object-Orientation - Advantages

As Object Oriented Models map directly to reality as we have seen in examples above therefore,
We can easily develop an object oriented model for a problem.
Everyone can easily understand an object oriented model.
We can easily implement an object oriented model for a problem using any object oriented language like c++ using its features like classes, inheritance, virtual functions etc.

What is an Object?
An object is,
  1. Something tangible (Human being, School, House, Car).
  2. Something conceptual (that can be apprehended intellectually for example time, date and so on…).
An object has,
  1. State (attributes)
  2. Well-defined behavior (operations)
  3. Unique identity

Tangible and Intangible Objects

Examples of Tangible Objects:
Ali is a tangible object, having some characteristics (attributes) and behavior as given below,
Characteristics (attributes)
Name
Age
Behaviour (operations)


Walks
Eats


We will identify Ali using his name.
Car is also a tangible object having some characteristics (attributes) and behavior given below,

State (attributes)

Color
Model

Behavior (operations)
Accelerate
Start Car
Change Gear


We can identify Car using its registration number.

Examples of Intangible Objects (also called as conceptual objects):

Time is an intangible (conceptual) object

State (attributes)

Hours
Seconds
Minutes


Behavior (operations)
Set/Get Hours
Set/Get Seconds
Set/Get Minutes
We will assign our own generated unique ID in the model for Time object. 


Date is also an intangible (conceptual) object

State (attributes)
Year
Day
Month

Behavior (operations)
Set/Get Year
Set/Get Day
Set/Get Month
We will assign our own generated unique ID in the model for Date object.

Let us look at some of the key aspects of object orientation.

Key Aspects:
  • Model is the abstraction of some real word scenario. It helps us to understand that scenario.
  • Object oriented model of any scenario (problem) describes that scenario (problem) in the form of interacting objects.
  • We use Object Orientation because it helps us in mapping real world problem in a programming language.
  • Object Orientation is achieved using objects and their relationships.
  • Properties of an object are described using its data members and behavior of an object is described using its functions.
  • Objects may be tangible (physical) or intangible (also called conceptual or virtual).
  • Generally when we have given a certain problem description, nouns in that problem description are candidates for becoming objects of our system.
  • There may be more than one aspects of an object
  • It is not necessary that every object has a specific role in implementation of a problem there may be some objects without any role, like school parking in our school.
  • It is easier to develop programs using Object Oriented Programming because it is closer to real life.

October 26, 2013

Windows 8.1 Password Resolved

In daily use, we do not really prefer to always type a long password that we have for our live account. Instead, what we can do is, we can very easily alter it or you can say bypass it by applying the following procedure.

Click on the search in the right panel as shown in the figure below.

 

1

 

After you click on search, the following panel will open. Type password. Here you see, as highlighted in the figure below. Click on “Sign-in options”.

 

 2

 

Now, here you have different options, the most suitable and easier way is to have a four digit pin.

Click, on the change button in the “PIN” option which is the third one here.

 

3

 

First it will prompt you to enter your Hotmail account password which you are using to log into your windows.

Enter your password. As i did mine in the following diagram.

 

 4

 

Now, enter your four digit pin number.

 5

 

Like this.

 

 6

 

And here you go, now when you want to log into your windows you will just have to enter the pin and it logs in.

 

October 10, 2013

C program to print different Star Formations using For Loop


Here is the code to print different star formations using for loop.

#include "stdio.h"
int main()
{
    int num,i,j,k,m,n,o,p;
     printf("Please enter a number to print the star formations \n");


     scanf("%d", &num);   
   
for(i=0;i<num;i++)
    {
    for(j=0;j<num;j++)
        {
                printf("*");
        }
                printf("\n");
    }

printf("\n");

j=0,i=0,k=0;
// Hollow Star formation
    for(i=0;i<num;i++)
    {
    for(j=0;j<num;j++)
        {
            if(i==0 || i==num-1)
            {
                printf("*");   
            }
            else if(j==0 || j==num-1)
            {
                printf("*");
            }
            else
            {
                printf(" ");
            }
        }
        printf("\n");
    }


     // downward descending formation
     printf("\n");
     j=0,i=0,k=0;
     for(i=num;i>0;i--)
        {
            for(j=i;j>0;j--)
            {
                printf("*");
            }
            printf("\n");
        }

printf("\n");
j=0,i=0,k=0;
 
    for(i=num;i>=0;i--)
        {
            for(k=i;k<num;k++)
            {
            printf(" ");
            }
            for(j=0;j<=i-1;j++)
            {
                printf("*");
            }
            printf("\n");
        }


printf("\n");
j=0,i=0,k=0;
// Star formation right angle triangle.
        for(i=num;i>0;i--)
        {
            for(j=0;j<i-1;j++)
            {
                printf(" ");
            }
            for(k=j;k<num;k++)
            {
            printf("*");
            }
            printf("\n");
        }


printf("\n");
j=0,i=0,k=0;
    for(i=0;i<num;i++)
    {
    for(j=0;j<=i;j++)
        {
                printf("*");
        }
                printf("\n");
    }

     return 0;
}











The output of the above code is following:

st

C program to print the hollow stars square

Here is the code for the C program to print the star formation of a hollow square. The program will input an integer value from the user and print the hollow star formations.
Here is the code.

#include “stdio.h”
int main()
{
int num,i,j;
printf("Please enter a number to print the hollow star formation \n");
scanf("%d", &num);
for(i=0;i<num;i++)
{
for(j=0;j<num;j++)
{
if(i==0 || i==num-1)
{
printf("*");
}
else if(j==0 || j==num-1)
{
printf("*");
}
else
{
printf(" "); // space is printed ..
}
}
printf("\n");
}
return 0;
}


























Here is the sample output.
star

As can be seen in the output above, for the user input, the hollow star square will be formed. The key to understand the logic of the code is as follows.
The outerloop controls the number of  lines and inner loop controls the number of stars.

October 8, 2013

C program to display integers in Ascending without using Loop

This program run sorts and display the integers in ascending order. It finds the largest and smallest integer values respectively.



#include "stdio.h"
#include "conio.h"


int main()



{


    int a=0, b=0, c=0,d=0,e=0, f=0, grade;
    printf("Enter four numbers\n");
   
    scanf("%d",&a);
    scanf("%d",&b);
    scanf("%d",&c);
    scanf("%d",&d);
   
    if(a>b && a>c && a>d)
    {
           printf("1st value that you entered is largest\n");
           e=1;
    }
    else if(b>a && b>c && b>d)
    {
           printf("2nd value that you entered is largest\n");
           e=2;
    }
    else if(c>a && c>b && c>d)
    {
           printf("3rd value that you entered is largest\n");   
           e=3;
    }
    else if(d>a && d>b && d>c)
    {
           printf("4th value that you entered is largest\n");
           e=4;
    }

// For finding smallest value

     if(a
    {
           printf("1st value that you entered is smallest\n");
           f=1;
    }
    else if(b
    {
           printf("2nd value that you entered is smallest\n");
           f=2;
    }
    else if(c
    {
           printf("3rd value that you entered is smallest\n");
           f=3;
    }
    else if(d
    {
           printf("4th value that you entered is smallest\n");
           f=4;
    }

//Sorting //

    if(e==1 && f==2)
    {
            if(c
            {
                   printf("Sorted order is \n %d %d %d %d", b,c , d, a);
            }
            else
            {
                printf("Sorted order is \n %d %d %d %d", b,d , c, a);
            }
    }
    else
    if(e==1 && f==3)
    {
            if(b
            {
                   printf("Sorted order is \n %d %d %d %d", c,b , d, a);
            }
            else
            {
                printf("Sorted order is \n %d %d %d %d", c,d , b, a);
            }
    }
    else
    if(e==1 && f==4)
    {
             if(b
            {
                   printf("Sorted order is \n %d %d %d %d", d,b , c, a);
            }
            else
            {
                printf("Sorted order is \n %d %d %d %d", d,c , b, a);
            }
    }
   
    // for e=2
   
        if(e==2 && f==1) // a is smallest, b is largest
    {
            if(c
            {
                   printf("Sorted order is \n %d %d %d %d",a, c , d, b);
            }
            else
            {
                printf("Sorted order is \n %d %d %d %d", a,d , c, b);
            }
    }
    else
    if(e==2 && f==3)// c is smallest
    {
            if(a
            {
                   printf("Sorted order is \n %d %d %d %d", c,a , d, b);
            }
            else
            {
                printf("Sorted order is \n %d %d %d %d", c,d , a, b);
            }
    }
    else
    if(e==2 && f==4)  // d is smallest
    {
             if(a
            {
                   printf("Sorted order is \n %d %d %d %d", d,a , c, b);
            }
            else
            {
                printf("Sorted order is \n %d %d %d %d", d,c , a, b);
            }
    }
   
    // for c is largest i.e.. e=3
            if(e==3 && f==1) // a is smallest, c is largest
    {
            if(b
            {
                   printf("Sorted order is \n %d %d %d %d",a, b , d, c);
            }
            else
            {
                printf("Sorted order is \n %d %d %d %d", a,d , b, c);
            }
    }
    else
    if(e==3 && f==2)// b is smallest
    {
            if(a
            {
                   printf("Sorted order is \n %d %d %d %d", b,a , d, c);
            }
            else
            {
                printf("Sorted order is \n %d %d %d %d", b,d , a, c);
            }
    }
    else
    if(e==3 && f==4)  // d is smallest
    {
             if(a
            {
                   printf("Sorted order is \n %d %d %d %d", d,a , b, c);
            }
            else
            {
                printf("Sorted order is \n %d %d %d %d", d,b , a, c);
            }
    }   
   
       
    // for d is largest i.e.. e=4
   
   
   
            if(e==4 && f==1) // a is smallest
    {
            if(b
            {
                   printf("Sorted order is \n %d %d %d %d",a, b , c, d);
            }
            else
            {
                printf("Sorted order is \n %d %d %d %d", a,c , b, d);
            }
    }
    else
    if(e==4 && f==2)// b is smallest
    {
            if(a
            {
                   printf("Sorted order is \n %d %d %d %d", b,a , c, d);
            }
            else
            {
                printf("Sorted order is \n %d %d %d %d", b,c , a, d);
            }
    }
    else
    if(e==4 && f==3)  // d is smallest
    {
             if(a
            {
                   printf("Sorted order is \n %d %d %d %d", d,a , b, d);
            }
            else
            {
                printf("Sorted order is \n %d %d %d %d", d,b , a, d);
            }
    }       

    getch();
    return 0;
   
   
}


Here is the output.


 

September 25, 2013

Ubuntu Installation using Windows Installer

There is an absolute easy way to install  ubuntu. Let us see step by step procedure to install ubuntu by using ubuntu windows installer.
First download the windows installer by writing following in google and open the first link.
1
Now, click on the Get the installer button.

September 9, 2013

C Program Phases Writing And Executing A Program

System Software
The system software controls the computer. It communicates with computer’s hardware (key board, mouse, modem, sound card etc) and controls different aspects of operations. Sub categories of system software are:
  • Operating system
  • Device drivers
  • Utilities
Operating system

An operating system (sometimes abbreviated as "OS") is the program that manages all the other programs in a computer. It is a integrated collection of routines that service the sequencing and processing of programs by a computer. Note: An operating system may provide many services, such as resource allocation, scheduling, input/output control, and data management.

Device drivers

The device driver software is used to communicate between the devices and the computer. We have monitor, keyboard and mouse attached to almost all PC’s; if we look at the properties of these devices we will see that the operating system has installed special software to control these devices. This piece of software is called device driver software. When we attach a new device with the computer, we need software to communicate with this device. These kinds of software are known as device drivers e.g. CD Rom driver, Sound Card driver and Modem driver. Normally manufacturer of the device provide the device driver software with the device. For scanners to work properly with the computers we install the device driver of the scanner. Nowadays if you have seen a scanner, it comes with TWAIN Drivers. TWAIN stands for Technology Without An Interesting Name.

September 4, 2013

Installation of Dev C++


Here is the step by step procedure for the installation of Dev C++
First, download the Dev C++ from the following link.
http://sourceforge.net/projects/orwelldevcpp/files/latest/download

Then after download execute the setup.  The screenshots are of different versions.

1

Select Language.

 2
Click I agree.

September 3, 2013

Introduction to Programming

Definition

"A program is a precise sequence of steps to solve a particular problem.”It means that when we say that we have a program, it actually means that we know about a complete set activities to be performed in a particular order. The purpose of these activities is to solve a given problem. Alan Perlis, a professor at Yale University, says:"It goes against the grain of modern education to teach children to program. What fun is there in making plans, acquiring discipline in organizing thoughts, devoting attention to detail and learning to be self-critical? ". It is a sarcastic statement about modern education, and it means that the modern education is not developing critical skills like planning, organizing and paying attention to detail. Practically, in our day to day lives we are constantly planning, organizing and paying attention to fine details (if we want our plans to succeed). And it is also fun to do these activities. For example, for a picnic trip we plan where to go, what to wear, what to take for lunch, organize travel details and have a good time while doing so.

May 27, 2013

Xgraph in NS2

 

One part of the ns-allinone package is 'xgraph', a plotting program which can be used to create graphic representations of simulation results. In this section, I will show you a simple way how you can create output files in your Tcl scripts which can be used as data sets for xgraph. On the way there, I will also show you how to use traffic generators.
A note: The technique I present here is one of many possible ways to create output files suitable for xgraph. If you think there is a technique which is superior in terms of understandablity (which is what I aim for in this tutorial), please let me know.


First of all, we create the following topology:
 
Nam snap shot
 
The following piece of code should look familiar to you by now if you read the first sections of this tutorial.



set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]

$ns duplex-link $n0 $n3 1Mb 100ms DropTail
$ns duplex-link $n1 $n3 1Mb 100ms DropTail
$ns duplex-link $n2 $n3 1Mb 100ms DropTail
$ns duplex-link $n3 $n4 1Mb 100ms DropTail


May 22, 2013

NS2 installation using Ubuntu Software Center

There is an absolute easy way to install ns2 (network simulator) by using the easiest technique available on ubuntu. 
Let us install ns2 in a very nice and easy way. Ubuntu Software Center provides with ease to install different softwares by wizard.

Here is how it is done.

Open “Ubuntu Software Center” as hightlighted in the below figure.



Now, in the search box on the top right corner. Write ns2 and press enter.


You will see the package. Hit install button and it will install ns2. Ofcourse, internet connectivity is must :)
Similarly, you can install nam(network animator) by following the same procedure. Just enter "nam" in the search box and hit enter.

Install the first package that you see. And there you go, you have installed ns2 and nam .  You can always install it using terminal simply by writing the following command.

sudo apt-get install ns2 nam xgraph


Remember, when you do the same in Ubuntu 13.04, it will not show you the desired ns2 package. And you will have to install it manually. I guess this is only available for 12.04 version. So, there is another way available if you want to install ns2 in that version.

This could be resolved by applying the following command.


sudo apt-get update


When the update is completed, go and run the command to install ns2 , nam and xgraph. If you want an easy way out to install Ubuntu using windows. Please click here.

 

May 21, 2013

Dynamic Nodes generation and traffic flow in ns2

Here is the code for dynamically creating nodes and create traffic flow dynamically using protocol.


#Create a simulator object
set ns [new Simulator]

#Tell the simulator to use dynamic routing
$ns rtproto DV

#Open the nam trace file
set nf [open out.nam w]
$ns namtrace-all $nf


#Define a 'finish' procedure
proc finish {} {
        global ns nf
        $ns flush-trace
#Close the trace file
        close $nf
#Execute nam on the trace file
        exec nam out.nam &
        exit 0
}

#Create seven nodes
for {set i 0} {$i < 7} {incr i} {
        set n($i) [$ns node]
}


#Create links between the nodes
for {set i 0} {$i < 7} {incr i} {
        $ns duplex-link $n($i) $n([expr ($i+1)%7]) 1Mb 10ms DropTail
}

#Create a UDP agent and attach it to node n(0)
set udp0 [new Agent/UDP]
$ns attach-agent $n(0) $udp0

# Create a CBR traffic source and attach it to udp0
set cbr0 [new Application/Traffic/CBR]
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.005
$cbr0 attach-agent $udp0

April 16, 2013

EVIL IS NOTHING BUT ABSENCE OF GOOD

Professor : You are a Muslim, aren’t you, son ?

Student : Yes, sir.

Professor: So, you believe in GOD ?

Student : Absolutely, sir.

Professor : Is GOD good ?

Student : Sure.

Professor: Is GOD all powerful ?

Student : Yes.

C program to Read From a File

#include <stdio.h> #include <stdlib.h> void main() {     FILE *fptr;     char filename[15];     char ch;   ...