Monday, 29 July 2013

Algorithm and program for Binary Search.

Binary search or half-interval search algorithm compares the
 element to be search(i.e key) with the middle element of the array. 
If the key is matched with the middle element ,it returns its 
position.and if not it divides the array into sub array according to 
the value of key. 
If the key value is greater than the value of middle element then 
the it will search the key from middle to right. 
If the key value is less than the value of middle element then the it 
will search the key from middle to left.
 It will repeat this action until no further division are possible or 
until the key matches with any of the element in array.
    Binary search  Binary search 
/*....binary search program ......*/
#include <stdio.h>
void main()
{ int arr[20]; int i, low, mid, high, key, size; printf("Enter the size of an array\n"); scanf("%d", &size); printf("Enter the elements\n"); for(i = 0; i < size; i++) { scanf("%d", &arr[i]); } printf("Enter the element to be search according to binary search\n"); scanf("%d", &key); /*Binary search */ low = 0; high = (size - 1); while(low <= high) { mid = (low + high)/2; if(key == arr[mid]) { printf("element found at %d position \n",mid+1); return; } if(key < table[mid]) high = mid - 1; else low = mid + 1; } printf("element not found\n");
getch(); } Binary search  Binary search  Binary search  Binary search 
 Output
Enter the size of an array 5 Enter the array elements 12 36 45 78 99 Enter the key 45 element found at 3 position

Sunday, 28 July 2013

Program to implement Stack in C

/* Write a program to implement stack in C. Stack is a LIFO data structure    *
 * LIFO - Last in First Out                                                *
 * Perform PUSH(insert operation), POP(Delete operation) and Display stack */
#include <stdio.h> #include <conio.h> #define MAX SIZE 5
struct stack /* Structure definition for stack */ { int stk[MAXSIZE]; int top; };
typedef struct stack STACK; STACK s;
/* Function declaration*/
void push (void); int pop(void); void display (void);
void main () { int choice; int option = 1;
clrscr ();
s.top = -1;
printf ("STACK OPERATION\n"); while (option) { printf ("----------------------------------\n"); printf (" 1 >---> PUSH \n"); printf (" 2 >---> POP \n"); printf (" 3 >---> DISPLAY \n"); printf (" 4 >---> EXIT \n"); printf ("----------------------------------\n");
printf ("Enter your choice\n"); scanf ("%d", &choice); switch (choice) { case 1: push(); break; case 2: pop(); break; case 3: display(); break; case 4: return; }
fflush (stdin); printf ("wish to continue(Type 0 or 1)?\n"); scanf ("%d", &option); } }
/*Function to add an element to the stack*/ void push () { int num; if (s.top == (MAXSIZE - 1)) { printf ("Stack is Full\n"); return; } else { printf ("Enter the element to be pushed\n"); scanf ("%d", &num); s.top = s.top + 1; s.stk[s.top] = num; } return; }
/*Function to delete element from stack*/ int pop () { int num; if (s.top == - 1) { printf ("Stack is Empty\n"); return (s.top); } else { num = s.stk[s.top]; printf ("poped element is = %d\n", s.stk[s.top]); s.top = s.top - 1; } return(num); }
/*Function to display the status of the stack*/ void display () { int i; if (s.top == -1) { printf ("Stack is empty\n"); return; } else { printf ("\nThe status of the stack is\n"); for (i = s.top; i >= 0; i--) { printf ("%d\n", s.stk[i]); } } printf ("\n"); }
/*--------------------------------------------------------------------------- Output STACK OPERATION ------------------------------------------ 1 >---> PUSH 2 >---> POP 3 >---> DISPLAY 4 >---> EXIT ------------------------------------------ Enter your choice 1 Enter the element to be pushed 23 Do you want to continue(Type 0 or 1)? 1 ------------------------------------------ 1 >---> PUSH 2 >---> POP 3 >---> DISPLAY 4 >---> EXIT ------------------------------------------ Enter your choice 1 Enter the element to be pushed 45 Do you want to continue(Type 0 or 1)? 1 ------------------------------------------
1 >---> PUSH 2 >---> POP
3 >---> DISPLAY 4 >---> EXIT ------------------------------------------ Enter your choice 1 Enter the element to be pushed 78 Do you want to continue(Type 0 or 1)? 1 ------------------------------------------ 1 >---> PUSH 2 >---> POP 3 >---> DISPLAY 4 >---> EXIT ------------------------------------------ Enter your choice 3
The status of the stack is 78 45 23
----------------------------------------------------------------*/

Saturday, 27 July 2013

Top wesbites to learn Java

Wish to create your software games and web applications .Here i suggest some resource for you. Now you habe to practice the things which you read theoretically. It will help to sharp your skills .Apart from us LANG-ADDA there some more website to learn java.

Top 5 website
  1. http://docs.oracle.com/javase/tutorial
  2. http://thenewboston.org/tutorials.php
  3. http://javabeginner.com/
  4. http://javalessons.com/
  5. http://w3schools.com/js/


