Friday, 2 May 2014

Example of Choice in Java

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/*<applet code="Ex63.java" width=500 height=400></applet>*/
public class Ex63 extends Applet
{
  MyChoice choice;
  public void init()
  {
    choice=new MyChoice();
    choice.add("Red");
    choice.add("Green");
    choice.add("Blue");
    add(choice);
  }
  class MyChoice extends Choice
  {
    public MyChoice()
     {
       enableEvents(AWTEvent.ITEM_EVENT_MASK);
     }
    protected void processItemEvent(ItemEvent ie)
    {
         showStatus("Choice selection:"+getSelectedItem());
         super.processItemEvent(ie);
    }
  }
} 

Example Of Jtable

import javax.swing.*;
import java.awt.*;
import javax.swing.tree.*;
import java.awt.event.*;

/*<applet code="JTabledemo" width=400 height=200></applet>*/

public class JTabledemo extends JApplet
{


public void init()
{
Container c=getContentPane();



c.setLayout(new BorderLayout());

String[] colHeads={"name","phone no","city"};

Object[][] data={
{"abc","9999999","pune"},
{"xyz","8888888","mumbai"}
};

JTable tb=new JTable(data,colHeads);




int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
int h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
JScrollPane jsp=new JScrollPane(tb,v,h);

c.add(jsp,BorderLayout.CENTER);



}


}




Example Of Java Program For JScrollbar (Implement ScrollBar)

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/*<applet code="JScrolldemo" width=200 height=200></applet>*/

public class JScrolldemo extends JApplet
{
public void init()
{

Container c=getContentPane();

c.setLayout(new BorderLayout());

JPanel jp=new JPanel();
jp.setLayout(new GridLayout(2,2));


JRadioButton b1=new JRadioButton("Indian Express");
jp.add(b1);
JRadioButton b2=new JRadioButton("Times of India");
jp.add(b2);
JRadioButton b3=new JRadioButton("Sakal");
jp.add(b3);
JRadioButton b4=new JRadioButton("Lokmat");
jp.add(b4);

ButtonGroup bg1=new ButtonGroup();
ButtonGroup bg2=new ButtonGroup();
bg1.add(b1);
bg1.add(b2);
bg2.add(b3);
bg2.add(b4);

int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
int h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
JScrollPane jsp=new JScrollPane(jp,v,h);

c.add(jsp,BorderLayout.CENTER);


}

}

Thursday, 1 May 2014

15 Puzzel problem C Program in C


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

int Game[4][4];

void Reset();
void Left();
void Right();
void Up();
void Down();
void Print();
int  Check();
void Logo();




void main()

{
int counter=0;
 char character='a';
 clrscr();

 Reset();    
 Logo();              
 Print();
 while(character!='q')
 {
  character=getch();
  switch(character)
  {
  case 80:
  Down();
  Print();
  Check();
  break;

  case 77:
  Right();
  Print();
  Check();
  break;

  case 75:
  Left();
  Print();
  Check();
  break;

  case 72:
  Up();
  Print();
  Check();
  break;

  case'r':
  Reset();
  Print();
  Check();
  break;
  }
 }



}


void Logo()
{

 clrscr();

 printf("\n==============================================================================");
 printf("\n                 This GAME Is Designed By ADMIN                ");
 printf("\n==============================================================================");

 printf("\n\n\n\n\n\n\t\t\t Keys: \n\n\n\t\t\t\t Up");
 printf("\n\n\t\t\t\t Down");
 printf("\n\n\t\t\t\t Left");
 printf("\n\n\t\t\t\t Right\n\n\n\n");


 printf("\n\n\n\n\t\t               'q' to Quit.");
 printf("\n\n\n\n\n\n\n\t\t\tPress Any Key To Continue");

getch();
}





