Wednesday, 7 September 2011

create a checkbox


import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.color.*;
public class SampleFrame2
{
public static void main(String args[])
{
 MyFrame F=new MyFrame();
}
}
class MyFrame extends JFrame implements ActionListener
{

   JPanel p=new JPanel();
    JCheckBox a=new JCheckBox ("YELLOW");
    JCheckBox b=new JCheckBox("BLUE");
    JCheckBox c=new JCheckBox("RED");
    JCheckBox d=new JCheckBox("GREEN");

MyFrame()
{
    p.add(a);
    p.add(b);
    p.add(c);
    p.add(d);
    a.addActionListener(this);
    b.addActionListener(this);
    c.addActionListener(this);
    d.addActionListener(this);
    add(p);
    setVisible(true);

}

public void actionPerformed(ActionEvent e)
{
    if(e.getSource()==a)
    {
        p.setBackground(Color.yellow);
    }
    if(e.getSource()==b)
    {
        p.setBackground(Color.blue);
    }
      if(e.getSource()==c)
      {
           p.setBackground(Color.red);
    }
    if(e.getSource()==d)
    {
     p.setBackground(Color.green);
}
}
}

output:











Design a thread-safe implementation of Queue class. Write a multi-threaded producer-consumer application that uses this Queue class.


import java.io.*;
class queue
{
int n;
boolean v=false;
synchronized int get()
{
if(!v)
try
{
wait();
}
catch(InterruptedException e){}
System.out.println("GOT:"+n);
v=false;
notify();
return n;
}
synchronized void put(int n)
{
if(v)
try
{
wait();
}
catch(InterruptedException e){}
this.n=n;
v=true;
System.out.println("put:"+n);
notify();
}
}
class prod implements Runnable
{
queue q;
int n;
prod(queue q,int n)
{
this.q=q;
this.n=n;
new Thread(this,"producer").start();
}
public void run()
{
int i=1;
while(i<=n)
{
try
{
Thread.sleep(1000);
q.put(i++);
}
catch(InterruptedException e)
{}
}
System.exit(0);
}
}
class cons implements Runnable
{
queue q;
cons(queue q)
{
this.q=q;
new Thread(this,"consumer").start();
}
public void run()
{
while(true)
{
try
{
Thread.sleep(1000);
q.get();
}
catch(InterruptedException e){}
}}}
class nprod
{
public static void main(String args[])throws IOException
{
DataInputStream in=new DataInputStream(System.in);
System.out.println("enter the buffer size");
int a=Integer.parseInt(in.readLine());
queue q=new queue();
new prod(q,a);
new cons(q);
}
}

output:
C:\j2sdk1.4.0\bin>javac nprod.java
Note: nprod.java uses or overrides a deprecated API.
Note: Recompile with -deprecation for details.

C:\j2sdk1.4.0\bin>java nprod
Enter the Buffer size
5
PUT:1
GOT:1
PUT:2
GOT:2
PUT:3
GOT:3
PUT:4
GOT:4
PUT:5
GOT:5

Write a multi-threaded Java program to print all numbers below 100,000 that are both prime and fibonacci number (some examples are 2, 3, 5, 13, etc.). 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. The main thread should read both the pipes to identify numbers common to both

import java.io.*;
import java.io.PipedWriter;
import java.io.PipedReader;
class fibonacci extends Thread
{
            PipedWriter fw=new PipedWriter();
            public PipedWriter getwrite()
            {
                        return fw;
            }
            public void run()
            {
                        super.run();
                        fibo();
            }
            int f(int n)
            {
                        if(n<2)
                                    return n;
                                    else
                                                return f(n-1)+f(n-2);
            }
            void fibo()
            {
                        for(int i=2,fibv=0;(fibv=f(i))<100000;i++)
                        {
                                    try{
                                   
                                    fw.write(fibv);
                                    }
                                    catch(IOException e){
                                    }
                        }
            }
}
class receiver extends Thread
{
            PipedReader fibr,primer;
            public receiver(fibonacci fib,prime pr)throws IOException
            {
                        fibr=new PipedReader(fib.getwrite());
                        primer=new PipedReader(pr.getwrite());
            }
            public void run()
            {
                        int p=0,f=0;
                        try{
                       
                        p=primer.read();
                        f=fibr.read();
                        }
                        catch(IOException e)
                        {
                        }
                        while(true)
                        {
                                    try
                                                {
                                   
                                    if(p==f){
                                   
                                                System.out.println ("Match:"+p);
                                                p=primer.read();
                                                f=fibr.read();
                                    }
                                    else if(f<p)
                                                f=fibr.read();
                                                else
                                                            p=primer.read();
                        }catch(IOException e)
                        {System.exit(-1);
                        }
                        }
                       
            }
}
class prime extends Thread
{
            PipedWriter pw=new PipedWriter();
            public PipedWriter getwrite()
            {
                        return pw;
            }
            public void run()
            {
                        super.run();
                        prim();
            }
            public void prim()
            {
                        for(int i=2;i<100000;i++)
                        {
                                    if(isprime(i))
                                    {
                                                try{
                                                            pw.write(i);
                                                }
                                                catch(IOException e){
                                                }
                                    }
                        }
            }
            boolean isprime(int n)
            {
                        boolean p=true;
                        int s=(int)Math.sqrt(n);
                        for(int i=2;i<=s;i++)
                        {
                                    if(n%i==0)
                                                p=false;
                        }
                        return p;
            }
}
class fibprime
{
            public static void main (String[] args)throws IOException {
                        fibonacci fi=new fibonacci();
                        prime pri=new prime();
                        receiver r=new receiver(fi,pri);
                        fi.start();
                        pri.start();
                        r.start();
                       
}
}
 
 
 
 
output:
Output:

C:\j2sdk1.4.0\bin>javac fibprime.java

C:\j2sdk1.4.0\bin>java fibprime
Match:2
Match:3
Match:5
Match:13
Match:89
Match:233
Match:1597
Match:28657

create a menu

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.color.*;
public class SampleFrame6
{
public static void main(String args[])
{
 MyFrame F=new MyFrame();
  }
}
class MyFrame extends JFrame implements ActionListener
{
   JPanel p=new JPanel();
    JMenuBar a=new JMenuBar();
    JMenu file=new JMenu("file");
    JMenuItem i=new JMenuItem("yellow");
     JMenuItem i1=new JMenuItem("blue");
      JMenuItem i2=new JMenuItem("green");
       JMenuItem i3=new JMenuItem("red");
    MyFrame()
{
    p.add(a);
    a.add(file);
    file.add(i);
    file.add(i1);
    file.add(i2);
    file.add(i3);
    i.addActionListener(this);
    i1.addActionListener(this);
    i2.addActionListener(this);
    i3.addActionListener(this);
    add(p);
    setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
    if(e.getSource()==i)
    {
        p.setBackground(Color.yellow);
    }
    if(e.getSource()==i1)
    {
        p.setBackground(Color.blue);
    }
      if(e.getSource()==i2)
      {
           p.setBackground(Color.red);
    }
    if(e.getSource()==i3)
    {
     p.setBackground(Color.green);
}
}
}
output:

creating a button

import java.awt.color.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SimpleFrame
{
public static void main(String args[])
{
 MyFrame F=new MyFrame();
}
}
class MyFrame extends JFrame implements ActionListener
{
    JPanel p=new JPanel();
    JButton a=new JButton("YELLOW");
    JButton b=new JButton("BLUE");
    JButton c=new JButton("RED");
    JButton d=new JButton("GREEN");
MyFrame()
{
    p.add(a);
    p.add(b);
    p.add(c);
    p.add(d);
    a.addActionListener(this);
    b.addActionListener(this);
    c.addActionListener(this);
    d.addActionListener(this);
    add(p);
    setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
    if(e.getSource()==a)
    {
        p.setBackground(Color.yellow);
    }
    if(e.getSource()==b)
    {
        p.setBackground(Color.blue);
    }
      if(e.getSource()==c)
      {
           p.setBackground(Color.red);
    }
    if(e.getSource()==d)
    {
     p.setBackground(Color.green);
}
}
}

output:

if u click the yellow button below colour will be display.

Develop a template for linked-list class along with its methods in Java

