• Click below to download Unit 4 programs in pdf file: -




Unit 4


/* 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" */


public class p1 

{

public static void main(String[] args)

{

String s = args[0];


Runnable r = new oddthread(s);

Thread t = new Thread(r); 

Runnable r2 = new eventhread(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 Thread: Both threads completed");

}

}


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 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 p2

{

 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("\nThread1 -  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("\nThread2  -  Sum is : " + sum+ "  Avg is : "+avg);  

        }


   }


}


class 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("\n ---- 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("\nThread 1  -  FYBCA" );         

        }

   }

}


class second extends Thread


{

    public void run()


    {

        for(int i=1;i<=6;i++)


       {

             System.out.println("\nThread 2  -  SYBCA" );              

        }

   }


class 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 ---- 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=p5.class height=400 width=400>

</applet> 

*/ 


public class 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=p6.class height=400 width=400>


</applet>*/ 


public class 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=p7.class WIDTH=500 HEIGHT=400>


                        <PARAM NAME = "string1" VALUE = "20">


                        <PARAM NAME="string2" VALUE="40">


                        </APPLET>*/


public class p7 extends Applet


{

            String str1,str2,str3,str4,str5;


            int t1,t2,t3,sum,avg;


            public void init()


            {


                        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=p8.class WIDTH=500 HEIGHT=400>


                        <PARAM NAME = "string1" VALUE = "50">


                        </APPLET>*/


public class 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);

            }

}



/* 9.Write an applet that draw a circle divided in 6 equal parts. */


import java.applet.*;

import java.awt.*;


/*

<applet code=p9.class height=400 width=400>

</applet>*/


public class p9 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);


}

}



/* p10.Write an applet that draw a rectangle divided in 5 equal parts. */


import java.applet.*;

import java.awt.*;


/*

<applet code=p10.class height=400 width=400>

</applet>*/


public class p10 extends Applet

{

Color colors[]= {Color.black,

Color.blue,

Color.cyan,

Color.yellow,

Color.red,

};


public void paint(Graphics g)

{

for(int i=0;i<5;i++)

{

g.setColor(colors[i]);

g.fillRect(i*80,0,80,400);

}