void Print()
{       clrscr();

 printf("\n\n\n\n\n\n\n\n\n\n\n\n");
 printf("\n\t\t\t_____________________");
 printf("\n\t\t\t|    |    |    |    |");
 printf("\n\t\t\t|    |    |    |    |");
 gotoxy(27,16);
 {
  printf("%d",Game[0][0]);
 }
 gotoxy(32,16);
 {
  printf("%d",Game[0][1]);
 }
 gotoxy(37,16);
 {
  printf("%d",Game[0][2]);
 }
 gotoxy(42,16);
 {
  printf("%d",Game[0][3]);
 }
 printf("\n\t\t\t|    |    |    |    |");
 printf("\n\t\t\t---------------------");
 printf("\n\t\t\t|    |    |    |    |");
 printf("\n\t\t\t|    |    |    |    |");
 gotoxy(27,20);
 {
  printf("%d",Game[1][0]);
 }
 gotoxy(32,20);
 {
  printf("%d",Game[1][1]);
 }
 gotoxy(37,20);
 {
  printf("%d",Game[1][2]);
 }
 gotoxy(42,20);
 {
  printf("%d",Game[1][3]);
 }
 printf("\n\t\t\t|    |    |    |    |");
 printf("\n\t\t\t---------------------");
 printf("\n\t\t\t|    |    |    |    |");
 printf("\n\t\t\t|    |    |    |    |");
 gotoxy(27,24);
 {
  printf("%d",Game[2][0]);
 } gotoxy(32,24);
 {
  printf("%d",Game[2][1]);
 }
 gotoxy(37,24);
 {
  printf("%d",Game[2][2]);
 }
 gotoxy(42,24);
 {
  printf("%d",Game[2][3]);
 }
 printf("\n\t\t\t|    |    |    |    |");
 printf("\n\t\t\t---------------------");
 printf("\n\t\t\t|    |    |    |    |");
 printf("\n\t\t\t|    |    |    |    |");
 gotoxy(27,28);
 {
  printf("%d",Game[3][0]);
 }
 gotoxy(32,28);
 {
  printf("%d",Game[3][1]);
 }
 gotoxy(37,28);
 {
  printf("%d",Game[3][2]);
 }
 gotoxy(42,28);
 {
  printf("%d",Game[3][3]);
 }
 printf("\n\t\t\t|    |    |    |    |");
 printf("\n\t\t\t~~~~~~~~~~~~~~~~~~~~~");
   
}




void Reset()
{
 int reset=15,i,j;

 for ( i=0;i<4;i++)
 {
  for ( j=0;j<4;j++)
  {
   Game[i][j]=reset--;
  }
 }
 Game[3][3]=0;
}




int Check()
{

    int count=0,i,j,counter;
    int check=0;
    if(check!=0)
    {
    counter++;
    }

 for ( i=0;i<4;i++)
 {
  for ( j=0;j<4;j++)
  {
   count++;
   if(i==3 && j==3)
   {
    break;
   }
   if( Game[i][j]==count)
   {
    check++;
   }

  }
 }
 if(check==15)
 {
  gotoxy(32,36);
  printf("Congratulations");
  return 1;

 }
 else return 0;
}





void Left()
{
 int x,y,i,j;
     
 for ( i=0;i<4;i++)
 {
  for ( j=0;j<4;j++)
  {
   if (Game[i][j]==0)
   {
    x=i;
    y=j;  
   }
  }
 }
 if(y!=3)
 {
  Game[x][y]=Game[x][y+1];
  Game[x][y+1]=0;
 }


}



void Right()
{
 int x,y,i,j;

 for ( i=0;i<4;i++)
 {
  for ( j=0;j<4;j++)
  {
   if (Game[i][j]==0)
   {
    x=i;
    y=j;  
   }
  }
 }
 if(y!=0)
 {
  Game[x][y]=Game[x][y-1];
  Game[x][y-1]=0;
 }


}




void Up()
{
 int x,y,i,j;

 for ( i=0;i<4;i++)
 {
  for ( j=0;j<4;j++)
  {
   if (Game[i][j]==0)
   {
    x=i;
    y=j;  
   }
  }
 }
 if(x!=3)
 {
  Game[x][y]=Game[x+1][y];
  Game[x+1][y]=0;
 }


}





void Down()
{
 int x,y,i,j;

 for ( i=0;i<4;i++)
 {
  for ( j=0;j<4;j++)
  {
   if (Game[i][j]==0)
   {
    x=i;
    y=j;  
   }
  }
 }
 if(x!=0)
 {
  Game[x][y]=Game[x-1][y];
  Game[x-1][y]=0;
 }


}

Longest Common Sub sequence C Propgram

#include<stdio.h>
    #include<string.h>
    #define size 20
    char A[size],B[size];
    int i,j,A_len,B_len;
    int score[size][size];
    void main()
    {
        printf("\tEnter first string\n\t");
        scanf("%s",A);
        printf("\tEnter second string\n\t");
        scanf("%s",B);
        printf("\n");
        A_len = strlen(A);
        B_len = strlen(B);
        A[0] = ' ';
        printf("\t      ");
        for(i=1;i<=B_len;i++)
            printf("%5c",toupper(B[i]));
        printf("\n\n");
        for(i=0;i<=A_len;i++)
        {
            for(j=0;j<=B_len;j++)
            {
                if(i==0 || j==0)
                    score[i][j]=0;
                else if(A[i] == B[j] )
                    score[i][j] = score[i-1][j-1] + 1;
                else
                {
                    if(score[i][j-1]>score[i-1][j])
                        score[i][j] = score[i][j-1];
                    else
                        score[i][j] = score[i-1][j];
                }
            }
        }
        for(i=0;i<=A_len;i++)
        {
            printf("\t%c",toupper(A[i]));
            for(j=0;j<=B_len;j++)
                printf("%5d",score[i][j]);
            printf("\n\n");
        }
        if(score[A_len][B_len] != 0)
        {
            printf("Longest common subsequence is of %d length :\t",score[A_len][B_len]);
        }
        else
            printf("longest common subsequence not found");
       getch();
    }

