Write a Multithreading program in java to convert smile face into the crying face after 5 seconds and vice versa(Use Applet).

/*<applet code= "smileface.class" height="300" width="300"></applet>*/import java.awt.*;
import java.applet.*;
public class smileface extends Applet implements Runnable
{
    int aflag;
    Thread t;
    public void init() {
        t=new Thread(this); aflag=0;
        t.start();
    }
    public void run()
    {
        try        {
            if (aflag==0)
            {
                t.sleep(1000);
                aflag=1;
            } else            {
                t.sleep(1000);
                aflag=0;
            }
            repaint();
            run();
        }
        catch(Exception e)
        {
        }
    }
    public void paint(Graphics g) {
        34        g.drawOval(100,100,100,100);
        g.fillOval(120,125,20,20);
        g.fillOval(160,125,20,20);
        g.drawLine(150,135,150,165);
        if (aflag==0)
        { g.drawArc(140,160,20,20,0,-180);
            aflag=1;
        }
        else        {
            g.drawArc(140,160,20,20,0,180);
            aflag=0;
        }
    }
}

Write a java program which will display name and priority of current thread. Change name of Thread to MyThread and priority to 2. Display the details of Thread.

public class MainThread
{
    public static void main(String arg[])
    {
        Thread t=Thread.currentThread();
        System.out.println("Current Thread:"+t);
//Change Name        t.setName("My Thread ");
        System.out.println ("After the name is Changed:"+t);
        try        {
            for(int i=2;i>0;i--)
            {
                System.out.println(i);
                Thread.sleep(1000);
            }
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
    }
}
**OUTPUT***

        D:\TY\Adv Java>javac MainThread.java
        D:\TY\Adv Java>java MainThread
        Current Thread:Thread[main,5,main]
        After the name is Changed:Thread[My Thread ,5,main]
        2        1

Write a Multithreading program in java to display the number’s between 1 to 100 continuously in a TextField by clicking on button. (use Runnable Interface)

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class MultiThread extends JFrame implements ActionListener
{
    Container cc;
    JButton b1,b2;
    JTextField t1;
    MultiThread()
    {
        setVisible(true);
        setSize(1024,768);
        cc=getContentPane();
        setLayout(null);
        t1=new JTextField(500);
        cc.add(t1);
        t1.setBounds(10,10,1000,30);
        b1=new JButton("start");
        cc.add(b1);
        b1.setBounds(20,50,100,40);
        b1.addActionListener(this);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource()==b1)
        {
            new Mythread();
        }
    }
    class Mythread extends Thread
    {
        Mythread()
        {
        start();
        }
        public void run()
        {
            for(int i=1;i<=100;i++)
            {
                try                {
                    Thread.sleep(1000);
                }
                catch (InterruptedException e) {
                }
                t1.setText(t1.getText()+""+i+"\n");
//System.out.println()            }
        }
    }
    public static void main(String arg[])
    {
        new MultiThread().show();
    }
}

Write a java program using multithreading to execute the threads sequentially .(Use Synchronized Method)

class SynchronizationDemo
{
    synchronized void printTable(int n,String s2) //synchronized method synchronized    {
        System.out.println(s2);
        for(int i=1;i<=5;i++)
        {
            System.out.println(n*i);
            try            {
                Thread.sleep(400);
            }
            catch(Exception e)
            {
                System.out.println(e);
            }
        }
    }
}
class MyThread1 extends Thread
{
    Table t;
    String s1="Table of five";
    MyThread1(Table t)
    {
        this.t=t;
    }
    public void run()
    {
        t.printTable(7,s1);
    }
}
class MyThread2 extends Thread
{
    Table t;
    String s1="Table of Ten";
    MyThread2(Table t)
    {
        this.t=t;
    }
    29    public void run()
    {
        t.printTable(9,s1);
    }
}
public class SynchronizationDemo
{
    public static void main(String args[])
    {
        Table obj = new Table();//only one object        MyThread1 t1=new MyThread1(obj);
        MyThread2 t2=new MyThread2(obj);
        t1.start();
        t2.start();
    }
}

