sreebrothers.blogspot.com

Welcome to sreebrothers.blogspot.com - A blog that helps engineering students, particularly for Computer Science and Engineering students. Are you a CSE student? Are you finding any study materials? This is your gateway to seek your study materials. This website offers various resources for the students pursuing their courses in affiliated college of Anna University. This included a number of resources in various subjects such as Programming Paradigms, Web Technology, Internet Programming Lab, Java Lab, and so on. In addition to these resources, it offers some university updates, and also included some previous year university question papers. Keep visiting this blog for new updates......

"If someone feels that they had never made a mistake in their life, then it means they had never tried a new things in their life"
Albert Einstein---

CS58 / CS2309 – Java Lab

1.Rational Number Class in Java

AIM:
To write a program in Java with the following:
  • Develop a Rational number class.
  • Use JavaDoc comments for documentation
  • Your implementation should use efficient representation for a rational number, i.e. (500/1000) should be represented as (1/2).
PROGRAM:
RationalClass.java
import java.util.*;
/**
*@author Sreekandan.K
*/
public class RationalClass
{
 /**
 The Numerator part of Rational
 */
 private int numerator; 
 /**
 The Denominator part of Rational
 */
 private int denominator;   
 /**
 create and initialize a new Rational object
 */
 public RationalClass(int numerator,int denominator)
 {
  if(denominator==0)
  {
   throw new RuntimeException("Denominator is zero");
  }
  int g=gcd(numerator,denominator);
  if(g==1)
  {
   System.out.println("No Common Divisor for Numerator and Denominator");
   this.numerator=numerator;
   this.denominator=denominator;
  }
  else
  {
   this.numerator=numerator/g;
   this.denominator=denominator/g;
  }
 }
 /**
 return string representation
 */
 public String display()
 {
  return numerator+"/"+denominator;
 }
 /**
 @param n
 @param d
 @return Greatest common divisor for n and d
 */
 private static int gcd(int n,int d)
 {
  if(d==0)
  return n;
  else
  return gcd(d,n%d);
 }
 public static void main(String[] args)
 {
  Scanner input=new Scanner(System.in);
  System.out.print("Enter Numerator : ");
  int numerator=input.nextInt();
  System.out.print("Enter Denominator : ");
  int denominator=input.nextInt();
  Rational rational = new Rational(numerator,denominator);
  String str=rational.display();
  System.out.println("Efficient Representation for the rational number :"     +str);
 }
}

2.Date Class in Java

AIM:
To develop Date class in Java similar to the one available in java.util package.  Use JavaDoc comments for documentation.

PROGRAM:
DateFormatDemo.java
import java.util.Calendar;
/**
 *Class DateInfo provides Date and Time Information
 *@author Sreekandan.K
*/
public class DateInfo
{
 long year;
 int month,day,hour,minute,second;
 /**
  * @see java.util package
 */
 Calendar cal=Calendar.getInstance();
 /**
 @return year
 */
 long getYear()
 {
  year=cal.get(Calendar.YEAR);
  return year;
 }
 /**
 @return month
 */
 int getMonth()
 {
  month=cal.get(Calendar.MONTH);
  return month;
 }
 /**
 @return date
 */
 int getDay()
 {
  day=cal.get(Calendar.DATE);
  return day;
 }
 /**
 @return hours
 */
 int getHour()
 {
  hour=cal.get(Calendar.HOUR_OF_DAY);
  return hour;
 }
 /**
 @return minutes
 */
 int getMinute()
 {
  minute=cal.get(Calendar.MINUTE);
  return minute;
 }
 /**
 @return seconds
 */
 int getSecond()
 {
  second=cal.get(Calendar.SECOND);
  return second;
 }
 public static void main(String args[])
 {
  DateInfo d=new DateInfo();
  System.out.println("CURRENT DATE AND TIME:");
  System.out.print(d.getDay()+"-"+d.getMonth()+"-"+d.getYear());
  System.out.print(" "+d.getHour()+":"+d.getMinute()+":"+d.getSecond());
 }
}

3.Lisp-like List in Java

AIM:
To implement Lisp-like list in Java that performs the basic operations such as 'car', 'cdr', and 'cons'.

