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;
            }
        }
    }
}

No comments:

Post a Comment