import java.io.*;
import java.util.*;
class Link<T>
{
public T data;
public Link nextLink;
public Link(T d) {
data = d;
}
public void printLink() {
System.out.println("item:"+data);
}
}
class LinkList<T>
{
private Link first;
private Link last;
public LinkList() {
first = null;
}
public boolean isEmpty() {
return first == null;
}
public void insert(T d){
Link link = new Link(d);
if(first==null){
link.nextLink = null;
first = link;
last=link;
}
else{
last.nextLink=link;
link.nextLink=null;
last=link;
}
}
public Link delete() {
Link temp = first;
first = first.nextLink;
return temp;
}
public void printList() {
Link currentLink = first;
while(currentLink != null) {
currentLink.printLink();
currentLink = currentLink.nextLink;
}
System.out.println("");
}
}
class template {
public static void main(String[] args)
{
int i,c=1,ch,p1=0,p2=0,p3=0;
Scanner in=new Scanner(System.in);
LinkList<Integer> l = new LinkList();
LinkList<String>  s=new LinkList();
LinkList<Double> d=new LinkList();
do {
System.out.println("1.INTEGER    2.STRING     3.DOUBLE  4.exit");
System.out.println("enter ur choice:");
c=in.nextInt();
switch(c)
{
case 1:
do {
if(p1==1)break;
System.out.println("1.insert  2.delete  3.display 4.exit");
System.out.println("enter ur choice:");
ch=in.nextInt();
switch(ch)
{
case 1:
System.out.println("Integer list");
System.out.println("enter the insert value:");
i=in.nextInt();
l.insert(i);
break;
case 2:
l.delete();
System.out.println("data deleted:");
break;
case 3:
System.out.println("elements are :");
l.printList();
break;
case 4:
p1=1;
continue;
}
}while(c!=0);
break;
case 2:
do  {
if(p2==1)break;
System.out.println("1.insert  2.delete  3.display 4.exit");
System.out.println("enter ur choice:");
ch=in.nextInt();
switch(ch)
{
case 1:
System.out.println("STRING list");
System.out.println("enter the insert value:");
String a=in.next();
s.insert(a);
break;
case 2:
s.delete();
System.out.println("data deleted:");
break;
case 3:
System.out.println("elements are :");
s.printList();
break;
case 4:
p2=1;
continue;
}
}while(c!=0);
break;
case 3:
do{
if(p3==1)break;
System.out.println("1.insert  2.delete  3.display 4.exit");
System.out.println("enter ur choice:");
ch=in.nextInt();
switch(ch)
{
case 1:
System.out.println("DOUBLE list");
System.out.println("enter the insert value:");
double x=in.nextDouble();
d.insert(x);
break;
case 2:
d.delete();
System.out.println("data deleted:");
break;
case 3:
System.out.println("elements are :");
d.printList();
break;
case 4:
p3=1;
continue;
}
}while(c!=0);
break;
case 4:
System.exit(0);
}
}while(c!=0);
}
}




Output:


C:\jdk1.5.0\bin>java template
1.INTEGER    2.STRING     3.DOUBLE  4.exit
enter ur choice:
1
1.insert  2.delete  3.display 4.exit
enter ur choice:
1
Integer list
enter the insert value:
1
1.insert  2.delete  3.display 4.exit
enter ur choice:
1
Integer list
enter the insert value:
2
1.insert  2.delete  3.display 4.exit
enter ur choice:
1
Integer list
enter the insert value:
3
1.insert  2.delete  3.display 4.exit
enter ur choice:
3
elements are :
item:1
item:2
item:3

1.insert  2.delete  3.display 4.exit
enter ur choice:
2
data deleted:
1.insert  2.delete  3.display 4.exit
enter ur choice:
3
elements are :
item:2
item:3

1.insert  2.delete  3.display 4.exit
enter ur choice:
4
1.INTEGER    2.STRING     3.DOUBLE  4.exit
enter ur choice:
2
1.insert  2.delete  3.display 4.exit
enter ur choice:
1
STRING list
enter the insert value:
niren
1.insert  2.delete  3.display 4.exit
enter ur choice:
1
STRING list
enter the insert value:
kumar
1.insert  2.delete  3.display 4.exit
enter ur choice:
1
STRING list
enter the insert value:
raj
1.insert  2.delete  3.display 4.exit
enter ur choice:
3
elements are :
item:niren
item:kumar
item:raj

1.insert  2.delete  3.display 4.exit
enter ur choice:
2
data deleted:
1.insert  2.delete  3.display 4.exit
enter ur choice:
3
elements are :
item:kumar
item:raj

1.insert  2.delete  3.display 4.exit
enter ur choice:
4
1.INTEGER    2.STRING     3.DOUBLE  4.exit
enter ur choice:
4

thread tester

import java.io.*;
import java.awt.*;
public class ThreadTester
{
    public static void main(String args[])
    {
        Q q=new Q();
        P p=new P(q);
        C c=new C(q);
        p.start();
        c.start();
        System.out.println("press control -c to stop");
        }
}
class Q
{
    int n;boolean v=false;
    synchronized int get()
    {
        if(!v)
         try
         {
             wait();
         }
         catch(Exception e)
         {
             e.printStackTrace();
             }
        System.out.println("Got"+n);
        v=false;
        notify();
        return n;
        }
    synchronized void put(int n)
    {
        if(v)
            try
            {
                wait();
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        this.n=n;
        v=true;
        System.out.println("put"+n);
        notify();
    }
}
class P extends Thread
{
    Q q;
    public P(Q q2)
    {
        super("producer");
        q=q2;
    }
    public void run()
    {
        int i=0;
        while(true)
        {
            q.put(i++);
        }
    }
}
class C extends Thread
{
    Q q;
    public  C(Q q2)
    {
      super("consumer");
        q=q2;
    }
    public void run()
    {
        while(true)
        {
            q.get();
        }
    }
}