The Sites are;-

  1. http://www.javaalmanac.com - The online counter part of  Java Developer's Almanac
  2. http://www.onjava.com - O'Reilly's Java website. articles on java weekly.
  3. http://java.sun.com - The official Java developer website - new articles. 
  4. http://www.developer.com/java - Java articles
  5. http://www.java.net - The Java  website hosted by Sun Microsystems. 
  6. http://www.builder.com - Cnet's Builder.com website - All technical articles 
  7. http://www.ibm.com/developerworks/java - IBM's Developerworks; the Java section. 
  8. http://www.javaworld.com - One of the originals.  updates of Java articles. 
  9. http://www.devx.com/java - Java articles hosted at DevX. 
  10. http://www.fawcette.com/javapro - The JavaPro online magazine website. \
  11. http://www.sys-con.com/java - The Java Developers Journal online magazine website. 
  12. http://www.javadesktop.org - The desktop Java community hosted at Java dotnet. 
  13. http://www.theserverside.com - Often considered the resource for all discussion server-side Java specific.
  14. http://www.jars.com - The Java  Covers frameworks and applications. 
  15. http://www.jguru.com - A great source for Q&A style interaction in the community. 
  16. http://www.javaranch.com 
  17. http://www.ibiblio.org/javafaq/javafaq.html - The comp.lang.java FAQ - questions asked and  answered,
  18. http://java.sun.com/docs/books/tutorial/ - The Official Java tutorial from Sun - very useful for almost any feature set. 
  19. http://www.javablogs.com - Blog aggregator for the most active Java-based blogs throughout the internet. 
  20. http://java.about.com/ - Java news and articles 

Introduction to computer networks

                     :: Introduction to computer networks::

Physical Component-
 A no. of Hardware component are required to built a computer network devices -
 1) End Devices
 2)Physical cables
 3)Hub.-Broad casting
 4)Switch - uni casting

 1)End Devices - These are the user consoles using which a user access network services e.g desktop.laptops ,network printers ,network phones ,etc.

 2)Physical cables - these are the guided mediums through which network data flows .The various categories of physical cables are .

 i. Coaxial cables - This cable supports database up to 10 kbps. This is highly protected insulated cable in which data flows through central copper core.

 ii. Twisted pair cable- these cables are further classified into two categories -

1.Shielded twisted pair and UN -shielded twisted pair. Twisted pair cables supports data rates ranging from 50 kbps to 10 Mbps.

 2.These cables are used most frequently to build a network.

 Optical Fiber - This cables works on the principle of total internal reflection. Optical fiber is the expensive cable which works on the principle of TIR (total internal reflection ). In optical fiber data is transferred using light rays . When light is being passed in the medium it represent a binary one being transmitted ,no light in the medium represents a binary zero .Optical fiber has a glass core usual made of silicon.Optical fiber has minimum signal decay.

3. Hub - is the basic hard ware component used to connect more than two terminals or end devices in a network . A hub when receives a packet broadcast it to all the attached terminals. Hub does no packet filtering & no conjection control.

 4. Switch - is a advance version of hub .It is configurable hardware component which can be con figured using user user console. Switch maintain a mac table which contain information about all available ports and packet forwarding is based on the information provided by the mac table .Switch have its own processor and internal memory as compared to a hub ,switch has routing functions less delay and supports conjunction control technique.

 5. Router- It is a hardware component used to connect more than two network and routing the signals .It is used for selecting more efficient path of transition. It maintains a routing table which is used for path sharing.

 6.Bridge - Bridge is used for  connecting the two isolated network of same network. 

 7.Repeater - Repeater is used to support & extend cable segment length.

Pointers in c

 A pointer is an address. Instead of  a variable, it is a pointer to a variable which is  stored somewhere in the memory of the program. Let  examine the a program [POINTER.C] which has some pointers to understand the concept of pointers.
C Programming
C Programming


#include "stdio.h"
/* illustration of pointer use */
void main( )
{
int i, *p1, *p2;
i = 39;        /* any  value */
p1 = &index;   /* the address of i */
p2 = pt1;
printf("The value is %d %d %d\n", i, *p1, *p2);

*p1 = 13; /* lets changes the value of i*/
printf("The value is %d %d %d\n", i, *p1, *p2);
}

We declare the pointers with the help of  star(asterisk).
Lets examine the first statement, it is clear that we assign 39 to the variable i. 
In next  statement, we assign the address of i to pt1."&" shows th address.
In this example, p1 and p2 are pointers, and the variable i is a simple variable.Before explaining the rest of program lets have look on Two very important s rule for using the pointers.



Two very important rules:

1. A variable have an ampersand (&) in front of it      refers to the address of the variable. line no 7. 
2.  A pointer with a star in front of it define  the value  of the variable.

 pointed to by the pointer. line no.10.

Memory aids::

1. Think of & as an address.

