Wednesday, May 5, 2010

Write a program to check weather a string accepted by grammar or not.

#include
#include
#include
int main()
{ char ch='y';
char ss[10];
int i, size;
clrscr();
cout<<"\nThe grammar is a*ab";
do
{
cout<<"\nenter the size";
cin>>size ;
cout<<"\nenter the string";
for(i=0;i {
cin>>ss[i];
}
if(ss[size-1]=='b' && ss[size-2]=='a')
{
for(i=0;i<=size-3;i++)
{
if(ss[i]=='a')
cout<<"\n"<<"Correct string:accepted by the grammar";
}
}
else
cout<<"\nincorrect string REJECTED!!!";
cout<<"\npress y to continue...";
cin>>ch;
}
while(ch=='y'|| ch=='Y');
getch();
return 0;
}

Write a program to convert NDFA to DFA.

#include«Iostream.h»
#include«conio.h»
#include«stdlib.h»
void main()
{
clrscr();
int n=10,A[10][2],init,final;
int temp[10][2];
cout««"Enter no of input";
cin»»n;
for(int i=0;i«n;i++)
{
cout««"Enter transition state for "««i+1««" node";
cout««"(Enter 11 for NULL)\n";
cout««"For a =";
cin»»A[i][1];
cout««"For b =";
cin»»A[i][2];

}
clrscr();
cout««"Entered NFA is\n";
cout««"STATE\t\t"««"a\t"««"b";
for(i=0;i«n;i++)
{ cout««"\nq"««i+1««"\t\t"««"q"««A[i][1]««"\t"««"q"««A[i][2];
}
cout««"\nEnter initial state: ";
cin»»init;
cout««"\nEnter final state: ";
cin»»final;
cout««"\n\n";
cout««" DFA is \n";
cout««"STATE\t\t"««"a\t"««"b\n";
cout««"q"««init««"\t\t"««"q"««A[init-1][1]««"\t"««"q"««A[init-1][2];
temp[init][1]=A[init][1];
temp[init][2]=A[init][2];
getch();
}

Write a program to convert Regular Expression to NFA.

#include
#include
#include
#include

void main()
{
clrscr();
char c[20];
int a[10][2];
int l,n=0,s=0,t;
cout<<"Enter the string:";
cin>>c;
l=strlen(c);
for(int f=0;f {
if(c[f]=='+')
{
s++;
}
}
n=s+1;
cout<<"Thus states="< for(int i=0;i {
t=1;
a[i][1]=t;
a[i][2]=(t-1);
t++;
if(i>=1)
{
a[i][2]=n-2;
if(i==2)
{
a[i][1]=n-1;
a[i][2]=n-1;
}
}
}
cout<<"\n\n\tNFA\n\n";
cout<<"state \t"<<"a\t"<<"b\t";
for(int j=0;j {
cout<<"\nq"< if(j==1)
{
cout<<",q"< }
cout<<"\t"<<"q"< }
getch();
}

Write a program to convert an infix expression to postfix expression.

#include
#include
#include
#include
#define operand(x)(x>='a'&&x<='z' || x>='A'&&x<='Z' || x>='0'&&x<='9')
char infix[30], postfix[30], stack[30];
int top,i=0;

void init()
{
top=-1;
}

void push(char x)
{
stack[++top]=x;
}

char pop()
{
return( stack[top--] );
}

int isp(char x)
{
int y;
y=(x=='('?6:x=='^'?4:x=='*'?2:x=='/'?2:x=='+'?1:x=='-'?1:x==')'?0:-1);
return y;
}

int icp(char x)
{
int y;
y=(x=='('?6:x=='^'?4:x=='*'?2:x=='/'?2:x=='+'?1:x=='-'?1:x==')'?4:-1);
return y;
}

void infixtopostfix()
{
int j,l=-1;
char x,y;
stack[++top]='\0';
for (j=0; j<=strlen(infix)-1; j++)
{
x=infix[j];
if (operand(x))
postfix[++l]=x;
else if (top=='\0')
push(x);
else
{
while (isp(stack[top]) >= icp(x) )
postfix[++l]=pop();
push(x);
}
}
while (top!='\0')
postfix[++l]=pop();
postfix[++l]='\0';
}

void main()
{
clrscr();
init();
cout<<"Enter an infix expression :\n";
gets(infix);

infixtopostfix();
cout<<"The resulting postfix expression is: "< getch();
}

Write program to convert an infix expression to prefix expression.

#include
#include
#include
#define operand(x)(x>='a'&&x<='z' || x>='A'&&x<'Z' || x<'0'&&x<='9')
char infix[30],prefix[30],stack[30];
int top,i=0;

void init()
{
top=-1;
}

void push(char x)
{
stack[++top]=x;
}

char pop()
{
return(stack[top--]);
}

int isp(char x)
{
int y;
y=(x=='('?6:x=='^'?4:x=='*'?2:x=='/'?1:x=='-'?1:x==')'?0:-1);
return y;
}
int icp(char x)
{
int y;
y=(x=='('?6:x=='^'?4:x=='*'?2:x=='/'?1:x=='-'?1:x==')'?4:-1);
return y;
}
void infixtoprefix()
{
int j,l=0;
char x,y;
stack[++top]='\0';
for (j=strlen(infix)-1;j>=0;j--)
{
x=infix[j];
if (operand(x))
prefix[l++]=x;
else
if(x=='(')
while((y=pop())!=')')
prefix[l++]=y;
else
{
while(isp(stack[top])>=icp(x))
prefix[l++]=pop();
push(x);
}
}

while(top>=0)
prefix[l++]=pop();
}

void main()
{
init();
printf("enter the infix expression :\n");
scanf("%s",infix);
infixtoprefix();
strrev(prefix);
printf("The resulting prefix expression is %s",prefix);
getch();
}

Tuesday, May 4, 2010

WAP that implements a JApplet that displays a simple label.

import java.awt.*;
import javax.swing.*;
import java.applet.*;
/*
Applet code is here...you should do urself..
*/
public class Class4 extends JApplet
{
public void init()
{
Container cp=getContentPane();
JLabel j=new JLabel("Hello");
cp.add(j);
cp.setVisible(true);
}

}

WAP to display 6 buttons on a panel using JFrame.

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

class Class2 extends JFrame
{
public static void main(String s[])
{
JButton b1,b2,b3,b4,b5,b6;
JPanel p=new JPanel();
b1=new JButton("1");
b2=new JButton("2");
b3=new JButton("3");
b4=new JButton("4");
b5=new JButton("5");
b6=new JButton("6");
p.add(b1);
p.add(b2);
p.add(b3);
p.add(b4);
p.add(b5);
p.add(b6);
JFrame f=new JFrame("Demo");
f.getContentPane().add(p);
f.setSize(300,300);
f.setVisible(true);
}}

wap to validate the user id and password


/*

* ser_reqs.java

*

* Created on March 18, 2009, 9:47 AM

*/

package com;

import java.io.*;

import java.net.*;

import java.sql.*;

import javax.servlet.*;

import javax.servlet.http.*;

/**

*

* @author Administrator

* @version

*/

public class ser_reqs extends HttpServlet {

/** Initializes the servlet.

*/

String userid;

String password;

Connection dbconnection;

ResultSet resultset;

public void init() throws ServletException {

ServletConfig config = getServletConfig();

String driverClassName = config.getInitParameter("driverclassname");

String dbURL = config.getInitParameter("dburl");

String username = config.getInitParameter("username");

String dbpassword = config.getInitParameter("dbpassword");

try

{

Class.forName(driverClassName);

}

catch(ClassNotFoundException cnfe)

{

System.err.println("Error loading driver: " + cnfe);

}

try

{

dbconnection = DriverManager.getConnection(dbURL, username, dbpassword);

}

catch(SQLException sqle)

{

System.err.println("Connection error: " + sqle);

}

}

/** Destroys the servlet.

*/

public void destroy() {

}

/** Processes requests for both HTTP GET and POST methods.

* @param request servlet request

* @param response servlet response

*/

protected void processRequest(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("text/html");

PrintWriter out = response.getWriter();

// String s1=request.getParameter("t1");

// String ps=request.getParameter("p1");

try

{

Statement statement = dbconnection.createStatement();

String sqlString = "SELECT * FROM st_record WHERE name='"+request.getParameter("t1")+"'AND password='"+request.getParameter("p1")+"'";

resultset=statement.executeQuery(sqlString);

while(resultset.next())

{

userid = resultset.getString("name");

password = resultset.getString("password");

}

}

catch(SQLException sqle)

{

System.err.println("Connection error: " + sqle);

}

if(password.equals(request.getParameter("p1")) && userid.equals(request.getParameter("t1")))

{

response.sendRedirect("/ser_req/corret.html");

}

else

{

response.sendRedirect("/ser_req/incorret.html");

}

//TODO output your page here

out.println("");

out.println("");

out.println("Servlet");

out.println("");

out.println("");

// out.println(s1);

// out.println(ps);

out.println("");

out.println("");

out.close();

}

/** Handles the HTTP GET method.

* @param request servlet request

* @param response servlet response

*/

protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

processRequest(request, response);

}

/** Handles the HTTP POST method.

* @param request servlet request

* @param response servlet response

*/

protected void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

processRequest(request, response);

}

/** Returns a short description of the servlet.

*/

public String getServletInfo() {

return "Short description";

}


Rest of program do urself....

Wap to implement Servlet

import java.io.*;
import java.net.*;

import javax.servlet.*;
import javax.servlet.http.*;

/**
*
* @author student
*/
public class NewServlet extends HttpServlet {

/**
* Processes requests for both HTTP GET and POST methods.
* @param request servlet request
* @param response servlet response
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("Welcome "+ request.getParameter("t1"));
}



protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { processRequest(request, response);

}

/**
* Handles the HTTP POST method.
* @param request servlet request
* @param response servlet response
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

/**
* Returns a short description of the servlet.
*/
//
}

You Have to do the jsp code by urself...

Program to implement Prepared Statement in JDBC

import java.sql.*;
import java.lang.*;
public class pst
{public static void main(String args[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String query="select *from employee where salary>20000";
Connection con=DriverManager.getConnection("jdbc:odbc:name","","");
PreparedStatement pst = con.prepareStatement(query);
ResultSet rs1 = pst.executeQuery();

while(rs1.next())
{
System.out.println(rs1.getString(1)+ "\t" +rs1.getString(2)+ "\t" +rs1.getString(3)+ "\t" +rs1.getString(4)+
"\t" +rs1.getString(5));
}
}
catch(Exception e)
{System.out.println("error " +e);
}
}
}

To implement Remote Method Invocation(RMI) (Client Server Application)

Product.java

import java.rmi.*;

public interface product extends Remote{

public String getname() throws RemoteException;

}

Productimpl.java

import java.rmi.*;

import java.rmi.server.*;

public class productimpl extends UnicastRemoteObject implements product

{

public String name1;

productimpl(String name) throws RemoteException

{

name1=name;

}

public String getname()

{

return name1;

}

}

Productclient.java

import java.rmi.*;

import java.rmi.registry.*;

public class productclient

{

public static void main(String s[])

{

try

{

product p=(product)Naming.lookup("rmi://127.0.0.1:1099/java");

System.out.println("productname="+p.getname());

}

catch(Exception e)

{

System.out.println(e);

}

}

}

Productserver.java

import java.rmi.*;

import java.rmi.registry.*;

public class productserver

{

public static void main(String s[])

{

try{

LocateRegistry.getRegistry(1099);

productimpl p1 =new productimpl("java");

Naming.bind("java",p1);

}

catch(java.lang.Exception e)

{System.out.println(e);

}

}

}

JDBC programing....

import java.sql.*;
/**
*
* @author Administrator
*/
public class jdbc_test {

/** Creates a new instance of jdbc_test */
public jdbc_test() {
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here

Connection con1=null;
Statement stmt1=null;
ResultSet rs1=null;
//PreparedStatement pstmt=null;
try{
Class.forName("com.mysql.jdbc.Driver");
}catch(ClassNotFoundException cnfe){ }
try{
con1 = DriverManager.getConnection("jdbc:mysql://localhost/test","root","asecse");
stmt1 = con1.createStatement();
rs1 = stmt1.executeQuery("select *FROM student");

while(rs1.next()){

System.out.println(rs1.getString("name"));

}

}catch(SQLException sqle){ }

}}

Monday, May 3, 2010

Q.WAP to create a client server application in which the client sends some string to the server and server responds by converting the string to upper

//Client:

import java.net.*;
import java.io.*;
class LocalClient1
{
public static void main(String s[]) throws IOException,UnknownHostException
{
String sin, sout;
Socket s=new Socket("localhost",1010);
BufferedReader bclient=new BufferedReader(new InputStreamReader(System.in));
DataOutputStream dos=new DataOutputStream(s.getOutputStream());
BufferedReader bserver=new BufferedReader(new InputStreamReader(s.getInputStream()));
sin=bclient.readLine();
dos.writeBytes(sin+"\n");
sout=bserver.readLine();
System.out.println("Output String:"+sout);
s.close();
}
}


//Server:

import java.net.*;
import java.io.*;
class LocalServer
{
public static void main(String s[]) throws IOException,UnknownHostException
{
String s1, s2;
SreverSocket ss=new ServerSocket(1010);
System.out.println("Listen...");
while(true)
{
Socket s=ss.accept();
BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
DataOutputStream d=new DataOutputStream(s.getOutputStream());
s1=br.readLine();
s2=s1.toUpperCase();
d.WriteBytes(s2+"\n");
s.close();
}

Q. WAP to obtain IP address of localhost and any well known website

import java.net.*;
import java.io.*;

class prg1{
static void p(String s)
{
System.out.println(s);
}

public static void main(String s[]) throws UnknownHostException, IOException
{
InetAddresss i,o[];
i=InetAddress.getLocalHost();
p("local host"+i);
i=InetAddress.getByName("10.0.56.80");
p("10.0.56.80"+i);
o=InetAddress.getAllByName("www.ndtv.com");
for(int j=0;j{
p("ndtv ip address"+j+" "+o[j]);
}
}
}

Q: To implement two way chatting.


Client Side:
import java.io.*;
import java.net.*;
class client
{
static void p(String s)
{
System.out.println(s);
}
public static void main(String args[])throws UnknownHostException,IOException
{
String s1,s2;
Socket s=new Socket("LocalHost",1010);
while(true)
{
BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
BufferedReader bclient=new BufferedReader(new InputStreamReader(System.in));
DataOutputStream dos=new DataOutputStream(s.getOutputStream());
s2=bclient.readLine();
dos.writeBytes(s2+'\n');
s1=bclient.readLine();
p(s1);
}
}
}
Server Side:
import java.net.*;
importjava.io.*;
class server
{
static void p(String s)
{
System.out.println(s);
}
public static void main(String args[])throws UnknownHostException,IOException
{
String s1,s2;
ServerSocket ss=new ServerSocket("LocalHost",1010);
while(true)
{
Socket s=ss.accept();
BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
BufferedReader bserser=new BufferedReader(new InputStreamReader(System.in));
DataOutputStream dos=new DataOutputStream(s.getOutputStream());
s1=br.readLine();
p(s1);
s2=bserver.readLine();
dos.writeBytes(s2+'\n');
}
}
}

Q: To implemet one way chatting.

Client side:
import java.io.*;
import java.net.*;
class client
{
static void p(String s)
{
System.out.println(s);
}
public static void main(String args[])throws UnknownHostException,IOException
{
String s1,s2;
Socket s=new Socket("LocalHost",1010);
BufferedReader bclient=new BufferedReader(new InputStreamReader(System.in));
DataOutputStream dos=new DataOutputStream(s.getOutputStream());
p("say..");
s1=bclient.readLine();
dos.writeBytes(s1+'\n');
}
}
Server Side:
import java.net.*;
importjava.io.*;
class server
{
static void p(String s)
{
System.out.println(s);
}
public static void main(String args[])throws UnknownHostException,IOException
{
String s1,s2;
ServerSocket ss=new ServerSocket("LocalHost",1010);
while(true)
{
Socket s=ss.accept();
BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
s1=br.readLine();
p(s1);
}
}
}

Q: Write a program that brings the address and the name of the local machine.

import java.net.*;

class ab

{

public static void main(String args[]) throws UnknownHostException

{

InetAddress ad = InetAddress.getLocalHost();

System.out.println(ad);

}

}

Q: To obtain a character using a BufferedReader object.

import java.io.*;

class charprint

{

public static void main(String args[])

{

char ch,ch1='y';

BufferedReader br;

try

{

do

{

System.out.println("Enter a character... ");

br=new BufferedReader(new InputStreamReader(System.in));

ch=(char)br.read();

System.out.println("Character entered... "+ch);

System.out.println("Continue?... (y/n)... ");

br=new BufferedReader(new InputStreamReader(System.in));

ch1=(char)br.read();

}while(ch1=='y' || ch1=='Y');

}

catch(IOException e)

{}

}

}