Hub Of Geekz

  • Home
  • About Me
  • Contact Us
  • Home
  • Languages
    • C++
    • Java
    • Perl
    • Prolog
    • Bootstrap
  • Database
    • SQL
    • PL/SQL
  • Study
    • Java
      • Java Collection
      • Java Concurrency
      • Java Interview Questions
      • Java Networking
    • Number System
  • Kavita
  • Entertainment
    • Hinglish Cafe
    • Videos
    • Jokes
  • Windows Tricks
  • How To

Tuesday, 30 October 2012

Reading data from a given file

 Earthcare Foundation NGO     07:52     Java Networking     No comments   

Program:

//In this program we are reading the data from a file.
import java.io.*;
public class filer
{
public static void main(String ar[])
{
try
{
FileReader fr=new FileReader("input.txt");
BufferedReader br=new BufferedReader(fr);
String str=br.readLine();
while(str!=null)
{
System.out.println(str);
str=br.readLine();
}

}catch(Exception e)
{
System.out.println(e);
}
}
}
Output:




Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Writing data in given file

 Earthcare Foundation NGO     07:48     Java Networking     No comments   


Program: /*In this program we are writing the data in the file.File will automatically created if there is not a file with the given name*/
import java.io.*;
public class file
{
public static void main(String ar[])
{
try
{
FileWriter fw=new FileWriter("output.txt");
PrintWriter pw=new PrintWriter(fw);
String str="Hello";
String str1="how are you";
pw.println(str);
pw.println(str1);
pw.flush();
pw.close();
fw.close();
}catch(Exception e)
{
System.out.println(e);
}
}
}
Output:
   The data will be written in the given file.
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Tuesday, 23 October 2012

Java Program Using GUI

 Earthcare Foundation NGO     09:28     Java Networking     No comments   

Program:

import javax.swing.*;
class sw
{
public static void main(String ar[]) throws Exception
{

int i=10;
int j=20;
JOptionPane.showInputDialog("This is a simple program using GUI");
JOptionPane.showMessageDialog(null,"The sum is:"+(i+j));

}
}
Output:


Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Thursday, 18 October 2012

Chating program using UDP

 Earthcare Foundation NGO     03:14     Java Networking     No comments   

Program to chat using UDP Protocol
Server:

import java.io.*;
import java.net.*;
import java.util.*;
class udpchs
{
public static void main(String ar[]) throws Exception
{
DatagramSocket ssoc=new DatagramSocket(3232);
BufferedReader dt=new BufferedReader(new InputStreamReader(System.in));
InetAddress ip=InetAddress.getLocalHost();
byte buf[]=new byte[1024];
DatagramPacket dpac=new DatagramPacket(buf,buf.length);
while(true)
{
ssoc.receive(dpac);
String str1=new String(dpac.getData(),0,dpac.getLength());
System.out.println("Received: "+str1);
String str=new String(dt.readLine());
buf=str.getBytes();
ssoc.send(new DatagramPacket(buf,buf.length,ip,32));
}

}
}
Client:

import java.io.*;
import java.net.*;
import java.util.*;
class udpchc
{
public static void main(String ar[]) throws Exception
{
DatagramSocket ssoc=new DatagramSocket(32);
InetAddress ip=InetAddress.getLocalHost();
BufferedReader dt=new BufferedReader(new InputStreamReader(System.in));
byte buf[]=new byte[1024];
DatagramPacket dpac=new DatagramPacket(buf,buf.length);
//String str=new String(dpac.getData());
while(true)
{
String str=new String(dt.readLine());
buf=str.getBytes();
ssoc.send(new DatagramPacket(buf,buf.length,ip,3232));
ssoc.receive(dpac);
String str2=new String(dpac.getData(),0,dpac.getLength());
System.out.println("Received: "+str2);
}


}

}

Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Echo program using UDP

 Earthcare Foundation NGO     03:12     Java Networking     No comments   

Program to send date using UDP Protocol
Server Code:

import java.io.*;
import java.net.*;
import java.util.*;
class udpehs
{
public static void main(String ar[]) throws Exception
{
String clsent,ssent;
DatagramSocket ssoc=new DatagramSocket(3232);
InetAddress ip=InetAddress.getLocalHost();
clsent=(new Date()).toString();
byte buf[]=clsent.getBytes();
ssoc.send(new DatagramPacket(buf,buf.length,ip,32));


ssoc.close();
}
}
Client code:

import java.io.*;
import java.net.*;
import java.util.*;
class udpehc
{
public static void main(String ar[]) throws Exception
{
DatagramSocket ssoc=new DatagramSocket(32);
byte buf[]=new byte[1024];
DatagramPacket dpack=new DatagramPacket(buf,buf.length);
ssoc.receive(dpack);
String df=new String(dpack.getData());
System.out.println(df);


ssoc.close();
}
}
Output:



Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Chating program using TCP

 Earthcare Foundation NGO     03:06     Java Networking     No comments   

Program for chatting using TCP Protocol
Server Code:

import java.io.*;
import java.net.*;
import java.util.*;
class tcpchs
{
public static void main(String ar[]) throws Exception
{
String clsent,ssent;
ServerSocket wsoc=new ServerSocket(3232);
Socket ssoc=wsoc.accept();
BufferedReader ifc=new BufferedReader(new InputStreamReader(ssoc.getInputStream()));
BufferedReader ifs=new BufferedReader(new InputStreamReader(System.in));
DataOutputStream otc=new DataOutputStream(ssoc.getOutputStream());
while(true)
{
clsent=ifc.readLine();
if(clsent.equals("exit"))
{break;}
System.out.println("From Client: "+clsent);
ssent=ifs.readLine();
otc.writeBytes(ssent+'\n');
}
ssoc.close();
}
}
Client Code:

import java.io.*;
import java.net.*;
import java.util.*;
class tcpchc
{
public static void main(String ar[]) throws Exception
{
String clsent,ssent;
Socket ssoc=new Socket("localhost",3232);
BufferedReader ifs=new BufferedReader(new InputStreamReader(ssoc.getInputStream()));
BufferedReader ifc=new BufferedReader(new InputStreamReader(System.in));
DataOutputStream ots=new DataOutputStream(ssoc.getOutputStream());
while(true)
{
clsent=ifc.readLine();
ots.writeBytes(clsent+'\n');
ssent=ifs.readLine();
if(ssent.equals("exit"))
 break;
System.out.println("From Server: "+ssent);
}
ssoc.close();
}
}
Output for client and server:




Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Echo program using TCP

 Earthcare Foundation NGO     02:57     Java Networking     No comments   

Echo Program using TCP Protocol
Program for Server:

import java.io.*;
import java.net.*;
import java.util.*;
class tcpechos
{
public static void main(String ar[]) throws Exception
{
String csent,ssent;
ServerSocket ss=new ServerSocket(1232);
while(true)
{Socket ws=ss.accept();
BufferedReader infromclient=new BufferedReader(new InputStreamReader(ws.getInputStream()));
DataOutputStream outtoclient=new DataOutputStream(ws.getOutputStream());
csent=infromclient.readLine();
System.out.println("From client: "+csent);
ssent=csent;
outtoclient.writeBytes(ssent+'\n');}
}
}
Program for Client:

import java.io.*;
import java.net.*;
import java.util.*;
class tcpechoc
{
public static void main(String ar[]) throws Exception
{
String csent,ssent;
Socket ws=new Socket("LocalHost",1232);
BufferedReader infromserver=new BufferedReader(new InputStreamReader(ws.getInputStream()));
DataOutputStream outtoserver=new DataOutputStream(ws.getOutputStream());
BufferedReader infromclient=new BufferedReader(new InputStreamReader(System.in));
csent=infromclient.readLine();
outtoserver.writeBytes(csent+'\n');
ssent=infromserver.readLine();
System.out.println("From server: "+ssent);
}
}
Output for client and server:



Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Remote Server and Remote Client

 Earthcare Foundation NGO     02:51     Java Networking     No comments   

Program to use Remote Server and Client programs to run any application
Program for Server:

import java.io.*;
import java.net.*;
import java.util.*;
class rms
{
public static void main(String ar[]) throws Exception
{
String csent,ssent;
ServerSocket ss=new ServerSocket(1234);
Socket soc=ss.accept();
BufferedReader ifs=new BufferedReader(new InputStreamReader(System.in));
BufferedReader ifc=new BufferedReader(new InputStreamReader(soc.getInputStream()));
DataOutputStream otc=new DataOutputStream(soc.getOutputStream());
csent=ifc.readLine();
Runtime h=Runtime.getRuntime();
Process p=h.exec(csent+'\n');
System.out.println("Executed successfully");

}
}

Program for Client:

import java.io.*;
import java.net.*;
import java.util.*;
class rmc
{
public static void main(String ar[]) throws Exception
{
String csent,ssent;
Socket soc=new Socket("localhost",1234);
BufferedReader ifc=new BufferedReader(new InputStreamReader(System.in));
BufferedReader ifs=new BufferedReader(new InputStreamReader(soc.getInputStream()));
DataOutputStream ots=new DataOutputStream(soc.getOutputStream());
System.out.println("Enter the command to be executed");
csent=ifc.readLine();
ots.writeBytes(csent+'\n');
ssent=ifs.readLine();
System.out.println("The command executed successfully");
}
}
Output for client and server:






Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Ping Program

 Earthcare Foundation NGO     02:45     Java Networking     No comments   

Program in java for Ping
Program:

import java.io.*;
import java.net.*;
import java.util.*;
class ping
{
public static void main(String ar[]) throws Exception
{
try{
String str;
BufferedReader buf=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the ip address");
String ip=buf.readLine();
Runtime H=Runtime.getRuntime();
Process p=H.exec("ping "+ip);
InputStream in=p.getInputStream();
BufferedReader buf1=new BufferedReader(new InputStreamReader(in));
while((str=buf1.readLine())!=null)
{System.out.println(str);
}}
catch(Exception e)
{System.out.println(e.getMessage());}
}
}
Output:

Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Shortest Distance Program

 Earthcare Foundation NGO     02:37     Java Networking     No comments   

Program to find the shortest distance in the given graph in java.
Program Code:
import java.io.*;
import java.net.*;
import java.util.*;
class dj
{
public static void main(String ar[]) throws Exception
{ int i,j,n,temp,temp1,source,destination,tc,min;
BufferedReader d=new BufferedReader(new InputStreamReader(System.in));
int no[][]=new int[10][10];
System.out.println("Enter the number of nodes");
n=Integer.parseInt(d.readLine());
System.out.println("Enter the transition between the nodes");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
System.out.print("Cost"+i+"-"+j+"-->");

no[i][j]=Integer.parseInt(d.readLine());
System.out.println();
}
}
System.out.println("Enter the source node");
source =Integer.parseInt(d.readLine());
temp1=source;
System.out.println("Enter the destination node");
destination=Integer.parseInt(d.readLine());
tc=0;
for(i=temp1;i<n;i++)
{min=1000;
for(j=0;j<n;j++)
{
temp=no[i][j];
if(i==j||no[i][j]==0)
continue;
else if(min>temp)
{min=temp;temp1=j;}
else if(temp1==destination)
break;
}
no[temp1][i]=0;
no[i][temp1]=0;
tc=tc+min;
}
if(no[source][destination]!=0)
{if(tc>no[source][destination])
tc=no[source][destination];
}
System.out.println("cost ="+tc);
}
Output:

}
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Monday, 8 October 2012

Intermediate Code Generation

 Earthcare Foundation NGO     11:36     C Plus Plus     No comments   

Program to generate intermediate code of any given expression.
Program:

#include<iostream.h>
#include<conio.h>
#include<ctype.h>
main()
{
      int i,k,t,len,b,p,j,c=0,y,o;
      char exp[45],temp[8][50],A[8],stack[8][8];
      char op[10]={'+','*','-','/'};
      char sym[11]={'0','1','2','3','4','5','6','7','8','9'};t=0;j=0;
      cout<<"Enter an  expression\n";
      cin>>exp;
      len=strlen(exp);
      for(i=len;i>=1;i--)
      {
          if(isdigit(exp[i]))
          {
                            while(isdigit(exp[i]))
                            {
                                                 temp[t][i]=exp[i];
                                                 i--;
                                               
                            } exp[i+1]='q';
                            cout<<"After reduction the new expression is  ";
                            for(b=0;b<=i+1;b++)
                            {
                                              cout<<exp[b];
                            }cout<<i<<endl;
          }
          else if(isalpha(exp[i]))
          {
               while(isalpha(exp[i]))
                 {
                    temp[t][i]=exp[i];
                    i--;
                 } exp[i+1]='q';
               
                            cout<<"After reduction the new expression is  ";
                            for(b=0;b<=i+1;b++)
                            {
                                              cout<<exp[b];
                            }cout<<i<<endl;
          }                
         t++;
      }
                                               
      getch();                    
}
Output:
                                      

                                 
                                                                       
                                                                                               

Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Implementation of Lexical Analyzer

 Earthcare Foundation NGO     11:28     C Plus Plus     No comments   

Program to implement lexical analyzer in c++.
Program:

#include<iostream.h>
#include<conio.h>
#include<ctype.h>
#include<string.h>
main()
{
      int i,j,k,f,len,t;
      char exp[100],word[12][12];
      char op[10]={'+','*','-','/'};
      char key[10][10]={"while","do","else","if"};
      cout<<"Enter the expression\n";
      cin>>exp;
      len=strlen(exp);t=0;
      for(i=0;i<len;i++)
      {
          j=0;f=0;
          if(isalpha(exp[i]))
          {
                            while(isalpha(exp[i])||isdigit(exp[i]))
                            {
                                        word[t][j]=exp[i];
                                        i++;j++;
                            }
                            word[t][j]='\0';
                            i--;
                            for(k=0;k<10;k++)
                            {
                                             if(word[t]==key[k])
                                             {
                                                                cout<<"The "<<word[t]<<" is keyword\n";
                                                                f=1;
                                                                break;
                                             }
                            }
                            if(f==0)
                            {
                                       cout<<"The "<<word[t]<<" is identifier\n";
                            }
          }
          else if(isdigit(exp[i]))
          {
                while(isdigit(exp[i]))
                            {
                                        word[t][j]=exp[i];
                                        i++;j++;
                            }
                            i--;
                            cout<<"The "<<word[t]<<" is constant\n";
          }
          else
          {
              cout<<"The "<<exp[i]<<" is operator\n";
          }
          t++;
      }
      getch();
}    
Output:

Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Leading Edges for non terminals

 Earthcare Foundation NGO     02:03     C Plus Plus     No comments   


Program to find the leading edge for non terminals present in the grammar production.
Program Code:


#include<iostream.h>
#include<conio.h>
#include<string.h>
main()
{

int n,j,i,k,b;
char pr[10][10],lead[10],temp;
cout<<"Enter the number of production rules you want to enter\n";
cin>>n;
cout<<"Enter the production rules";
for(i=0;i<n;i++)
{
cin>>pr[i];
}int d=0;
for(i=0;i<n;i++)
{d=0;
               for(j=3;j<7;j++)
               {
                                if(islower(pr[i][j]))
                                {
                                                      lead[pr[i][0]]=pr[i][j];
                                                     d=1;
                                                      break;
                                }
                    }
                    if(d==0)
                    {
                   
                      if(lead[pr[i][j]]!='\0')
                      {
                                             lead[pr[i][0]]=lead[pr[i][3]];
                                             break;
                      }
                    }
                   
                       
   }for(i=0;i<n;i++)
    {
                    cout<<"leading["<<pr[i][0]<<"]="<<lead[pr[i][0]]<<endl;
    }    
   getch();
}
                                                   
                                                     


Output:


                                                    



                                                   
                                                     

Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Trailing Edges For Productions

 Earthcare Foundation NGO     02:02     C Plus Plus     No comments   



Program to find the trailing edge for non terminals present in the grammar production.
Program Code:


#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
main()
{

int n,j,i,len,l;

char pr[10][10],trail[10];
cout<<"Enter the number of production rules you want to enter\n";
cin>>n;
cout<<"Enter the production rules";
for(i=0;i<n;i++)
{
cin>>pr[i];
}
for(i=0;i<n;i++)
{int d=0;
               for(j=strlen(pr[i]);j>2;j--)
               {
                                if(islower(pr[i][j]))
                                {
                                                      trail[pr[i][0]]=pr[i][j];
                                                      d=1;
                                                      break;
                                }
                     }
                   if(d==0)
                   { while(pr[i][j]!='\0')
                    {l=j;j++;}
                           
                       if(trail[pr[i][j]]!='\0')
                      {
                                             trail[pr[i][0]]=trail[pr[i][l]]
                                             ;
                                             break;
                      }
                    }
                   
                       
   }for(i=0;i<n;i++)
    {
                    cout<<"trailing["<<pr[i][0]<<"]="<<trail[pr[i][0]]<<endl;
    }    
   getch();
}
                                                   
                                                     


Output:
                                                  
                                                     

Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Wednesday, 3 October 2012

Dijkastra's Algorithm

 Earthcare Foundation NGO     08:53     C Plus Plus     No comments   


Here is the program to find the shortest path between two nodes using Dijkastra algorithm

import java.io.*;