PROGRAM:
LispOperation.java
import java.util.*;
/**
 *@author Sreekandan.K
*/
class Lisp
{
 public Vector car(Vector v)
 {
  Vector elt=new Vector();
  elt.addElement(v.elementAt(0));
  return elt;
 }
 public Vector cdr(Vector v)
 {
  Vector elt=new Vector();
  for(int i=1;i<v.size();i++)
  elt.addElement(v.elementAt(i));
  return elt;
 }
 public Vector cons(int x, Vector v)
 {
  v.insertElementAt(x,0);
  return v;
 }
}
class LispOperation
{
 public static void main(String[] args)
 {
  Lisp L=new Lisp();
  Vector v=new Vector(4,1);
  v.addElement(3);
  v.addElement(0);
  v.addElement(2);
  v.addElement(5);
  System.out.println("Basic Lisp Operations");
  System.out.println("---------------------");
  System.out.println("The Elements of the List are:"+v);
  System.out.println("Lisp Operation-CAR:"+L.car(v));
  System.out.println("Lisp Operation-CDR:"+L.cdr(v));
  System.out.println("Elements of the List After CONS:"+L.cons(9,v));
 }
}

4.Implementation of Stack ADT Using Array

AIM:

To write a java program with the following:
  • Design an interface for stack ADT.
  • Develop a class that implements this interface using array.
  • Provide necessary exception handling in the implementation

PROGRAM:
StackADTArray.java