**OUTPUT***
        D:\TY\Adv Java>javac SynchronizationDemo.java
        D:\TY\Adv Java>java SynchronizationDemo
        Table of five
        7        14        21        28        35        Table of Ten
        9        18        27        36        45

Write a Multithreading program in java using Runnable interface to move text on the frame as follow

import java.awt.*;
import java.awt.event.*;
class MoveText extends Frame implements Runnable
{
    Label l1;
    Thread t;
    int x,y,side;
    public MoveText()
    {
        setLayout(null);
        l1=new Label("Sachin ");
        l1.setFont(new Font("",Font.BOLD,14));
        l1.setForeground(Color.red);
        setSize(400,400);
        setVisible(true);
        t=new Thread(this);
        t.start();
        x=5;
        y=200;side=1;
        addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent we)
            {
                System.exit(0);
            }
        });
    }
    public void run()
    {
        try        {
            if(side==1)
            {
                t.sleep(50);
                l1.setBounds(x+=5,y-=5,70,15);
                add(l1);
                26                if(y==20)
                    side=2;
            }
            if(side==2)
            {
                t.sleep(50);
                l1.setBounds(x+=5,y+=5,70,15);
                add(l1);
                if(y==200)
                    side=3;
            }
            if(side==3)
            {
                t.sleep(50);
                l1.setBounds(x-=5,y+=5,70,15);
                add(l1);
                if(y==390)
                    side=4;
            }
            if(side==4)
            {
                t.sleep(50);
                l1.setBounds(x-=5,y-=5,70,15);
                add(l1);
                if(x==0)
                {
                    side=1;
                    x=0;y=200;
                }
            }
        }catch(Exception e)
        {
            System.out.println(e);
        }
        run();
    }
    public static void main(String args[])
    {
        new MoveText();
    }
    27}

Write a java program to simulate traffic signal using multithreading

/*<applet code= "signal.class" height="600" width="600"></applet>*/import java.awt.*;
import java.applet.*;
public class signal extends Applet implements Runnable
{
    int r,g1,y,i;
    Thread t;
    public void init()
    {
        r=0;g1=0;y=0;
        t=new Thread(this);
        t.start();
    }
    public void run()
    {
        try        { for (i=24;i>=1;i--)
        {
            t.sleep(100);
            if (i>=16 && i<24)
            {
                g1=1;
                repaint();
            }
            if (i>=8 && i<16)
            {
                y=1;
                repaint();
            }
            if (i>=1 && i<8)
            {
                r=1;
                repaint();
            }
        }
            23            if (i==0)
            {
                run();
            }
        }
        catch(Exception e)
        {
        }
    }
    public void paint(Graphics g) {
        g.drawOval(100,100,100,100);
        g.drawOval(100,225,100,100);
        g.drawOval(100,350,100,100);
        g.drawString("start",200,200);
        if (r==1)
        { g.setColor(Color.red);
            g.fillOval(100,100,100,100);
            g.drawOval(100,100,100,100);
            g.drawString("stop",200,200);
            r=0;
        }
        if (g1==1)
        { g.setColor(Color.green);
            g.fillOval(100,225,100,100);
            g1=0;
            g.drawOval(100,225,100,100);
            g.drawString("go",200,200);
        }
        if (y==1)
        { g.setColor(Color.yellow);
            g.fillOval(100,350,100,100);
            y=0;
            g.drawOval(100,350,100,100);
            g.drawString("slow",200,200);
        }
    }
}

Write a menu driven java program for the following