class MyDijkastra3
{
public static void main(String args[]) throws Exception

{

int n,i,j,tc,temp,current,source,destination,minimum;
int nodes[]=new int[20];
int cost[]=new int[10];
int distance[][]= new int[20][20];
tc=0;temp=0;int f=0;

DataInputStream d = new DataInputStream(System.in);

System.out.println("Enter the number of nodes(max 30)");


n =Integer.parseInt(d.readLine());
System.out.println("Enter the costs to each node(if there is no path between two nodes put distance -1");

for( i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{

System.out.println("Cost"+i+"-"+j+"-->");

distance[i][j] = Integer.parseInt(d.readLine());

}

}

System.out.println("Enter the source node");
source =Integer.parseInt(d.readLine());

System.out.println("Enter the destination node");
destination=Integer.parseInt(d.readLine());
current=source;
minimum=1000;
for( i=current;i<=n;i++)
{
for(j=1;j<=n;j++)
{ if((i==j )|| (distance[i][j]==-1))
                                                                    {continue;
                                                                     }
else if(minimum>=distance[i][j])
{
minimum= distance[i][j];
current=j;
}
if(current==destination)
{System.out.println("Complete");f=1;
break;

}

}tc=tc+minimum;
minimum=10000;
distance[i][current]=-1;
distance[current][i]=-1;

if(f==1)
{
break;
}

}
if((distance[source][destination]!=0)&&(distance[source][destination]!=-1))
{if(distance[source][destination]<tc)
tc=distance[source][destination];
}


                                  System.out.println("The total min cost from"+ source +"to"+ destination +"is:"+tc);

}}


Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Newer Posts Older Posts Home

Ad


Jobsmag.inIndian Education BlogThingsGuide

Subscribe

Do you want fresh and hot updates about us? Then subscribe to our Feeds.

Total Pageviews

Popular Posts

  • Write a program in PL/SQL to print the factorial of a number.
    In this post I will explain how to get the factorial of any given number. For that first you need to know what is the procedure to find ...
  • To find the GCD of two numbers in PROLOG.
    gcd(X,Y):-X=Y,write('GCD of two numbers is '),write(X); X=0,write('GCD of two numbers is '),write(Y); Y=0,write('G...
  • Write a PL/SQL code to get the Fibonacci Sequence
    First, I will explain what is Fibonacci Sequence and how to get this series. So, Fibonacci Sequence is a series of numbers 0,1,1,2,3,5,8,1...

Label

All Articles Best Resources Blogging Boost Traffic Bootstrap C Plus Plus Collection Comedy Comedy Posts Comedy Videos Concurrency creative commons website Education Employee Entertainment Fibonacci Sequence free images GirlFriend Hinglish Cafe How To Image Websites Inspirational Java Java Interview Questions Java Networking Kavita Sangrah Life Lock Sreen Love Number System Patterns Perl Picture PL/SQL Plastic Engineering Programming Prolog public domain SEO Servlet Short Story Shortcut Keys Social Media Social Services SQL SuVichar Thread Traffic True Events Ultimate Guide Windows Tricks Windows8.1 WordPress

Blog Archive

  • ►  2020 (43)
    • ►  September (41)
    • ►  August (2)
  • ►  2019 (1)
    • ►  July (1)
  • ►  2018 (9)
    • ►  September (7)
    • ►  July (1)
    • ►  May (1)
  • ►  2017 (8)
    • ►  June (3)
    • ►  May (3)
    • ►  March (1)
    • ►  January (1)
  • ►  2016 (2)
    • ►  September (1)
    • ►  January (1)
  • ►  2015 (91)
    • ►  December (1)
    • ►  November (1)
    • ►  October (6)
    • ►  May (10)
    • ►  March (20)
    • ►  February (50)
    • ►  January (3)
  • ►  2014 (339)
    • ►  December (1)
    • ►  October (55)
    • ►  September (58)
    • ►  August (94)
    • ►  July (64)
    • ►  June (67)
  • ►  2013 (34)
    • ►  August (5)
    • ►  April (29)
  • ▼  2012 (20)
    • ►  November (1)
    • ▼  October (15)
      • Reading data from a given file
      • Writing data in given file
      • Java Program Using GUI
      • Chating program using UDP
      • Echo program using UDP
      • Chating program using TCP
      • Echo program using TCP
      • Remote Server and Remote Client
      • Ping Program
      • Shortest Distance Program
      • Intermediate Code Generation
      • Implementation of Lexical Analyzer
      • Leading Edges for non terminals
      • Trailing Edges For Productions
      • Dijkastra's Algorithm
    • ►  September (4)

Author

  • Earthcare Foundation NGO
  • Kavita house
  • Unknown
  • Unknown

Info

Copyright © Hub Of Geekz | Powered by Blogger
Design by Hardeep Asrani | Blogger Theme by NewBloggerThemes.com | Distributed By Gooyaabi Templates