Bazier curv C program

#include <stdio.h>
#include <graphics.h>
#include <conio.h>
int gd,gm,maxx,maxy;
float xxx[4][2];

/* Function to draw line from relative position
specified in array xxx-----------------------*/

void line1(float x2,float y2)
{
line(xxx[0][0],xxx[0][1],x2,y2);
xxx[0][0]=x2;
xxx[0][1]=y2;
}
/* Bezier function
-------------------- */
void bezier(float xb,float yb,float xc,float yc,float xd,float yd,int n)
{
float xab,yab,xbc,ybc,xcd,ycd;
float xabc,yabc,xbcd,ybcd;
float xabcd,yabcd;
if (n==0)
{
line1(xb,yb);
line1(xc,yc);
line1(xd,yd);
}
else
{
xab = (xxx[0][0]+xb)/2;
yab = (xxx[0][1]+yb)/2;
xbc = (xb+xc)/2;
ybc = (yb+yc)/2;
xcd = (xc+xd)/2;
ycd = (yc+yd)/2;
xabc = (xab+xbc)/2;
yabc = (yab+ybc)/2;
xbcd = (xbc+xcd)/2;
ybcd = (ybc+ycd)/2;
xabcd = (xabc+xbcd)/2;
yabcd = (yabc+ybcd)/2;
n=n-1;
bezier(xab,yab,xabc,yabc,xabcd,yabcd,n);
bezier(xbcd,ybcd,xcd,ycd,xd,yd,n);
}
}

/* Function to initialise graphics
----------------------------------- */
void igraph()
{
detectgraph(&gd,&gm);
if(gd<0)
{
puts("CANNOT DETECT A GRAPHICS CARD");
exit(1);
}
initgraph(&gd,&gm,"d:\\tcpp\\bgi");
}
       void  main()
{
int i;
float temp1,temp2;
igraph();

/* Read two end points and two control points of the curve
---------------------------------------------------------- */
for(i=0;i<4;i++)
{
printf("Enter (x,y) coordinates of point%d : ",i+1);
scanf("%f,%f",&temp1,&temp2);
xxx[i][0] = temp1;
xxx[i][1] = temp2;
}
bezier(xxx[1][0],xxx[1][1],xxx[2][0],xxx[2][1],xxx[3][0],xxx[3][1],8);
getch();
closegraph();
}

Prim Algorithm C Program

/*Prim Algorithm C Program*/
#include<stdio.h>
#include<conio.h>
#define size 20
#define INFINITY 32767
void prim(int G[][size],int nodes)
{
 int tree[size],i,j,k;
 int min_dist,v1,v2,total=0;
 for(i=0;i<nodes;i++)
 {
  tree[i]=0;
 }
  printf("\n\n The minimal spaning tree is:\n");
  tree[0]=1;
  for(k=1;k<=nodes-1;k++)
  {
   min_dist=INFINITY;
   for(i=0;i<=nodes-1;i++)
   {
    for(j=0;j<=nodes-1;j++)
    {
     if(G[i][j]&&((tree[i]&&!tree[j])||(!tree[i]&&tree[j])))
     {
      if(G[i][j]<min_dist)
      {
       min_dist=G[i][j];
       v1=i;
       v2=j;
      }
     }
    }
   }
   printf("\nEdge(%d,%d) and weight=%d",v1,v2,min_dist);
   tree[v1]=tree[v2]=1;
   total=total+min_dist;
  }
  printf("\n\n\tTotal path length is=%d",total);
}
void main()
{
 int G[size][size] ,nodes;
 int v1,v2,length,i,j,n;
 clrscr();
 printf("\n\tPrim's Algorithm\n");
 printf("\nEnter the no. of nodes in Graph");
 scanf("%d",&nodes);
 printf("\nEnter the no. of edges in Graph");
 scanf("%d",&n);
 for(i=0;i<nodes;i++)
  for(j=0;j<nodes;j++)
   G[i][j]=0;
   printf("Enter Edge and weight");
   for(i=0;i<n;i++)
   {
    printf("\nEnter edge by v1&v2:");
    printf("[read the graph from starting node[0]]");
    scanf("%d%d",&v1,&v2);
    printf("Enter the corresponding weight");
    scanf("%d",&length);
    G[v1][v2]=G[v2][v1]=length;
   }
 getch();
 printf("\n\t");
 clrscr();
 prim(G,nodes);
 getch();
}