Wednesday 21 September 2011

NEXT LAB PROGRAMS:

PRODUCER CONSUMER USING THREAD QUEUE CLASS:
class Q
{
    int n;
    boolean valueSet=false;
    synchronized int get()
    {
        if(!valueSet)
            try
            {
                wait();
            }catch(InterruptedException e)
            {
                System.out.println("InterruptedException caught");
            }
        System.out.println("Consumer consumes:Got: "+n);
        valueSet=false;
        notify();
        return n;
    }
    synchronized void put(int n)
    {
        if(valueSet)
            try
            {
                wait();
            }catch(InterruptedException e)
            {
                System.out.println("InterruptedExeption caught");
            }
        this.n=n;
        valueSet=true;
        System.out.println("producer input -Put: "+n);
        notify();
    }
}
class Producer implements Runnable
{
    Q q;
    Producer(Q q)
    {
        this.q=q;
        new Thread(this,"Producer").start();
    }
    public void run()
    {
        int i=0;
        while(i<20)
        {
            q.put(i++);
        }
    }
}
class Consumer implements Runnable
{
    Q q;
    Consumer(Q q)
    {
        this.q=q;
        new Thread(this,"Consumer").start();
    }
    public void run()
    {
        while(true)
        {
            q.get();
        }
    }
}
class PCQue
{
    public static void main(String args[])
    {
        Q q=new Q();
        new Producer(q);
        new Consumer(q);
        System.out.println("Press Control-C to stop!");
    }
}


CLIENT SERVER COMMUNICATION:
EchoClient.java:
import java.io.*;
import java.net.*;
import java.awt.event.*;
import javax.swing.*;

public class EchoClient extends JFrame implements ActionListener
{
    JTextField tf=new JTextField(15);
    JTextField tf2=new JTextField(15);
    BufferedReader in;
    PrintWriter out;
    JButton b;
    public EchoClient()
    {
        try
        {
            JFrame f=new JFrame();
            f.setSize(100,100);
            Socket socket=new Socket("localhost",8008);
            in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
            out=new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
            f.setTitle("client"+socket);
            tf.setText(" ");
            tf2.setText(" ");
            JPanel p=new JPanel();
            JLabel l=new JLabel("enter the text to sent to the server");
            JLabel l2=new JLabel("Text echoed from server in uppercase");
            b=new JButton("send");
            p.setLayout(new BoxLayout(p,BoxLayout.Y_AXIS));
            p.add(l);
            p.add(tf);
            p.add(l2);
            p.add(tf2);
            p.add(b);
            f.add(p);
            f.setVisible(true);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            b.addActionListener(this);
            out.flush();
            while(true)
            {
                String str=in.readLine();
                tf2.setText(str);
            }
        }catch(Exception e)
        {}
    }
public void actionPerformed(ActionEvent event)
{
    String str1=tf.getText();
    System.out.println("sending "+str1);
    out.println(str1.toString());
    out.flush();
    tf.setText(" ");
}
public static void main(String args[])
{
    EchoClient f=new EchoClient();
}
}

EchoServer.java:
import java.io.*;
import java.net.*;
public class EchoServer
{
public static void main(String args[])throws Exception
{
    try
    {
        ServerSocket s=new ServerSocket(8008);
        while(true)
        {
            Socket incoming=s.accept();
            new ClientHandler(incoming).start();
        }
    }catch(Exception e)
    {}
    }
}
class ClientHandler extends Thread
{
    Socket incoming;
    ClientHandler(Socket incoming)
    {
        this.incoming=incoming;
    }
    public void run()
    {
        try
        {
            BufferedReader in=new BufferedReader(new InputStreamReader(incoming.getInputStream()));
            PrintWriter out=new PrintWriter(new PrintWriter(new OutputStreamWriter(incoming.getOutputStream())));
            out.flush();
            while(true)
            {
                String str=in.readLine();
                System.out.println("receiving from "+incoming+" "+str);
                out.println(str.toUpperCase());
                out.flush();
                if(str.trim().endsWith("BYE"))
                    break;
            }
            incoming.close();
        }catch(Exception e)
        {}
    }
}

