Natchatran

NATCHATRAN

Natchatran Blogs includes Technical Tutorials, E-books, Notes, Lab Manual, Question Banks, Viva questions and Interview questions for engineering students, provides all study material for cse students.

-Natchatran(Prem Anandh.J)

Tuesday, July 30, 2013

CS2309 - JAVA LAB - Manual

1. RATIONAL NUMBER
                       

PROGRAM:
import java.io.*;
class rational
{
private float num;
private float den;
public rational(float x, float y)         
{
num=x;
den=y;                 
}
public String toString()
{
return num+"/"+den;
}
public void rf()
{
if(num<den)
{
for(int i=2;i<=num;i++)
{
while((num%i)==0&&(den%i)==0)
{
num=num/i;
den=den/i;
}
}
}
else
{
for(int i=2;i<=den;i++)
{
while((num%i)==0&&(den%i)==0)
{
num=num/i;
den=den/i;
}
}
}
}
public rational add(rational r)
{
rational result=new rational(0,0);
result.den=den*r.den;
result.num=r.den*num+r.num*den;
return result;
}

public static void main(String a[])throws IOException
{
BufferedReader  in=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the rational number1 in the form a/b:");
String relstr1=in.readLine();
System.out.print("Enter the rational number2 in the form a/b:");
String relstr2=in.readLine();
float[] num1=new float[2];
float[] num2=new float[2];
try
{
String[] numstr=new String[2];
numstr=relstr1.split("/");
for(int i=0;i<2;i++)
{
num1[i]=Float.parseFloat(numstr[i]);        
}
numstr=new String[2];
numstr=relstr2.split("/");
for(int i=0;i<2;i++)
{
num2[i]=Float.parseFloat(numstr[i]);        
}
rational rel1=new rational(num1[0],num1[1]);
rational rel2=new rational(num2[0],num2[1]);
rel1.rf();
                             rel2.rf();
rational reladd=rel1.add(rel2);
reladd.rf();
System.out.println("Reduced form of Relational number1:"+rel1.toString());
System.out.println("Reduced form of Relational number2:"+rel2.toString());
System.out.println("Reduced form of Addition:"+reladd.toString());
}
catch(Exception e)
{
System.out.println("Wrong insert");
}
}
}
OUTPUT
E:\Java>javac rational.java
E:\Java>java rational
Enter the rational number1 in the form a/b:123/321
Enter the rational number2 in the form a/b:789/987
Reduced form of Relational number1:41.0/107.0
Reduced form of Relational number2:263.0/329.0
Reduced form of Addition:41630.0/35203.0

E:\Java>




 


2. DATE CLASS
PROGRAM:
import java.util.*;
class Date1
{
private int date=0;
private int month=0;
private int year=0;
public int getyear()
{
return year;
}
public int getmonth()
{
return month;
}
public int getdate()
{
return date;
}
public Date1()
{
Calendar c=Calendar.getInstance();
date=c.get(Calendar.DAY_OF_MONTH);
month=c.get(Calendar.MONTH);
year=c.get(Calendar.YEAR);
}
public Date1(int d,int m,int y)
{
date=d;
month=m;
year=y;
}
public String toString()
{
String ret=" "+date+"/"+month+"/"+year;
return ret;
}
public static void main(String args[])
{
Date1 d=new Date1();
System.out.println("Today’s Date is "+d);
}
}
OUTPUT:
E:\Java>javac Date1.java

E:\Java>java Date1
Today’s Date is 27/5/2002

E:\Java>








                                                            3. LISP LIKE LIST
 