import java.io.*;
import java.util.*;
interface stackintf
{
 int n=50;
 public void pop();
 public void push();
 public void display();
}
class Stack implements stackintf
{
 int arr[]=new int[n];
 int top=-1;
 Scanner in=new Scanner(System.in);
 public void push()
 {
  try
  {
   System.out.println("Enter The Element of Stack");
   int elt=in.nextInt();
   arr[++top]=elt;
  }
  catch (Exception e)
  {
   System.out.println("e");
  }
 }
 public void pop()
 {
  int pop=arr[top];
  top--;
  System.out.println("Popped Element Is:"+pop);
 }
 public void display()
 {
  if(top<0)
  {
   System.out.println("Stack Is Empty");
  }
  else
  {
   System.out.print("Stack Elements Are:");
   for(int i=0;i<=top;i++)
   {
    System.out.print(arr[i]+" ");
   }
  }
 }

/**
 *@author Sreekandan.K
*/
class StackADTArray
{
 public static void main(String arg[])throws IOException
 {
  System.out.println("\nIMPLEMENTATION OF STACK USING ARRAY");
 System.out.println("===================================");
  Scanner in=new Scanner(System.in);
  Stack st=new Stack();
  int no=0;
  do
  {  
   System.out.println("\n1.push \n2.pop \n3.display \n4.Exit\n");
   System.out.print("Enter Your Choice:");
   no=in.nextInt();
   switch(no)
   {
    case 1:
    st.push();
    break;
    case 2:
    st.pop();
    break;
    case 3:
    st.display();
    break;
    case 4:
    System.exit(0);
   }
  }
  while(no<=5);
 }
}


5.Implementation of Stack ADT Using LinkedList 

AIM:

To write a java program with the following:
  • Design an interface for stack ADT.
  • Develop a class that implements this interface using LinkedList.
  • Provide necessary exception handling in the implementation

PROGRAM:
StackADTLinkedList.java 
import java.io.*; 
import java.util.*;
interface stackintf


{
 public void pop();
 public void push();
 public void display();
}
class Stack implements stackintf
{
 LinkedList list=new LinkedList();
 Scanner in=new Scanner(System.in);
 public void push()
 {
  try
  {
   System.out.print("Enter the Element of Stack:");
   int elt=in.nextInt();
   list.addLast(elt);
  }
  catch(Exception e)
  {
   System.out.println(e);
  }
 }
 public void pop()
 {
  Object last=list.getLast();
  list.removeLast();
  System.out.println("Popped Element Is:"+last);
 }
 public void display()
 {
  if(list.isEmpty())
  {
   System.out.println("Stack Is Empty");
  }
  else
  {
   System.out.println("Stack Elements Are:"+list);
  }
 }

/**
 *@author Sreekandan.K
*/ 
class StackADTLinkedList
{
 public static void main(String arg[])throws IOException
 {
  System.out.println("\nIMPLEMENTATION OF STACK USING LINKED LIST");
 System.out.println("=========================================");
  Scanner in=new Scanner(System.in);
  int no;
  Stack st=new Stack();
  do
  {
   System.out.println("\n1.push \n2.pop \n3.display \n4.Exit\n");
   System.out.print("Enter Your Choice:");
   no=in.nextInt();
   switch(no)
   {
    case 1:
    st.push();
    break;
    case 2:
    st.pop();
    break;
    case 3:
    st.display();
    break;
    case 4:
    System.exit(0);
    default:
    System.out.println("Invalid Choice!");
   }
  }
  while(no<=4);
 } 
} 
6.Vehicle Class Hierarchy in Java

AIM:
To design a vehicle class hierarchy in Java that demonstrates the concept of polymorphism.

PROGRAM:
PolyDemo.java
import java.io.*;
class Vehicle
{
 String regno;
 int model;
 Vehicle(String r, int m)
 {
  regno=r;
  model=m;
 }
 void display()
 {
  System.out.println("Registration Number:"+regno);
  System.out.println("Model:"+model);
 }
}
class Twowheeler extends Vehicle
{
 int wheel,door;
 Twowheeler(String r,int m,int w,int d)
 {
  super(r,m);
  wheel=w;
  door=d;
 }
 void display()
 {
  System.out.println("Two Wheeler");
  System.out.println("============================");
  super.display();
  System.out.println("Number of Wheel:"+wheel);
  System.out.println("Number of Door:"+door+"\n");
 }
}
class Threewheeler extends Vehicle
{
 int wheel,door;
 Threewheeler(String r,int m,int w,int d)
 {
  super(r,m);
  wheel=w;
  door=d;
 }
 void display()
 {
  System.out.println("Three Wheeler");
  System.out.println("==============================");
  super.display();
  System.out.println("Number of Wheel:"+wheel);
  System.out.println("Number of Door:"+door+"\n");
 }
}
class Fourwheeler extends Vehicle
{
 int wheel,door;
 Fourwheeler(String r,int m,int w,int d)
 {
  super(r,m);
  wheel=w;
  door=d;
 }
 void display()
 {
  System.out.println("Four Wheeler");
  System.out.println("=============================");
  super.display();
  System.out.println("Number of Wheel:"+wheel);
  System.out.println("Number of Door:"+door);
 }
}
/**
@author Sreekandan.K
*/
public class PolyDemo
{
 public static void main(String arg[])
 {
  Twowheeler two=new Twowheeler("TN75 9843",2011,2,0);
  Threewheeler three=new Threewheeler("TN30 8766",2010,3,2);
  Fourwheeler four=new Fourwheeler("TN15 2630",2009,4,4);
  two.display();
  three.display();
  four.display();
 }
}

7.Object Serialization

AIM:
To write a program in java to demonstrate object serialization with the following:
  • Design classes for Currency, Rupee, and Dollar.
  • Write a program that randomly generates Rupee and Dollar objects and write them into a file using object serialization.
  • Write another program to read that file.
  • Convert to Rupee if it reads a Dollar and leave the value if it reads a Rupee.
PROGRAM:
writeObj.java
import java.io.*;
import java.util.*;
abstract class Currency implements Serializable
{
 protected double money;
 public abstract double getValue();
 public abstract String printObj();
 public Currency(double money)
 {
  this.money=money;
 }
}
class Dollar extends Currency
{
 public Dollar(int money)
 {
  super(money);
 }
 public double getValue()
 {
  return this.money*51;
 }
 public String printObj()
 {
  String object="Object Name : Dollar\nUSD : $"+this.money+"\nINR : Rs"+getValue()+"\n";
  return object;
 }
}
class Rupee extends Currency
{
 public Rupee(int amount)
 {
  super(amount);
 }
 public double getValue()
 {
  return this.money;
 }
 public String printObj()
 {
  String object="Object Name : Rupee \nINR : Rs "+getValue()+"\n";
  return object;
 }
}
/**
 *@author Sreekandan.K
*/
public class writeObj
{
 public static void main(String[] args) throws FileNotFoundException,IOException
 {
  FileOutputStream fos=new FileOutputStream("currencyInfo.txt");
  ObjectOutputStream oos=new ObjectOutputStream(fos);
  Random rand=new Random();
  for(int i=0;i<10;i++)
  {
   Object[] obj={new Rupee(rand.nextInt(100)),new Dollar(rand.nextInt(100))};
   Currency currency=(Currency)obj[rand.nextInt(2)];
   oos.writeObject(currency);
  }
  oos.writeObject(null);          
  oos.close();
 }
}
readObj.java
import java.io.*;
/**
 *@author Sreekandan.K
*/
public class readObj
{
 public static void main(String[] args) throws IOException,ClassNotFoundException
 {
  Currency currency=null;
  FileInputStream fis=new FileInputStream("currencyInfo.txt");
  ObjectInputStream ois=new ObjectInputStream(fis);
  while((currency=(Currency)ois.readObject())!=null)
  {
   System.out.println(currency.printObj());
  }
  ois.close();
 }
}
8.Scientific Calculator in Java

AIM:
To design a scientific calculator using event-driven programming paradigm of Java.

PROGRAM:
SimpleCalculator.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.*;
/**
 *@author Sreekandan.K
*/
public class SimpleCalculator
{
 public static void main(String[] args)
 {
  CalcFrame cf=new CalcFrame();
  cf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  cf.setVisible(true);
 }
}
class CalcFrame extends JFrame
{
 public CalcFrame()
 {
  setTitle("CALCULATOR");
  CalcPanel panel=new CalcPanel();
  add(panel);
  pack();
 }
}
class CalcPanel extends JPanel
{
 JButton display;
 JPanel panel;
 double result;
 String lastcmd;
 boolean start;
 public CalcPanel()
 {
  setLayout(new BorderLayout());
  result=0;
  lastcmd="=";
  start=true;
  display=new JButton("0");
  display.setEnabled(false);
  add(display,BorderLayout.NORTH);
  ActionListener insert=new InsertAction();
  ActionListener cmd=new CommandAction();
  panel=new JPanel();
  panel.setLayout(new GridLayout(5,4));
  addButton("1",insert);
  addButton("2",insert);
  addButton("3",insert);
  addButton("/",cmd);
  addButton("4",insert);
  addButton("5",insert);
  addButton("6",insert);
  addButton("*",cmd);
  addButton("7",insert);
  addButton("8",insert);
  addButton("9",insert);
  addButton("-",cmd);
  addButton("0",insert);
  addButton(".",insert);
  addButton("pow",cmd);
  addButton("+",cmd);
  addButton("sin",insert);
  addButton("cos",insert);
  addButton("tan",insert);
  addButton("=",cmd);
  add(panel, BorderLayout.CENTER);
 }
 private void addButton(String label,ActionListener listener)
 {
  JButton button=new JButton(label);
  button.addActionListener(listener);
  panel.add(button);
 }
 private class InsertAction implements ActionListener
 {
  public void actionPerformed(ActionEvent ae)
  {
   String input=ae.getActionCommand();
   if(start==true)
   {
    display.setText("");
    start=false;
   }
   if(input.equals("sin"))
   {
    Double angle=Double.parseDouble(display.getText())*2.0*Math.PI/360.0;
    display.setText(""+Math.sin(angle));
   }
   else if(input.equals("cos"))
   {
    Double angle=Double.parseDouble(display.getText())*2.0*Math.PI/360.0;
    display.setText(""+Math.cos(angle));
   }
   else if(input.equals("tan"))
   {
    Double angle=Double.parseDouble(display.getText())*2.0*Math.PI/360.0;
    display.setText(""+Math.tan(angle));
   }
   else
   display.setText(display.getText()+input);
  }
 }
 private class CommandAction implements ActionListener
 {
  public void actionPerformed(ActionEvent ae)
  {
   String command=ae.getActionCommand();
   if(start==true)
   {
    if(command.equals("-"))
    {
     display.setText(command);
     start=false;
    }
    else
    lastcmd=command;
   }
   else
   {
    calc(Double.parseDouble(display.getText()));
    lastcmd=command;
    start=true;
   }
  }
 }
 public void calc(double x)
 {
  if(lastcmd.equals("+"))
  result=result+x;
  else if(lastcmd.equals("-"))
  result=result-x;
  else if(lastcmd.equals("*"))
  result=result*x;
  else if(lastcmd.equals("/"))
  result=result/x;
  else if(lastcmd.equals("="))
  result=x;
  else if(lastcmd.equals("pow"))
  {
   double powval=1.0;
   for(double i=0.0;i<x;i++)
   powval=powval*result;
   result=powval;
  }
  display.setText(""+ result);
 }
} 

9.Multithreading in Java

AIM:
To write a multi-threaded java program with the following:
  • Design a thread that generates prime numbers below 100,000 and writes them into a pipe.
  • Design another thread that generates fibonacci numbers and writes them to another pipe.
  • Design a main thread should read both the pipes to identify numbers common to both prime and Fibonacci and print all the numbers common to both prime and Fibonacci.
PROGRAM:
MultiThreadDemo.java
import java.util.*;
import java.io.*;
class Fibonacci extends Thread
{
 private PipedWriter out=new PipedWriter();
 public PipedWriter getPipedWriter()
 {
  return out;
 }
 public void run()
 {
  Thread t=Thread.currentThread();
  t.setName("Fibonacci");
  System.out.println(t.getName()+" Thread started...");
  int fibo=0,fibo1=0,fibo2=1;
  while(true)
  {
   try
   {
    fibo=fibo1+fibo2;
    if(fibo>100000)
    {
     out.close();
     break;
    }
    out.write(fibo);
    sleep(1000);     
   }
   catch(Exception e)
   {
    System.out.println("Exception:"+e);
   }
   fibo1=fibo2;
   fibo2=fibo;
  }
  System.out.println(t.getName()+" Thread exiting...");
 }
}
class Prime extends Thread
{
 private PipedWriter out1=new PipedWriter();
 public PipedWriter getPipedWriter()
 {
  return out1;
 }
 public void run()
 {
  Thread t=Thread.currentThread();
  t.setName("Prime");
  System.out.println(t.getName() +" Thread Started...");
  int prime=1;
  while(true)
  {
   try
   {
    if(prime>100000)
    {
     out1.close();
     break;
    }
    if(isPrime(prime))
    out1.write(prime);
    prime++;
    sleep(0);
   }
   catch(Exception e)
   {
    System.out.println(t.getName()+" Thread exiting...");
    System.exit(0);
   }
  }
 }
 public boolean isPrime(int n)
 {
  int m=(int)Math.round(Math.sqrt(n));
  if(n==1||n==2)
  return true;
  for(int i=2;i<=m;i++)
  if(n%i==0)
  return false;
  return true;
 }
}
/**
 *@author Sreekandan.K
*/
public class MultiThreadDemo
{
 public static void main(String[] args)throws Exception
 {
  Thread t=Thread.currentThread();
  t.setName("Main");
  System.out.println(t.getName()+" Thread Started...");
  Fibonacci fibObj=new Fibonacci();
  Prime primeObj=new Prime();
  PipedReader pr=new PipedReader(fibObj.getPipedWriter());
  PipedReader pr1=new PipedReader(primeObj.getPipedWriter());
  fibObj.start();
  primeObj.start();
  int fib=pr.read(),prm=pr1.read();
  System.out.println("The numbers common to PRIME and FIBONACCI:");
  while((fib!=-1)&&(prm!=-1))
  {
   while(prm<=fib)
   {
    if(fib==prm)
    System.out.println(prm);
    prm=pr1.read();
   }
   fib=pr.read();
  }
  System.out.println(t.getName()+ " Thread exiting...");
 }
}

10.OPAC System

AIM:
To develop a simple OPAC system for library using event-driven programming paradigms of Java. Use JDBC to connect to a back-end database.

PROGRAM:
OpacSystem.java
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
/**
 *@author Sreekandan.K
*/
public class OpacSystem implements ActionListener
{
 JRadioButton author=new JRadioButton("Search By Author");
 JRadioButton book=new JRadioButton("Search by Book");
 JTextField txt=new JTextField(30);
 JLabel label=new JLabel("Enter Search Key");
 JButton search=new JButton("SEARCH");
 JFrame frame=new JFrame();
 JTable table;
 DefaultTableModel model;
 String query="select*from opacTab";
 public OpacSystem()
 {
  frame.setTitle("OPAC SYSTEM");
  frame.setSize(800,500);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setLayout(new BorderLayout());
  JPanel p1=new JPanel();
  p1.setLayout(new FlowLayout());
  p1.add(label);
  p1.add(txt);
  ButtonGroup bg=new ButtonGroup();
  bg.add(author);
  bg.add(book); 
  JPanel p2=new JPanel();
  p2.setLayout(new FlowLayout());
  p2.add(author);
  p2.add(book);
  p2.add(search);
  search.addActionListener(this);
  JPanel p3=new JPanel();   
  p3.setLayout(new BorderLayout());
  p3.add(p1,BorderLayout.NORTH);
  p3.add(p2,BorderLayout.CENTER);
  frame.add(p3,BorderLayout.NORTH);
  addTable(query);
  frame.setVisible(true);
 }
 public void addTable(String str)
 {
  try
  {
   Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
   Connection con=DriverManager.getConnection("jdbc:odbc:opacDS");
   Statement stmt=con.createStatement();
   ResultSet rs=stmt.executeQuery(str);
   ResultSetMetaData rsmd=rs.getMetaData();
   int cols=rsmd.getColumnCount();
   model=new DefaultTableModel(1,cols);
   table=new JTable(model);
   String[] tabledata=new String[cols];
   int i=0;
   while(i<cols)
   {
    tabledata[i]=rsmd.getColumnName(i+1);
    i++;
   }
   model.addRow(tabledata);
   while(rs.next())
   {
    for(i=0;i<cols;i++)
    tabledata[i]=rs.getObject(i+1).toString();
    model.addRow(tabledata);
   }
   frame.add(table,BorderLayout.CENTER);
   con.close();
  }
  catch(Exception e)
  {
   System.out.println("Exception:"+e);
  }
 }
 public void actionPerformed(ActionEvent ae)
 {
  if(author.isSelected())
  query="select*from opacTab where AUTHOR like '"+txt.getText()+"%'";
  if(book.isSelected())
  query="select*from opacTab where BOOK like '"+txt.getText()+"%'";
  while(model.getRowCount()>0)
  model.removeRow(0);
  frame.remove(table);
  addTable(query);
 }
 public static void main(String[] args)
 {
  OpacSystem os=new OpacSystem();
 }  
}

11.Multi-threaded Echo Server

AIM:
To develop a multi-threaded echo server and a corresponding GUI client in Java.

PROGRAM:
EchoServer.java
import java.io.*;
import java.net.*;
/**
 *@author Sreekandan.K
*/
public class EchoServer
{
 public static void main(String [] args)
 {
  System.out.println("Server Started....");
  try
  {
   ServerSocket ss=new ServerSocket(300);
   while(true)
   {
    Socket s= ss.accept();
    Thread t = new ThreadedServer(s);
    t.start();
   }
  }
  catch(Exception e)
  {
   System.out.println("Error: " + e);
  }
 }
}
class ThreadedServer extends Thread
{
 Socket soc;
 public ThreadedServer(Socket soc)
 {
  this.soc=soc;
 }
 public void run()
 {
  try
  {
   BufferedReader in=new BufferedReader(new InputStreamReader(soc.getInputStream()));
   PrintWriter out=new PrintWriter(soc.getOutputStream());
   String str=in.readLine();
   System.out.println("Message From Client:"+str);
   out.flush();
   out.println("Message To Client:"+str);
   out.flush();
  }
  catch(Exception e)
  {
   System.out.println("Exception:"+e);
  }
 }
}
EchoClient.java
import java.net.*;
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
 *@author Sreekandan.K
*/
class EchoClient extends JFrame
{
 JTextArea ta;
 JTextField msg;
 JPanel panel;
 JScrollPane scroll;
 JButton b1=new JButton("Close");
 JButton b2=new JButton("Send");
 JLabel l1=new JLabel("Echo Client GUI");
 Container c;
 EchoClient()
 {
  c=getContentPane();
  setSize(300,470);
  setTitle("GUI Client");
  panel=new JPanel();
  msg=new JTextField(20);
  panel.setLayout(new FlowLayout(FlowLayout.CENTER));
  ta=new JTextArea(20,20);
  scroll=new JScrollPane(ta);
  panel.add(l1);
  panel.add(ta);
  panel.add(msg);
  panel.add(b2);
  panel.add(b1);
  c.add(panel);
  b2.addActionListener(new ActionListener()
  {
   public void actionPerformed(ActionEvent ae)
   {
    try
    {
     Socket s=new Socket("localhost",300);
     BufferedReader in=new BufferedReader(new InputStreamReader(s.getInputStream()));
     PrintWriter out=new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
     out.println(msg.getText());
     out.flush();
     String temp =ta.getText();
     if(temp.equalsIgnoreCase("quit"))
     {
      System.exit(0);
     }
     msg.setText("");
     ta.append("\n"+in.readLine());
    }
    catch (IOException e)
    {
     ta.setText("Exception:"+e);
    }
   }
  });
  b1.addActionListener(new ActionListener()
  {
   public void actionPerformed(ActionEvent e)
   {
    System.exit(0);
   }
  });
 }
 public static void main(String args[])
 {
  EchoClient frame=new EchoClient();
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setVisible(true);
 }
}

Popular Posts

About Author

Marthandam, Tamilnadu, India