import java.io.*;
import java.sql.*;
class Menu
{
    public static void main(String args[])
    {
        DataInputStream din=new DataInputStream(System.in);
        int rno,k,ch,per;
        String nm;
        try        {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection cn=DriverManager.getConnection("jdbc:odbc:BCA");
            Statement st=cn.createStatement();
            do            {
                System.out.println(" 1. Insert \n 2. Update \n 3. Delete \n 4. Search \n 5. Display \n 6. Exit");
                System.out.print("Enter your choice: ");
                ch=Integer.parseInt(din.readLine());
                System.out.println("............................................");
                switch(ch)
                {
                    case 1:
                        System.out.print("How many records you want to inserted ? ");
                        Int n=Integer.parseInt(din.readLine());
                        for(int i=0;i<n;i++)
                        {
                            System.out.println("Enter Roll No : ");
                            rno=Integer.parseInt(din.readLine());
                            System.out.println("Enter Name : ");
                            nm=din.readLine();
                            System.out.println("Enter Percentage: ");
                            per=Integer.parseInt(din.readLine());
                            18                            k=st.executeUpdate("insert into Stud values(" + rno + ",'"+ nm + "'," + per +")");
                            if(k>0)
                            {
                                System.out.println("Record Inserted Successfully..!!");
                                System.out.println("..............................................");
                            }
                        }
                        break;
                    case 2:
                        System.out.print("Enter the Roll no: ");
                        rno=Integer.parseInt(din.readLine());
                        System.out.print("Enter the Sname: ");
                        nm=din.readLine();
                        k=st.executeUpdate("update Stud set sname='" + nm + "' where rno="+rno);
                        if(k>0)
                        {
                            System.out.println("Record Is Updated..!!");
                        }
                        System.out.println("...............................................");
                        break;
                    case 3:
                        System.out.print("Enter the Roll no: ");
                        rno=Integer.parseInt(din.readLine());
                        k=st.executeUpdate("delete from Stud where rno=" +rno);
                        if(k>0)
                        {
                            System.out.println("Record Is Deleted..!!");
                        }
                        System.out.println(".............................................");
                        break;
                    case 4:
                        System.out.print("Enter the Roll no Whoes search record: ");
                        rno=Integer.parseInt(din.readLine());
                        System.out.println(".............................................");
                        ResultSet rs1=st.executeQuery("select * from Stud where rno=" +rno);
                        while(rs1.next())
                        {
                            System.out.println(rs1.getInt(1) +"\t" +rs1.getString(2)+"\t"+rs1.getInt(3));
                        }
                        19                        System.out.println(".........................................");
                        break;
                    case 5:
                        ResultSet rs=st.executeQuery("select * from Stud");
                        while(rs.next())
                        {
                            System.out.println(rs.getInt(1) +"\t" +rs.getString(2)+"\t"+rs.getInt(3));
                        }
                        System.out.println(".............................................");
                        break;
                    case 6:
                        System.exit(0);
                }
            }
            while(ch!=6);
        }
        catch(Exception e)
        {
            System.out.println("Error");
        }
    }
}

**OUTPUT**
D:\TY\Adv Java>javac Menu.java D:\TY\Adv Java>java Menu 1. Insert 2. Update 3. Delete 4. Search 5. Display 6. Exit Enter your choice: 1 ............................................ How many records you want to inserted ? 2 ............................................ Enter Roll No : 319 Enter Name : Yogesh Jagtap 20 Enter Percentage: 75 Record Inserted Successfully..!! .............................................. Enter Roll No : 342 Enter Name : Sachin Shinde Enter Percentage: 70 Record Inserted Successfully..!! .............................................. 1. Insert 2. Update 3. Delete 4. Search 5. Display 6. Exit Enter your choice: 2 ............................................ Enter the Roll no: 342 Enter the Sname: Sachin Shinde Record Is Updated..!! 1. Insert 2. Update 3. Delete 4. Search 5. Display 6. Exit Enter your choice: 4 ............................................ Enter the Roll no Whoes search record: 319 ............................................. 319 Yogesh Jagtap 75 ......................................... 1. Insert 2. Update 3. Delete 4. Search 5. Display 21 6. Exit Enter your choice: 3 ............................................ Enter the Roll no: 319 Record Is Deleted..!! ............................................. 1. Insert 2. Update 3. Delete 4. Search 5. Display 6. Exit Enter your choice: 5 ............................................ 342 Sachin Shinde 70 ............................................. 1. Insert 2. Update 3. Delete 4. Search 5. Display 6. Exit