PROGRAM
import java.io.*;
import java.util.*;
class lisp1
{
private Vector <Integer> elements;
public lisp1()
{
elements=new Vector<Integer>();
}
public void push(int elem)
{
elements.add(elem);
}
public int car()
{
try
{
int a=elements.firstElement();
return a;
}
catch(NoSuchElementException e)
{
System.out.println("Lisp is empty");
return 0;
}
catch(Exception e)
{
System.out.println(e);
return 0;
}
}
public int[] cdr()
{
int r[]=new int[elements.size()];
ListIterator iter = elements.listIterator(0);
int i=0;
while (iter.hasNext())
r[i++] = (Integer)iter.next();
return r;
          }
public int[] cons()
{
int r[]=new int[elements.size()];
ListIterator iter = elements.listIterator(r.length);
int i=0;
while (iter.hasPrevious())
r[i++]=(Integer)iter.previous();
return r;
}
public static void main(String args[])
{
int j;
lisp1 l=new lisp1();
int count,value;
Scanner in=new Scanner(System.in);
System.out.println("Enter the no.of elements");
count=in.nextInt();
for(j=0;j<count;j++)
{
System.out.println("Enter the  "+j+" element");
value=in.nextInt();
l.push(value);
}
System.out.println("Car=> " + l.car());
System.out.println("Cdr=> ");
int nums[]=l.cdr();
for(int i=1;i<nums.length;i++)
System.out.println(""+i+" -> "+ nums[i]);
System.out.println("Cons=> ");
int all[]=l.cons();
for(int i=0;i<all.length;i++)
System.out.println(""+i+" -> "+ all[i]);
}
}

OUTPUT
E:\Java>javac lisp1.java

E:\Java>java lisp1
Enter the no.of elements
5
Enter the  0 element
1
Enter the  1 element
2
Enter the  2 element
3
Enter the  3 element
4
Enter the  4 element
5
Car=> 1
Cdr=>
1 -> 2
2 -> 3
3 -> 4
4 -> 5
Cons=>
0 -> 5
1 -> 4
2 -> 3
3 -> 2
4 -> 1

E:\Java>


4. ADT STACK
PROGRAM
import java.util.*;
interface Stack_interface
{
public void push(int val)throws Exception;
public int pop()throws Exception;
public boolean isEmpty();
public String toString();
}        
class Stack_array implements Stack_interface
{
private int[]array;
private int top=-1;
private int array_max=-1;
public void push(int val)throws Exception
{
if(top==array_max)
{
throw new Exception("Stack overflow");
}
top++;
array[top]=val;
}
public int pop() throws Exception
{
if(top==-1)
{
throw new Exception("Stack underflow");
}
int ret=array[top];
top--;
return ret;
}
public boolean isEmpty()
{
if(top==-1)
return true;
else
return false;
}
public String toString()
{
String message="STACK=> ";
for(int i=0;i<=top;i++)
message+=array[i]+" ";
return message;
}
public Stack_array(int max)
{
array_max=max;
array=new int[array_max];
}
}
class Stack_list implements Stack_interface
{
private LinkedList<Integer>list;
public void push(int val)throws Exception
{
try
{
list.addFirst(val);
}
catch(Exception e)
{
throw e;
}
}
public int pop()throws Exception
{
try
{
return(int)list.removeFirst();
}
catch(Exception e)
{
throw e;
}
}
public boolean isEmpty()
{
return list.isEmpty();
}
public Stack_list()
{
list=new LinkedList<Integer>();
}
public String toString()
{
String message="STACK=> ";
for(int i=0;i<list.size();i++)
message+=list.get(i)+" ";
return message;
}
}
public class StackADT
{
public static void main(String args[])
{
try
{
int x,y,ch;
String ans;
Scanner in=new Scanner(System.in);
Stack_array b=new Stack_array(10);
Stack_list a=new Stack_list();
while(true)
{
try
{
System.out.println("1.PUSH BY LINKED LIST"+"\n"+"2.POP BY LINKED LIST");
System.out.println("3.PUSH BY ARRAY"+"\n"+"4.POP BY ARRAY"+"\n"+"5.EXIT"+"\n");
System.out.println("Enter ur choice");
ch=in.nextInt();
switch(ch)
{
case 1:
System.out.println(" Enter the elements to be pushed by linked list");
x=in.nextInt();
a.push(x);
System.out.println("pushed item by linked list is "+a);
break;
case 2:
System.out.println("popped item by linked list is"+a.pop());
break;
case 3:
System.out.println("Enter the elements to be pushed using array");
y=in.nextInt();
b.push(y);
System.out.println("pushed item by using array is"+b);
break;
case 4:
System.out.println("poped item by array is"+b.pop());
break;
case 5:
System.exit(0);
break;
default:
System.out.println("Enter the correct choice");
}
}
catch(Exception e)
{
System.err.println("try again("+e+")");
}
}
}
catch(Exception e)
{
System.err.println(e);
}
}
}