TEMP LIST JAVA:
import java.util.List;
import java.util.LinkedList;
import java.util.ListIterator;
public class TempList
{
final String colors[]={"red "," blue" ,"green ","grey "};
final String numbers[]={"One "," Two","Three "};

TempList()
{
List<String> list1=new LinkedList();
List<String> list2=new LinkedList();
List<String> list3=new LinkedList();
for(String s:colors)
{
    list1.add(s);
}
System.out.println("-------List1------");
printList(list1);
for(String s:numbers)
{
    list2.add(s);
}
System.out.println("-------List2------");
printList(list2);
list3.addAll(list2);
System.out.println("-------List3------");
printList(list3);
list3.addAll(list1);
printList(list3);
list3.retainAll(list2);
System.out.println("-------After reatining--------");
printList(list3);
addItem(list3," four ");
System.out.println("-------After adding--------");
printList(list3);
addItem(list3," five ",4);
System.out.println("-------After adding--------");
printList(list3);
removeItem(list3,1);
System.out.println("-------After removing--------");
printList(list3);
removeItem(list3,2,4);
System.out.println("-------After removing--------");
printList(list3);
toUpperCaseString(list3);
printList(list3);

}
void addItem(List<String> list,String str)
{
list.add(str);
}
void addItem(List<String> list,String str,int index)
{
list.add(index,str);
}
void removeItem(List<String> list,int index)
{
list.remove(index);
}
void toUpperCaseString(List<String> list)
{
ListIterator<String> iterator=list.listIterator();
while(iterator.hasNext())
{
String str=iterator.next();
iterator.set(str.toUpperCase());
}
}
void removeItem(List<String> list,int s,int e)
{
list.subList(s,e).clear();
}

void printList(List<String> list)
{


for(String s:list)
{
    System.out.print(s);
}
System.out.println();
}
public static void main(String args[])
{
    new TempList();
}
}






Tuesday 20 September 2011

IT2301 retest question paper


The same 15 questionns
PART-B
1.What is object cloning? Why it is needed? Explain how objects are cloned? And
          Explain the types of cloning with an example program.                                            
 2.Explain all 2D package classes.Write a java program to draw the circle using
          swing components.                                                                                             
3.Define reflection and uses of reflection.Explain the following term related to Reflection
        1)Analyze the cabability of classes.     2) Analyze the Object capability at run time.
        3)Generic array code implementation.  4)Exception types.5)Class class                         
4.Draw the structure of relationship between the shape classes.                                  
     
5.What is Frame? How is a Frame created? Explain Frame properties.Write a java program
         display “Not a Hello,World  Program with Red Color” label  in the Frame window.        
6.Write a code to copy the contents of the file input.txt one byte at a time to output.txt     
7.Write a java program to calculate sum,average of  5 number using interface      
8.Explain the types of Inner classes.                                                                        
9.Briefly explain about I/O streams with an example program.                                        
10. Consider a class person with attributes firstname and lastname.Write a java program to       
        create and clone instances of the Person class.                                                      
11.write a java program  to display  the “JAVA PROGRAMMING LAB”  message  with  
             yellow color  using java swing text component.
12.Define Proxy class?When you need that class? Define all Proxy Class methods.         

*******************************ALL THE BEST*************************

IT2301 RETEST QUESTION PAPERS


RETEST QUESTION  PAPERS
Answer All Questions
PART A – (5 x 2 = 10 Marks)
1. What are the mehods available in the Object class.
2. How does the declaration of a local inner class differ from the declaration of an anonymous    
     inner class.
3. What is the difference between the File and Random Access File classes?
4. Define serialization and BufferedReader class
5. Differentiate Interface and Abstract class.

PART – B [16+16+8 = 40 Marks]
6.a).i).What is object cloning? Why it is needed? Explain how objects are cloned? And
          Explain the types of cloning with an example program.                                               (10)                                                                     
       ii)Explain all 2D package classes.Write a java program to draw the circle using
          swing components.                                                                                                   (6)                                               
                                                                    (or)                                                         
    b).i))Define reflection and uses of reflection.Explain the following term related to Reflection
        1)Analyze the cabability of classes.     2) Analyze the Object capability at run time.
        3)Generic array code implementation.  4)Exception types.5)Class class                          (14)
        ii)Draw the structure of relationship between the shape classes.                                      (2)
     
7.a)i)What is Frame? How is a Frame created? Explain Frame properties.Write a java program
         display “Not a Hello,World  Program with Red Color” label  in the Frame window.        (8)
      ii) Write a code to copy the contents of the file input.txt one byte at a time to output.txt     (4)
      iii)Write a java program to calculate sum,average of  5 number using interface                  (4)
                                                                  (or)
  b).i) Explain the types of Inner classes.                                                                   (6)
       ii)Briefly explain about I/O streams with an example program.                                       (10)
                       
