Monday, February 7, 2011

IT2301 - Java Programs - Develop a application using GUI and multithreads in java




import java.awt.*;
import java.awt.Graphics2D;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;
class AnalogClock extends JPanel
{
ImageIcon img;
private GregorianCalendar m_calendar;
private int[] x=new int[2];
private int[] y=new int[2];                                                                   
private java.util.Timer clocktimer=new java.util.Timer();
private TimeZone clockTimeZone=TimeZone.getDefault();
public AnalogClock()
{
this.setPreferredSize(new Dimension(210,210));
this.setMinimumSize(new Dimension(210,210));
clocktimer.schedule(new TickTimerTask(),0,1000);
}
public void paint(Graphics g)
{
g.setColor(Color.orange);
g.fillRect(0,0,this.getWidth(),this.getHeight());
drawCardinals((Graphics2D)g);
drawHands((Graphics2D)g);
}
void clockMinutes(int startRadius,int endRadius,double theta)
{
theta-=Math.PI/2;
x[0]=(int)(getWidth()/2+startRadius*Math.cos(theta));
y[0]=(int)(getHeight()/2+startRadius*Math.sin(theta));
x[1]=(int)(getWidth()/2+endRadius*Math.cos(theta));
y[1]=(int)(getHeight()/2+endRadius*Math.sin(theta));
}
void drawCardinals(Graphics2D g)
{
g.setStroke(new BasicStroke(9));
g.setColor(Color.black);
for(double theta=0;theta<Math.PI*2;theta+=Math.PI/6)
{
clockMinutes(100,100,theta);
g.drawPolyline(x,y,2);
}
}
public void drawHands(Graphics2D g)
{
double h=2*Math.PI*(m_calendar.get(Calendar.HOUR));
double m=2*Math.PI*(m_calendar.get(Calendar.MINUTE));
double s=2*Math.PI*(m_calendar.get(Calendar.SECOND));
g.setStroke(new BasicStroke(9));
clockMinutes(0,55,h/12+m/(60*12));
g.setColor(Color.red);
g.drawPolyline(x,y,2);
clockMinutes(0,70,m/60+s/(60*60));
g.setColor(Color.blue);
g.drawPolyline(x,y,2);
clockMinutes(0,70,s/60);
g.setColor(Color.black);
g.drawPolyline(x,y,2);
g.fillOval(getWidth()/2-8,getHeight()/2-8,16,16);
}
class TickTimerTask extends TimerTask
{
public void run()
{
m_calendar=(GregorianCalendar)GregorianCalendar.getInstance(clockTimeZone);
repaint();
}
}
}
class Analog
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setSize(300, 400);
frame.setTitle("My Java Project Clock");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setIconImage(Toolkit.getDefaultToolkit().getImage("Clock.gif"));
AnalogClock m_AnalogClock=new AnalogClock();
frame.add(m_AnalogClock);
frame.setVisible(true);
}
}












Output:

C:\jdk1.5.0\bin>javac Analog.java

C:\jdk1.5.0\bin>java Analog

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:

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

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







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