OUTPUT
E:\Java>javac StackADT.java
E:\Java>java StackADT
1.PUSH BY LINKED LIST
2.POP BY LINKED LIST
3.PUSH BY ARRAY
4.POP BY ARRAY
5.EXIT

Enter ur choice
1
 Enter the elements to be pushed by linked list
1
pushed item by linked list is STACK=> 1
1.PUSH BY LINKED LIST
2.POP BY LINKED LIST
3.PUSH BY ARRAY
4.POP BY ARRAY
5.EXIT

Enter ur choice
1
 Enter the elements to be pushed by linked list
2
pushed item by linked list is STACK=> 2 1
1.PUSH BY LINKED LIST
2.POP BY LINKED LIST
3.PUSH BY ARRAY
4.POP BY ARRAY
5.EXIT

Enter ur choice
1
 Enter the elements to be pushed by linked list
3
pushed item by linked list is STACK=> 3 2 1
1.PUSH BY LINKED LIST
2.POP BY LINKED LIST
3.PUSH BY ARRAY
4.POP BY ARRAY
5.EXIT

Enter ur choice
2
popped item by linked list is3
1.PUSH BY LINKED LIST
2.POP BY LINKED LIST
3.PUSH BY ARRAY
4.POP BY ARRAY
5.EXIT

Enter ur choice
2
popped item by linked list is2
1.PUSH BY LINKED LIST
2.POP BY LINKED LIST
3.PUSH BY ARRAY
4.POP BY ARRAY
5.EXIT

Enter ur choice
2
popped item by linked list is1
1.PUSH BY LINKED LIST
2.POP BY LINKED LIST
3.PUSH BY ARRAY
4.POP BY ARRAY
5.EXIT

Enter ur choice
2
try again(java.util.NoSuchElementException)
1.PUSH BY LINKED LIST
2.POP BY LINKED LIST
3.PUSH BY ARRAY
4.POP BY ARRAY
5.EXIT

Enter ur choice
3
Enter the elements to be pushed using array
1
pushed item by using array isSTACK=> 1
1.PUSH BY LINKED LIST
2.POP BY LINKED LIST
3.PUSH BY ARRAY
4.POP BY ARRAY
5.EXIT

Enter ur choice
3
Enter the elements to be pushed using array
2
pushed item by using array isSTACK=> 1 2
1.PUSH BY LINKED LIST
2.POP BY LINKED LIST
3.PUSH BY ARRAY
4.POP BY ARRAY
5.EXIT

Enter ur choice
3
Enter the elements to be pushed using array
3
pushed item by using array isSTACK=> 1 2 3
1.PUSH BY LINKED LIST
2.POP BY LINKED LIST
3.PUSH BY ARRAY
4.POP BY ARRAY
5.EXIT

Enter ur choice
4
poped item by array is3
1.PUSH BY LINKED LIST
2.POP BY LINKED LIST
3.PUSH BY ARRAY
4.POP BY ARRAY
5.EXIT

Enter ur choice
4
poped item by array is2
1.PUSH BY LINKED LIST
2.POP BY LINKED LIST
3.PUSH BY ARRAY
4.POP BY ARRAY
5.EXIT

Enter ur choice
4
poped item by array is1
1.PUSH BY LINKED LIST
2.POP BY LINKED LIST
3.PUSH BY ARRAY
4.POP BY ARRAY
5.EXIT

Enter ur choice
4
try again(java.lang.Exception: Stack underflow)
1.PUSH BY LINKED LIST
2.POP BY LINKED LIST
3.PUSH BY ARRAY
4.POP BY ARRAY
5.EXIT
Enter ur choice
5
E:\Java>

 


                                      5. VECHICLE CLASS HIERARCHY