3. a) Consider a class person with attributes firstname and lastname.Write a java program to       
        create and clone instances of the Person class.                                                             (8)                                                                                  
                                                                 (or)
      
b)i)write a java program  to display  the “JAVA PROGRAMMING LAB”  message  with  
             yellow color  using java swing text component.(4)
          ii) Define Proxy class?When you need that class? Define all Proxy Class methods.         (4)

*******************************ALL THE BEST*************************

Tuesday 13 September 2011

Report: (Assignment and Observation)

Dear students,
           Submit the assignment and observation on 19.09.2011.

Monday 12 September 2011

LAB PROGRAMS

APPLEt PROGRAM:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class OOPDraw extends Applet implements ActionListener
{
 String s;
 Button b1,b2,b3,b4;
    public void init()
 {
  b1=new Button("Line");
  b2=new Button("Oval");
  b3=new Button("Rectangle");
  b4=new Button("String");
  add(b1);
  add(b2);
  add(b3);
  add(b4);
  b1.addActionListener(this);
  b2.addActionListener(this);
  b3.addActionListener(this);
                b4.addActionListener(this);
 
 }
 public void actionPerformed(ActionEvent e)
 {
  s=e.getActionCommand();
  repaint();
 }
 public void paint(Graphics g)
 {
  if(s.equals("Line"))
   g.drawLine(3,300,200,10);
                      
             if(s.equals("Oval"))
   g.drawOval(250,50,100,100);
  if(s.equals("Rectangle"))
   g.drawRect(400,50,200,100);
   if(s.equals("String"))
   g.drawString("Simple Paint Applet",200,100);
 }
}

/*<applet code="OOPDraw.java" Height=400 width=600>
</applet>
*/



PROGRAm-2:
SWING CALCULATOR:
import java.awt.event.*;
import javax.swing.*;
class sss
{
    public static void main(String args[])
    {
        JButton b1=new JButton("Add");
        JButton b2=new JButton("Sub");
        JButton b3=new JButton("Sin");
        JLabel l1=new JLabel("Number 1");
        final JTextField num1=new JTextField(20);
        JLabel l2=new JLabel("Number 2");
        final JTextField num2=new JTextField(20);
        final JFrame f=new JFrame();
        f.setTitle("Calculator");
        f.setSize(300,300);
        JPanel p=new JPanel();
        //p.setLayout(new GridLayout(3,2));
        p.add(l1);
        p.add(num1);
        p.add(l2);
        p.add(num2);
        p.add(b1);
        p.add(b2);
        p.add(b3);
        f.add(p);
    
b1.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                double a=Double.parseDouble(num1.getText());
                double b=Double.parseDouble(num2.getText());
                JOptionPane.showMessageDialog(f,"The sum is "+(a+b));
            }
        });
       b2.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                double a=Double.parseDouble(num1.getText());
                double b=Double.parseDouble(num2.getText());
                JOptionPane.showMessageDialog(f,"The Difference is "+(a-b));
            }
        });
    
        b3.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                double a=Double.parseDouble(num1.getText());
                double b=Math.toRadians(a);
                JOptionPane.showMessageDialog(f,"The Sine value is "+Math.sin(b));
            }
        });
             f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
}
}


PROGRAM-3
THREAD:
class Order
{
    private static int i=0;
    private int count=i++;
    public Order()
    {
        if(count==10)
        {
            System.out.println("Out of food,closing");
            System.exit(0);
        }
    }
    public String toString()
    {
        return "Order "+(count+1);
    }
}
class WaitPerson extends Thread
{
    private Restaurant restaurant;
    public WaitPerson(Restaurant r)
    {
        restaurant=r;
        start();
    }
    public void run()
    {
        while(true)
        {
            while(restaurant.order==null)
                synchronized(this)
            {
                    try
                    {
                        wait();
                    }
                    catch(InterruptedException e)
                    {
                        throw new RuntimeException(e);
                    }
             }
            System.out.println("WaitPerson got "+restaurant.order);
            restaurant.order=null;
        }
    }
}

class Chef extends Thread {
    private Restaurant restaurant;
    private WaitPerson waitperson;
    public Chef(Restaurant r,WaitPerson w)
    {
        restaurant=r;
        waitperson=w;
        start();
    }
    public void run()
    {
        while(true)
        {
            if(restaurant.order==null)
            {
                restaurant.order=new Order();
                System.out.println("Order up!");
                synchronized(waitperson)
                {
                    waitperson.notify();
                }
            }
            try
            {
                sleep(1000);
            }
            catch(InterruptedException e)
            {
                throw new RuntimeException(e);
            }
        }
    }
}
public class Restaurant
{
    Order order;
    public static void main(String args[])
    {
        Restaurant rest=new Restaurant();
        WaitPerson wait=new WaitPerson(rest);
        Chef chef=new Chef(rest,wait);
    }
}