2. Think of * as a star referring to stored.

 As a pointers p1 and p2 do not contain a  value but an address of a variable. Line 7 in program assigns the pointer p1 to point to the variable i.


Line 10 modifies the value by using the pointer. because p1 points to the variable index, then putting a star in front of the pointer refers to the memory location to which it is pointing
C - Overview
C - Basic Syntax
C - Data Types
C - Variables
C - Constants
C - Storage Classes
C - Operators
C - Decision Making
C - Loops
C - Functions
C - Scope Rules
C - Arrays
C - Pointers
C - Strings
C - Structures
C - Unions
C - Bit Fields
C - Typedef
C - Input & Output
C - File I/O
C - Preprocessors
C - Header Files
C - Type Casting
C - Error Handling
C - Recursion
C - Variable Arguments
C - Memory Management
C - Command Line Arguments

Access modifers in java

 Java's access specifiers are  : -
1. Public,
2. Private,
3.Default, and
4.Protected.
 Java also defines a default access level. protected applies only when inheritance is involved. The other access specifiers public  and private is described from below diagram.

Relationship in java

                                                                     RELATIONSHIPS
IS-A   :  Is-A   relation is that relation in which the child class inherits all the properties of super class. The best e.g of explaining this relationship is as follow, Rose IS-A flower.In this example flower is the super class and Rose is the child class .Rose inherits all those properties which a flower have.

HAS-A : Has -A relation is that relation in which the child class do not have all properties of super class.In this type of relation the child class inherits only some properties of super class .


Friday, 26 July 2013

Multithreaded programming in Java

java  supports  multithreaded programming. A multithreaded program contains more than one part that can run parallerly. Each part  is called a thread, and each thread defines a separate way of execution.
The another term related to threads is process: A process consists of the memory space which is allocated by the operating system that can contain one or more threads. A thread must be a part of a process. A process remains running until all threads are done executing.
By Multithreading you can write  programs that make maximum use of the CPU, because idle time of CPU should be minimum.

Life Cycle of a Thread:

A thread goes through various stages in at time of execuition. The stages are shown below.
Java Thread
Stages  mentioned  above are explained here:
  • New: A new thread is created or born a It remains in this state until the program starts the thread.
  • Runnable: at this state the thread becomes runnable and thread considered to be executing its task.
  • Waiting: In this state the thread is waiting for the complition of task of another thread .
  • Timed waiting: A runnable thread can enter itn this stage for a short interval of time. 
  • Terminated: A runnable thread enters at this stage for to be terminated 

Thread Priorities:

Each Java thread has a priority order that which helps operating system to determine the order in which threads are to be execuited
  The range of priorities lies between MIN_PRIORITY (a constant of 1) and MAX_PRIORITY (a constant of 10). By default, every thread has priority NORM_PRIORITY (a constant of 5).
Threads has higer priority are more important tant.

Creating a Thread:

Java defines two ways :
  • By implement the Runnable interface.
  • B extend the Thread class, itself.

Create Thread by Implementing Runnable:

The first  way is to create a class that implements the Runnable interface.
 A class need only implement a single method called run( ), which is declared like this:
public void run( )
Inside run( ), you may define the code that of  new thread. It is
important to understand that run can call other methods,  other classes, and
declare variables, 


Create thread by extend the Thread class : 

for  creation  of a athread is to  extends Thread class,
and then to create an object  of that class. The  class must override the
run( ) method of thread class, in which u can define the new thread. It must also call start( ) method 
to begin execution of the new thread. 

Thursday, 25 July 2013

Inheritance in JAVA

Inheritance is a process in which a class acquiers the properties as well as behaviour of another class. In this, the class which acquires the properties is called Subclass and the class from which subclass acquires the properties is called Superclass.

Here  Bicycle is a Superclass And all other class is its sub classes  which  inherits the propeties of superclass.

Inheritance can be of 5 types :

1. Single Inheritance
2. Multiple Inheritance
3. Multi-level Inheritance
4. Hierarchial Inheritance
5. Network Inheritance

JAVA doesnot supports Multiple Inheritance. Instead of Multiple Inheritance, it uses another concept termed as Interfacing.
 To inherit the properties we use extends key word. Supposewe have class A and class B.

Monday, 22 July 2013

Data types in java


  Primitive Data type in JAVA

   Primitive data type can divided into three main categories:                                  
  •     Integral  Types à used for signed integer  (byte short,int ,long) and unsigned character values(char).
  •         Floating point Types à used for fractional signed numbers(float,double).
  •          Boolean Type à used for logical values.




Data type
With(bits)
Minimum& Maximum value
Wrapper Class
boolean
Not applicable
True, false
Boolean
byte
8
-27,   27-1
Byte
short
16
-215  ,215-1
Short
char
16
0x0,0xfff
Character
int
32
-231,231-1
Integer
long
64
-263,263-1
Long
float
32
+-1.4012986432481707e-45f
+-3.402823476638528860+38f
Float
double
64
+-4.9065645841246544-324
+-1.79769313486231570e+308
Double