PROGRAM
import java.io.*;
import java.util.*;
abstract class vehicle
{
String name;
double weight;
int wheels;
abstract void display();
}
class car extends vehicle
{
public int passenger;
public car(String name,int wheels,int passenger,double weight)
{
this.name=name;
this.weight=weight;
this.passenger=passenger;
this.wheels=wheels;
}
public void display()
{
System.out.println("Name:"+name);
System.out.println("Weight:"+weight);
System.out.println("Passenger:"+passenger);
System.out.println("Wheels:"+wheels);
}
}
class truck extends vehicle
{
public double payload;
public truck(String name,int wheels,double payload,double weight)
{
this.name=name;
this.weight=weight;
this.payload=payload;
this.wheels=wheels;
}
public void display()
{
System.out.println("Name:"+name);
System.out.println("Weight:"+weight);
System.out.println("Payload:"+payload);
System.out.println("Wheels:"+wheels);
}
}
class test
{
public static void main(String args[])throws IOException
{
System.out.println("Car details");
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
String s1=in.readLine();
String s2=in.readLine();
int x=Integer.parseInt(s2);
String s3=in.readLine();
int y=Integer.parseInt(s3);
String s4=in.readLine();
double z=Double.parseDouble(s3);
car cr1=new car(s1,x,y,z);
cr1.display();
System.out.println("Truck details");
BufferedReader is=new BufferedReader(new InputStreamReader(System.in));
String s5=is.readLine();
String s6=is.readLine();
int e=Integer.parseInt(s2);
String s7=is.readLine();
double r=Double.parseDouble(s3);
String s8=is.readLine();
double t=Double.parseDouble(s3);
truck trk=new truck(s5,e,r,t);
trk.display();
}
}

OUTPUT
E:\Java>javac test.java
E:\Java>java test
Car details - Name, Wheels, Passenger, Weight
Ford
4
4
500
Name:Ford
Weight:500.0
Passenger:4
Wheels:4
Truck details - Name, Wheels, Payload, Weight
Bolero
4
4
500
Name:Bolero
Weight:500.0
Payload:4.0
Wheels:4
E:\Java>
                            

6. OBJECT SERIALIZATION

