---------------------------------------------------------------------------------------------------------------------------
1 Write an application that starts two thread. First thread displays even numbers in the range specified from the command line and second thread displays odd numbers in the same range. Each thread waits for 300 milliseconds before displaying the next numbers. The application waits for both the thread to finish and then displays the message “Both threads completed”.
import java.lang.*;
public class odevthread
{
public static void main(String[] args)
{
String s=args[0];
Runnable r=new oddthread(s);
Thread t=new Thread(r);
Runnable r2=new oddthread(s);
Thread t2=new Thread(r2);
t.start();
t2.start();
try
{
t.join();
t2.join();
}
catch(InterruptedException e)
{
System.out.println(e);
}
System.out.println("end of main");
}
}
class oddthread implements Runnable
{
int n;
oddthread(String s)
{
n=Integer.parseInt(s);
}
public void run()
{
for(int i=0;i<n;i++)
{
try
{
if(i%2 ==1)
{
System.out.println("odd"+i);
Thread.sleep(300);
}
}catch(InterruptedException e)
{ }
}
}
}
class eventhread implements Runnable
{
int n;
eventhread(String s)
{
n=Integer.parseInt(s);
}
public void run()
{
for(int i=0;i<n;i++)
{
try
{
if(i%2==0)
{
System.out.println("even"+i);
Thread.sleep(300);
}
}catch(InterruptedException e)
{}
}
}
}
2 Write a program that create and starts five threads. Each thread is instantiated from the same class. It executes a loop with ten iterations. Each iteration displays the character 'x' and sleep for 500 milliseconds. The application waits for all threads to complete and then display a message ‘hello’
class thread1 extends Thread
{
int i;
public void run()
{
for(i=1;i<=10;i++)
{
try
{
System.out.println("T1-X "+i);
Thread.sleep(500);
}
catch(InterruptedException e)
{
}
}
}
}
class thread2 extends Thread
{
int i;
public void run()
{
for(i=1;i<=10;i++)
{
try
{
System.out.println("T2-X " +i);
Thread.sleep(500);
}
catch(InterruptedException e)
{}
}
}
}
class thread3 extends Thread
{
int i;
public void run()
{
for(i=1;i<=10;i++)
{
try
{
System.out.println("T3-X " +i);
Thread.sleep(500);
}
catch(InterruptedException e)
{}
}
}
}
class thread4 extends Thread
{
int i;
public void run()
{
for(i=1;i<=10;i++)
{
try
{
System.out.println("T4-X " +i);
Thread.sleep(500);
}
catch(InterruptedException e)
{}
}
}
}
class thread5 extends Thread
{
int i;
public void run()
{
for(i=1;i<=10;i++)
{
try
{
System.out.println("T5-X " +i);
Thread.sleep(500);
}
catch(InterruptedException e)
{}
}
}
}
class u4_p2
{
public static void main(String[] args)
{
thread1 th1=new thread1();
thread2 th2=new thread2();
thread3 th3=new thread3();
thread4 th4=new thread4();
thread5 th5=new thread5();
Thread t5=new Thread(th5);
Thread t4=new Thread(th4);
Thread t1=new Thread(th1);
Thread t2=new Thread(th2);
Thread t3=new Thread(th3);
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
try
{
t1.join();
t2.join();
t3.join();
t4.join();
t5.join();
}
catch(InterruptedException e)
{}
System.out.println("Hello");
}
}
3 Write a java program to create 2 threads each thread calculates the sum and average of 1 to 10 and 11 to 20 respectively. After all thread finish, main thread should print message “ Task Completed”. Write this program with use of runnable interface.
class thread1 implements Runnable
{
int sum1=0,i;
public void run()
{
for(i=1;i<=10;i++)
{
try
{
sum1=sum1+i;
Thread.sleep(1000);
}
catch(InterruptedException e)
{}
}
System.out.println("Thread1 sum="+sum1);
System.out.println("Average of Thread 1 Sum is = "+(sum1/10));
}
}
class thread2 implements Runnable
{
int sum2=0,i;
public void run()
{
for(i=11;i<=20;i++)
{
try
{
sum2=sum2+i;
Thread.sleep(1000);
}
catch(InterruptedException e)
{}
}
System.out.println("Thread2 Sum Is :- "+sum2);
System.out.println("Average of Thread 2 Sum is="+(sum2/10));
}
}
class u4_p3
{
public static void main(String[] args)
{
thread1 th1=new thread1();
thread2 th2=new thread2();
Thread t1= new Thread(th1);
Thread t2=new Thread(th2);
t1.start();
t2.start();
try
{
t1.join();
t2.join();
}
catch(InterruptedException e)
{
}
int total=th1.sum1+th2.sum2;
System.out.println("Total Sum is:- "+total);
System.out.println("Total Average Is :- "+(total/2));
}
}
4 Create two thread. One thread print ‘fybca’ 4 times and another thread print ‘sybca’ 6 times. Set priority for both thread and when thread finished print ‘tybca’ from main
class thread1 extends Thread
{
public void run()
{
int i;
for(i=1;i<=4;i++)
{
try
{
System.out.println("FyBCA");
Thread.sleep(1000);
}
catch(InterruptedException e)
{}
}
}
}
class thread2 extends Thread
{
public void run()
{
int i;
for(i=1;i<=6;i++)
{
try
{
System.out.println("SyBCA");
Thread.sleep(1000);
}
catch(InterruptedException e)
{}
}
}
}
class u4_p4
{
public static void main(String[] args)
{
thread1 th1=new thread1();
thread2 th2 = new thread2();
Thread t1=new Thread(th1);
Thread t2=new Thread(th2);
t1.start();
t2.start();
try
{
t1.join();
t2.join();
}
catch(InterruptedException e)
{}
System.out.println("TyBCA");
}
}
5 Create an applet which draws a line, rectangle and filled circle in applet display area.
//java file
import java.applet.*;
import java.awt.*;
public class u4_p5 extends Applet
{
public void paint(Graphics g)
{
g.drawLine(200,10,200,10);
g.fillOval(10,20,120,100);
g.drawRect(10,20,120,100);
}
}
//For Applet Program We Want To Make HTML File
<html>
<body>
<applet code= "u4_p5.class" width=200 height=150></applet>
</body>
</html>
--------------------------------------------------------------
For Run Applet Program:
Compile :- javac file_name.java
Run:- appletviewer file_name.html
6. Write applets to draw the following shapes. a. cone b. cylinder c. cube
import java.applet.*;
import java.awt.*;
public class sixth extends Applet
{
public void paint(Graphics g)
{
g.drawString("(i).Cylinder",10,110);
g.drawOval(10,10,50,10);
g.drawOval(10,80,50,10);
g.drawLine(10,15,10,85);
g.drawLine(60,15,60,85);
g.drawString("(ii).Cube",95,110);
g.drawRect(80,10,50,50);
g.drawRect(95,25,50,50);
g.drawLine(80,10,95,25);
g.drawLine(130,10,145,25);
g.drawLine(80,10,95,75);
g.drawLine(130,10,145,75);
g.drawString("(iii).Cone",200,110);
g.drawLine(200,15,175,85);
g.drawLine(200,15,225,85);
g.drawOval(175,80,50,15);
showStatus("applet with cylinder,cube,cone");
}
}
//HTML File
<html>
<body>
<applet code=sixth width=400 height=450>
</applet>
</body>
</html>
7 Write an applet that take 2 numbers as parameter and display their average and sum.
import java.applet.*;
import java.awt.*;
import java.util.Random.*;
public class u4_p7 extends Applet
{
/*public void init()
{
setBackground(Color.red);
setForeground(Color.yellow);
}*/
public void pain(Graphics g)
{
String str1,str2;
int a,b,sum=0,avg=0;
str1=getParameter("first");
str2=getParameter("secound");
a=Integer.parseInt(str1);
b=Integer.parseInt(str2);
sum=a+b;
avg=sum/2;
g.drawString("sum",50,10);
g.drawString("avg",50,40);
}
}
//HTML File
<html>
<body>
<applet code=u4_p7.class height=400 width=400>
<param name ="first" Value = "10">
<param name = "secound" Value="20">
</applet>
</body>
</html>
8 Write a Java applet that draws a circle centred in the centre of the applet. The radius of the circle should be passed as a parameter.
import java.applet.*;
import java.awt.*;
import java.util.Random.*;
public class u4_p8 extends Applet
{
public void paint(Graphics g)
{
Dimension d =getSize();
int xc=d.width/2;
int yc=d.width/2;
String str1;
int r1;
str1=getParameter("radius");
r1=Integer.parseInt(str1);
g.setColor(Color.cyan);
g.fillOval(xc-r1,yc-r1,2*r1,2*r1);
}
}
//HTML File
<html>
<body>
<applet code = u4_p8.class height=400 width=400>
<param name="radius" value="100">
</applet>
</body>
</html>
9 Write an applet that draw a circle divided in 6 equal parts.
import java.applet.*;
import java.awt.*;
public class u4_p9 extends Applet
{
public void paint(Graphics g)
{
g.drawOval(10,10,100,100);
g.drawLine(60,10,60,110);
g.drawLine(10,60,110,60);
g.drawLine(25,25,95,95);
g.drawLine(95,25,25,95);
}
}
//HTML File
<html>
<body>
<applet code=u4_p9.class height=400 width=400>
</applet>
</body>
</html>
10 Write an applet that draw a rectangle divided in 5 equal parts
No comments:
Post a Comment