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”.
class
eventhread extends Thread
{
int ll,up;
eventhread(int x, int y)
{
ll=x;
up=y;
}
public void run()
{
try
{
for(int i=ll;i<=up;i++)
{
if(i%2==0)
{
Thread.sleep(300);
System.out.println("Even
thread "+i);
}
}
}catch(Exception e){e.printStackTrace(); }
}
}
class
oddthread extends Thread
{
int ll,up;
oddthread(int x, int y)
{
ll=x;
up=y;
}
public void run()
{
for(int i=ll;i<=up;i++)
{
if(i%2!=0)
{
System.out.println("Odd thread "+i);
}
}
}
}
class u4_p1
{
public static void main(String args[])
{
int ll= Integer.parseInt(args[0]);
int up=Integer.parseInt(args[1]);
eventhread e1= new eventhread(ll,up);
oddthread o1=new oddthread(ll,up);
e1.start();
o1.start();
try
{
e1.join();
o1.join();
}
catch(InterruptedException e){}
}
System.out.println("\n Both threads completed");
}
}
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
Mythread extends Thread
{
public void run()
{
try
{
for(int i=1;i<=10;i++)
{
Thread.sleep(500);
System.out.println('x');
}
}catch(Exception e){e.printStackTrace(); }
}
}
class
Fivethread
{
public static void main(String args[])
{
Mythread m[] =new Mythread[5];
for(int i=0;i<5;i++)
{
m[i]=new Mythread();
}
for(int i=0;i<5;i++)
{
m[i].start();
}
for(int i=0;i<5;i++)
{
try
{
m[i].join();
}
catch(InterruptedException e){}
}
System.out.println("\n 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
sum1 implements Runnable
{
int sum=0;
float avg =0.0f;
public void run()
{
for(int i=1;i<=10;i++)
{
sum=sum+i;
avg=(float)sum/i;
System.out.println("\n In thread
sum1 Sum is" + sum+ "Avg is
"+avg);
}
}
}
class
sum2 implements Runnable
{
int sum=0;
float avg =0.0f;
public void run()
{
for(int i=11;i<=20;i++)
{
sum=sum+i;
avg=(float)sum/i;
System.out.println("\n In thread
sum2 Sum is" + sum+ "Avg is
"+avg);
}
}
}
class u4_p3
{
public static void main(String args[])
{
sum1 s1= new sum1();
Thread th1= new Thread(s1);
sum2 s2=new sum2();
Thread th2= new Thread(s2);
th1.start();
th2.start();
try
{
th1.join();
th2.join();
}
catch(Exception
e){}
System.out.println("Task
Completed");
}
}
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 one
extends Thread
{
public void run()
{
for(int i=1;i<=4;i++)
{
System.out.println("\n In thread
one FYBCA" );
}
}
}
class
second extends Thread
{
public void run()
{
for(int i=1;i<=6;i++)
{
System.out.println("\n In thread
second SYBCA" );
}
}
}
class u4_p4
{
public static void main(String args[])
{
one o1= new one();
second s2=new second();
o1.setPriority(1);
s2.setPriority(10);
o1.start();
s2.start();
try
{
o1.join();
s2.join();
}
catch(Exception
e){}
System.out.println("\n In main thread
TYBCA");
}
}
5.
Create an applet which draws a line,
rectangle and filled circle in applet display area.
import
java.applet.*;
import
java.awt.*;
/*
<applet
code=u4_p5.class height=400 width=400>
</applet>*/
public
class u4_p5 extends Applet
{
public void paint(Graphics g)
{
g.drawString("Line ,Rectangle and
filled circle",10,10);
g.drawRect(20,20,80,80);
g.fillRect(120,20,80,80);
g.fillOval(120,120,30,50);
g.drawLine(10,140,200,200);
}
}
6.Write applets to draw the following
shapes.
a. cone
b. cylinder
c. cube
import
java.applet.*;
import
java.awt.*;
/*
<applet
code=u4_p6.class height=400 width=400>
</applet>*/
public
class u4_p6 extends Applet
{
public void paint(Graphics g)
{
g.drawString("cone ,cylinder and
cube",10,10);
g. drawOval(100,100,70,30);
g. drawLine(100,120,130,200);
g. drawLine(170,120,130,200);
g. drawOval(200,100,70,30);
g. drawOval(200,150,70,30);
g. drawLine(200,120,200,170);
g. drawLine(270,120,270,170);
g.drawRect(270,270,80,80);
g.drawRect(300,300,80,80);
g. drawLine(270,270,300,300);
g. drawLine(270,350,300,380);
g. drawLine(350,270,380,300);
g. drawLine(350,350,380,380);
}
}
7.Write an applet that take 2 numbers as
parameter and display their average and
sum.
import
java.applet.*;
import
java.awt.*;
/*<APPLET
CODE=u4_p7.class WIDTH=500 HEIGHT=400>
<PARAM NAME =
"string1" VALUE = "20">
<PARAM
NAME="string2" VALUE="40">
</APPLET>*/
public
class u4_p7 extends Applet
{
String str1,str2,str3,str4,str5;
int t1,t2,t3,sum,avg;
public void init()
{
//receiving paramete
value
str1 =
getParameter("string1");
str2 =
getParameter("string2");
t1= Integer.parseInt(str1);
t2=
Integer.parseInt(str2);
sum = t1+t2;
avg = sum/2;
}
public void paint(Graphics g)
{
g.drawString("First
Value :-", 10, 100);
g.drawString(str1, 150,
100);
g.drawString("Second
Value:-", 10, 150);
g.drawString(str2,150,150);
g.drawString("Total
:-", 10, 250);
g.drawString("
"+sum,150,250);
g.drawString("Average
:-", 10, 300);
g.drawString("
"+avg,150,300);
}
}
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.*;
/*<APPLET
CODE=u4_p8.class WIDTH=500 HEIGHT=400>
<PARAM NAME =
"string1" VALUE = "50">
</APPLET>*/
public
class u4_p8 extends Applet
{
String str1;
int t1;
public void init()
{
str1 =
getParameter("string1");
t1=
Integer.parseInt(str1);
}
public void paint(Graphics g)
{
g.drawOval(250,250,t1,t1);
}
}//end of
ParaDemo
9 Write an applet that draw a circle
divided in 6 equal parts.
import java.applet.*;
import java.awt.*;
/*
<applet code=appdemo.class height=400 width=400>
</applet>*/
public class appdemo extends Applet
{
Color colors[]= {Color.black,
Color.blue,
Color.cyan,
Color.yellow,
Color.red,
Color.green};
public void paint(Graphics g)
{
g.drawString("Circle in 6 parts",10,10);
for(int i=0;i<6;i++)
{
g.setColor(colors[i]);
g.fillArc(80,80,100,100,60*i,60);
} } }
10. Write an applet that draw a rectangle divided
in 5 equal parts.
import
java.applet.*;
import
java.awt.*;
/*
<applet
code=u4_p10.class height=400 width=400>
</applet>*/
public class
u4_p10 extends Applet
{
Color
colors[]= {
Color.black,
Color.blue,
Color.cyan,
Color.yellow,
Color.red,
};
public void
paint(Graphics g)
{
//g.drawString("Rectangle
in 5 parts",10,10);
for(int
i=0;i<5;i++)
{
g.setColor(colors[i]);
g.fillRect(i*80,0,80,400);
} } }