PROGRAM
abstract class Currency implements java.io.Serializable
{
protected final double rupeesperDollar=45;
protected double value;
public Currency(double val)
{
value=val;
}
abstract public String toString();
abstract public double convert();
}
class Dollar extends Currency
{
public Dollar(double val)
{
super(val);
}
public String toString()
{
return "$"+value;
}
public double convert()
{
return value*rupeesperDollar;
}
}
class Rupee extends Currency
{
public Rupee(double val)
{
super(val);
}
public String toString()
{
return "Rs"+value;
}
public double convert()
{
return value/rupeesperDollar;
}
}
import java.util.Random;
import java.io.*;
public class WriteCurrency
{
public static void main(String args[])
{
try
{
int numberOfElements=10;
ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream("Currency"));
Random randomGenerator=new Random();
out.writeInt(numberOfElements);
for(int i=0;i<numberOfElements;i++)
{
if(randomGenerator.nextBoolean())
{
Rupee r = new Rupee(randomGenerator.nextInt(100));
out.writeObject(r);
System.out.println("("+i+")"+r);
}
else
{
Dollar d=new Dollar(i);
out.writeObject(d);
System.out.println("("+i+")"+d);
}
}
out.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
import java.io.*;
public class ReadCurrency
{
public static void main(String args[])throws Exception
{
try
{
ObjectInputStream in=new ObjectInputStream(new FileInputStream("Currency"));
int  numberOfElements=in.readInt();
for(int i=0;i<numberOfElements;i++)
{
Object o=in.readObject();
System.out.print("("+i+")");
if(o instanceof Dollar) //can also use Dollar.class.is.Instance(d)
System.out.println("Rs"+((Dollar)o).convert()+"(converted from Dollar)");
else
System.out.println((Rupee)o);
}
in.close();
}
catch(FileNotFoundException e)
{
System.out.println("Please run write Currency first");
}
catch(Exception e)
{
System.out.println(e);
}
}
}

OUTPUT
E:\Java>javac Currency.java
E:\Java>javac WriteCurrency.java
E:\Java>javac ReadCurrency.java
E:\Java>java WriteCurrency
(0)Rs30.0
(1)Rs54.0
(2)Rs17.0
(3)$3.0
(4)Rs50.0
(5)Rs10.0
(6)$6.0
(7)Rs74.0
(8)Rs23.0
(9)Rs73.0
E:\Java>java ReadCurrency
(0)Rs30.0
(1)Rs54.0
(2)Rs17.0
(3)Rs135.0(converted from Dollar)
(4)Rs50.0
(5)Rs10.0
(6)Rs270.0(converted from Dollar)
(7)Rs74.0
(8)Rs23.0
(9)Rs73.0
E:\Java>

 


                              7. SCIENTIFIC CALCULATOR
PROGRAM
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class calculatorapplet extends Applet implements ActionListener
{
private Button keysArray[];
private Panel keyPad;
private TextField lcdField;
private double result;
private boolean first;
private boolean foundKey;
static boolean clearText;
private int prevOperator;
public void init()
{
lcdField=new TextField(20);
keyPad=new Panel();
keysArray=new Button[17];
result=0.0;
prevOperator=0;
first=true;
lcdField.setEditable(false);
for(int i=0;i<=9;i++)
keysArray[i]=new Button(String.valueOf(i));
keysArray[10]=new Button("/");
keysArray[11]=new Button("*");
keysArray[12]=new Button("-");
keysArray[13]=new Button("+");
keysArray[14]=new Button("=");
keysArray[15]=new Button(".");
keysArray[16]=new Button("CLR");
keyPad.setLayout(new GridLayout(4,4));

for(int i=7;i<=10;i++)
keyPad.add(keysArray[i]);

for(int i=4;i<=6;i++)
keyPad.add(keysArray[i]);
keyPad.add(keysArray[11]);

for(int i=1;i<=3;i++)
keyPad.add(keysArray[i]);
keyPad.add(keysArray[12]);
keyPad.add(keysArray[0]);

for(int i=15;i>=13;i--)
keyPad.add(keysArray[i]);

add(lcdField,BorderLayout.NORTH);
add(keyPad,BorderLayout.CENTER);
add(keysArray[16],BorderLayout.EAST);

for(int i=0;i<keysArray.length;i++)
keysArray[i].addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
foundKey=false;
for(int i=0;i<keysArray.length&&!foundKey;i++)
if(e.getSource()==keysArray[i])
{
foundKey=true;
switch(i)
{
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
case 15:
if(clearText)
{
lcdField.setText("");
clearText=false;
}
lcdField.setText(lcdField.getText()+
keysArray[i].getLabel());
break;
case 10:
case 11:
case 12:
case 13:
case 14:
clearText=true;
if(first)
{
if(lcdField.getText().length()==0)
result=0.0;
else
result=Double.valueOf(lcdField.getText()).doubleValue();
first=false;
prevOperator=i;
}
else
{
switch(prevOperator)
{
case 10:
result/=Double.valueOf(lcdField.getText()).doubleValue();
break;
case 11:
result*=Double.valueOf(lcdField.getText()).doubleValue();
break;
case 12:
result-= Double.valueOf (lcdField.getText()).doubleValue();
break;
case 13:
result+=Double.valueOf (lcdField.getText()).doubleValue();
break;
}
lcdField.setText(Double.toString(result));
if(i==14)
first=true;
else
prevOperator=i;
}
break;
case 16:
clearText=true;
first=true;
lcdField.setText("");
result=0.0;
prevOperator=0;
break;
}
}
}
}
//<applet code="calculatorapplet.class" width=300 height=200></applet>
OUTPUT
E:\Java>javac calculatorapplet.java
E:\Java>appletviewer calculatorapplet.java
E:\Java>


 



                                      8. MULTI THREADING USING PIPES

PROGRAM
import java.io.*;
class global
{
public static int max=0;
public static final int size_of_int=4;
public static int getInt(PipedInputStream pi)
{
byte[] b=new byte[4];
try
{
if(4!=pi.read(b,0,size_of_int))
return -1;
}
catch(IOException e)
{
return -1;
}
catch(Exception e)
{
e.printStackTrace();
System.exit(0);
return -1;
}
int v=0;
v=(b[0]<<24)+((b[1]&0xFF)<<16)+((b[2]&0xFF)<<8)+((b[3]&0xFF));
return v;
}
public static byte[] getBytes(int v)
{
byte b[]=new byte[]{(byte)(v>>>24),(byte)(v>>>16),(byte)(v>>>8),(byte)(v)};
return b;
}
}
class Fibbinocci extends global implements Runnable
{
PipedOutputStream po;
public Fibbinocci(PipedInputStream pi)
{
try
{
po=new PipedOutputStream(pi);
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void run()
{
int prev=0;
int curr=1;
try
{
for(prev=0;prev<max;)
{
po.write(getBytes(prev));
int temp=curr;
curr=curr+prev;
prev=temp;
}
po.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
class Prime extends global implements Runnable
{
PipedOutputStream po;
public Prime(PipedInputStream pi)
{
try
{
po=new PipedOutputStream(pi);
}
catch(Exception e)
{
e.printStackTrace();
System.exit(0);
}
}
public void run()
{
try
{
for(int p=2;p<max;p++)
{
int i;
boolean isprime=true;
int sqr=(int)Math.sqrt(p);
for(i=2;i<=sqr;i++)
if(p%i==0)
{
isprime=false;
break;
}
if(isprime)
{
po.write(getBytes(p));
}
}
po.close();
}
catch(InterruptedIOException ie)
{
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
public class Pipe extends global
{
public static void main(String[] args)
{
try
{
 max=100000;
PipedInputStream fibb_pipe=new PipedInputStream();
PipedInputStream prim_pipe=new PipedInputStream();
Fibbinocci fibb=new Fibbinocci(fibb_pipe);
Prime prim=new Prime(prim_pipe);
Thread t1=new Thread(fibb);
Thread t2=new Thread(prim);
t1.start();
t2.start();
for(int common=0,f=getInt(fibb_pipe),p=getInt(prim_pipe);;)
{
if(f==p)
{
common=f;
System.out.println(common);
f=getInt(fibb_pipe);
p=getInt(prim_pipe);
}
else if(f<p)
{
f=getInt(fibb_pipe);
}
else
{
p=getInt(prim_pipe);
}
if(f==-1||p==-1)
{
break;
}
}
System.out.println("Done");
t1.interrupt();
t1.join();
t2.interrupt();
t2.join();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
OUTPUT
E:\Java>javac Pipe.java
E:\Java>java Pipe
2
3
5
13
89
233
1597
28657
Done

E:\Java>

 


9.JAVA DATABASE CONNECTIVITY

PROGRAM:
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
import java.net.*;
public class college extends Frame implements ActionListener
{
          Label l1,l2,l3,l4,l5;
          TextField t1,t2,t3,t4,t5;
          Button b1,b2;
          Connection con;
          ResultSet rs;
          Statement st;
          public college()
          {
                   setLayout(null);
                   l1=new Label("Emp Name");
                   l2=new Label("Emp ID");
                   l3=new Label("Emp Department");
                   l4=new Label("Emp Desg");
                   l5=new Label("Emp Salary");
                   t1=new TextField(20);
                   t2=new TextField(20);
                   t3=new TextField(20);
                   t4=new TextField(20);
                   t5=new TextField(20);
                   b1=new Button("Save");
                   b2=new Button("cancel");
                   l1.setBounds(40,40,100,25);
                   t1.setBounds(140,40,100,25);
                   l2.setBounds(40,80,100,25);
                   t2.setBounds(140,80,100,25);
                   l3.setBounds(40,120,100,25);
                   t3.setBounds(140,120,100,25);
                   l4.setBounds(40,160,100,25);
                   t4.setBounds(140,160,100,25);
                   l5.setBounds(40,200,100,25);
                   t5.setBounds(140,200,100,25);
                   b1.setBounds(40,240,100,25);
                   b2.setBounds(140,240,100,25);
                   add(l1);
                   add(t1);
                   add(l2);
                   add(t2);
                   add(l3);
                   add(t3);
                   add(l4);
                   add(t4);
                   add(l5);
                   add(t5);
                   add(b1);
                   add(b2);
                   b1.addActionListener(this);
                   b2.addActionListener(this);
          }
          public static void main(String args[])throws Exception
          {
                   college n=new college();
                   n.setSize(500,350);
                   n.show();
                  
          }
          public void actionPerformed(ActionEvent ae)
          {
                   try
                   {       
                             Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                             con=DriverManager.getConnection("jdbc:odbc:vvk","scott","tiger");
                             st=con.createStatement();
                             if(ae.getSource()==b1)
                             {
                                      //System.out.println("ok");
                                      st.executeUpdate("insert into jay2 values('"+(t1.getText())+"','"+(t2.getText())+"','"+(t3.getText())+"','"+(t4.getText())+"','"+(t5.getText())+"')");
                                      JOptionPane.showMessageDialog(null,"This record has been inserted","RV'S Project",1);
                                      t1.setText("");
                                      t2.setText("");
                                      t3.setText("");
                                      t4.setText("");
                                      t5.setText("");
                             }
                             else if(ae.getSource()==b2)
                             {
                                      //System.out.println("nothing happened");
                                      System.exit(0);
                             }
                   }
                   catch(Exception l)
                   {}
          }
}

 



                              10. MULTITHREADED ECHO SERVER

PROGRAMSERVER
import java.io.*;
import java.net.*;
class eserver
{
public static void main(String[] args)
{
System.out.println("Echo server started");
try
{
ServerSocket s=new ServerSocket(8010);
Socket incoming=s.accept();
System.out.println("Connected to"+ incoming.getInetAddress()+"at port"+incoming.getLocalPort());
BufferedReader in = new BufferedReader(new InputStreamReader(incoming.getInputStream()));
PrintWriter out=new PrintWriter(new OutputStreamWriter(incoming.getOutputStream()));
out.println("Hello This is java EchoServer Enter BYE to exit");
out.flush();
for(;;)
{
String str=in.readLine();
if(str==null)
{
break;
}
else
{
out.println("Echo "+str);
out.flush();
System.out.println("Received "+str);
if(str.trim().equals("bye"))
break;
}
}
incoming.close();
}
catch(Exception e)
{
System.out.println("Error"+e);
}
System.out.println("EchoServer Stopped");
}
}
PROGRAM-client
import java.io.*;
import java.net.*;
class eclient
{
public static void main(String[] args)
{
try
{
String host;
if(args.length>0 && args[0]!= null)
{
host=args[0];
}
else
{
host="localhost";
}
Socket t =new Socket(host,8010);
BufferedReader in=new BufferedReader(new InputStreamReader(t.getInputStream()));
PrintWriter out=new PrintWriter(new OutputStreamWriter(t.getOutputStream()));
for(int i=1;i<=10;i++)
{
System.out.println("Sending Line"+i);
out.println("line"+i);
out.flush();
}
out.println("bye");
out.flush();
for(;;)
{
String str=in.readLine();
if(str==null)
{
break;
}
else   
{
System.out.println(str);
}
}
}
catch(Exception e)
{
System.out.println("Error"+e);
}
}
}

OUTPUT
E:\Java>javac eserver.java

E:\Java>javac eclient.java
E:\Java>java eserver
Echo server started
Connected to/127.0.0.1at port8010
Received line1
Received line2
Received line3
Received line4
Received line5
Received line6
Received line7
Received line8
Received line9
Received line10
Received bye
EchoServer Stopped

E:\Java>
E:\Java>java eclient
Sending Line1
Sending Line2
Sending Line3
Sending Line4
Sending Line5
Sending Line6
Sending Line7
Sending Line8
Sending Line9
Sending Line10
Hello This is java EchoServer Enter BYE to exit
Echo line1
Echo line2
Echo line3
Echo line4
Echo line5
Echo line6
Echo line7
Echo line8
Echo line9
Echo line10
Echo bye

E:\Java>

PROGRAM
import java.awt.*;
import java.awt.event.*;
import java.awt.datatransfer.*;
import java.io.*;
public class Editor extends Frame
{
String filename;
TextArea tx;
Clipboard clip = getToolkit().getSystemClipboard();
Editor()
{
setLayout(new GridLayout(1,1));
tx = new TextArea();
add(tx);
MenuBar mb = new MenuBar();
Menu F = new Menu("File");
MenuItem n = new MenuItem("New");
MenuItem o = new MenuItem("Open");
MenuItem s = new MenuItem("Save");
MenuItem e = new MenuItem("Exit");
n.addActionListener(new New());
F.add(n);
o.addActionListener(new Open());
F.add(o);
s.addActionListener(new Save());
F.add(s);
e.addActionListener(new Exit());
F.add(e);
mb.add(F);
Menu E = new Menu("Edit");
MenuItem cut = new MenuItem("Cut");
MenuItem copy = new MenuItem("Copy");
MenuItem paste = new MenuItem("Paste");
cut.addActionListener(new Cut());
E.add(cut);
copy.addActionListener(new Copy());
E.add(copy);
paste.addActionListener(new Paste());
E.add(paste);
mb.add(E);
setMenuBar(mb);
mylistener mylist = new mylistener();
addWindowListener(mylist);
}
class mylistener extends WindowAdapter
{
public void windowClosing (WindowEvent e)
{
System.exit(0);
}
}
class New implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
tx.setText(" ");
setTitle(filename);
}
}
class Open implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
FileDialog fd = new FileDialog(Editor.this, "select File",FileDialog.LOAD);
fd.show();
if (fd.getFile()!=null)
{
filename = fd.getDirectory() + fd.getFile();
setTitle(filename);
ReadFile();
}
tx.requestFocus();
}
}
class Save implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
FileDialog fd = new FileDialog(Editor.this,"Save File",FileDialog.SAVE);
fd.show();
if (fd.getFile()!=null)
{
filename = fd.getDirectory() + fd.getFile();
setTitle(filename);
try
{
DataOutputStream d = new DataOutputStream(new FileOutputStream(filename));
String line = tx.getText();
BufferedReader br = new BufferedReader(new StringReader(line));
while((line = br.readLine())!=null)
{
d.writeBytes(line + "\r\n");
d.close();
}
}
catch(Exception ex)
{
System.out.println("File not found");
}
tx.requestFocus();
}
}
}
class Exit implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
void ReadFile()
{
BufferedReader d;
StringBuffer sb = new StringBuffer();
try
{
d = new BufferedReader(new FileReader(filename));
String line;
while((line=d.readLine())!=null)
sb.append(line + "\n");
tx.setText(sb.toString());
d.close();
}
catch(FileNotFoundException fe)
{
System.out.println("File not Found");
}
catch(IOException ioe){}
}
class Cut implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String sel = tx.getSelectedText();
StringSelection ss = new StringSelection(sel);
clip.setContents(ss,ss);
tx.replaceRange(" ",tx.getSelectionStart(),tx.getSelectionEnd());
}
}
          class Copy implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String sel = tx.getSelectedText();
StringSelection clipString = new StringSelection(sel);
clip.setContents(clipString,clipString);
}
}
class Paste implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Transferable cliptran = clip.getContents(Editor.this);
try
{
String sel = (String) cliptran.getTransferData(DataFlavor.stringFlavor);
tx.replaceRange(sel,tx.getSelectionStart(),tx.getSelectionEnd());
}
catch(Exception exc)
{
System.out.println("not string flavour");
}
}
}
public static void main(String args[])
{
Frame f = new Editor();
f.setSize(500,400);
f.setVisible(true);
f.show();
}
}

OUTPUT
E:\Java>javac Editor.java

E:\Java>java Editor

E:\Java>