PROGRAM:4
PRODUCER CONSUMER PROBLEM:
import java.util.*;
import java.io.*;
class Prodcons
{
    final protected LinkedList list=new LinkedList();
    Object justproduced;
    protected void produce()
    {
        int len=0;
        synchronized(list)
        {
            System.out.println("Producing the object");
            justproduced=new Object();
            list.addFirst(justproduced);
            len=list.size();
            list.notifyAll();
        }
        System.out.println("List size now "+len+" justproduced "+justproduced);
    }

    protected void consume()
    {
        Object obj=null;
        int len=0;
        synchronized(list)
        {
            while(list.size()==0)
            {
                try
                {
                    System.out.println("no objects are available to be consumed \n please wait until produce");
                    list.wait();
                }
                catch(InterruptedException ex)
                {
                    return;
                }
            }
         System.out.println("Concuming object from last");
         obj=list.removeLast();
         len=list.size();
        }
        System.out.println("Consuming object "+obj);
        System.out.println("List size now "+len);
    }
    public static void main(String args[]) throws IOException
    {
        Prodcons pc=new Prodcons();
        System.out.println("Ready (p to produce, c to consume):");
        int i;
        while((i=System.in.read())!=-1)
        {
            char ch=(char)i;
            switch(ch)
            {
                case 'p':
                    pc.produce();
                    break;
                case 'c':
                    pc.consume();
                    break;
            }
        }
    }
}

Saturday 10 September 2011

QUESTION BANK

QUESTIONS: 
 1.Write a java  program to draw a rectangle, the ellipse that is enclosed in the rectangle, a    
   diagonal of the rectangle, and a circle that has the same center as the  rectangle.          
  2.Explain the properties of Proxy Class and explain Proxy class methods.                             
    3.What is object cloning? Why it is needed? Explain how objects are cloned? And
     Explain the types of cloning with an example program.                                                  
   4.Explain the properties of equals() method and explain the methods of Frame,Component
      class.                                                                                                                                
  5.Define Frame.How is a Frame created? Explain the properties of Frame.Write a java   
      program to display “Not a Hello,Program with Red Color” message in the Frame.     
 6.Write a short note about character stream classes with an example program.                   
 7. Define reflection and uses of reflection explain the following term related to Reflection
           1)Analyze the cabability of classes.     2) Analyze the Object capability at run time.
            3)Generic array code implementation.  4)Exception types.         5)Class class.               
8.Write a java program to draw the rectangle,Circle,line using java swings.                         
9. Explain DataInputStream and DataOutputStream class.Write a code to copy the contents   
     of the file input.txt one byte at a time to output.txt using FOS and FIS.                 
10.Explain all 2D package classes.Write a java program to draw the circle using
          swing components.
11.Write a java program to calculate sum,average of  5 number using interface
12. Explain the types of Inner classes.                                                                     
13.Briefly explain about I/O streams with an example program.
14.Consider a class person with attributes firstname and lastname.Write a java program to        
    create and clone instances of the Person class.
15.write a java program  to display  the “JAVA PROGRAMMING LAB”  message  with  
             yellow color  using java swing text component.
 16.Define Proxy class?When you need that class? Define all Proxy Class methods.        

Thursday 8 September 2011

TWO MARK QUESTIONS

QUESTIONS:

1. Define serialization and BufferedOutputStream class
2..Define hashCode() method and write the hashCode algorithm for String class.
3. Write a java program to read the characters  using BufferedReader class.
4.  How does the declaration of a local inner class differ from the declaration of an anonymous    
     inner class.

5. Define  AWT and Swing

6. Differentiate Interface and Abstract class.

7. What are the mehods available in the Object class.
8. How does the declaration of a local inner class differ from the declaration of an anonymous    
     inner class.
9. What is the difference between the File and Random Access File classes?
10. Define serialization and BufferedReader class
11.What are the steps to implement  the Interface and define any two default interfaces.
12.
Explain DataInputStream and DataOutputStream class.
13.Draw the structure of relationship between the shape classes.
14.Write a code to copy the contents of the file input.txt one byte at a time to output.txt using FOS and FIS.  
15.Explain the properties of Proxy Class