Wednesday, 24 January 2018

Advancedjava-Slip5-Write a java program in multithreading using applet for drawing flag.



Slip5

Q1. Write a java program to create Teacher table(TNo.TName, Sal, Desg) and insert a record in it.         



import java.sql.*;
import java.io.*;
import javax.sql.*;

class slip2
{
    public static void main(String args[])
    {
        Connection con;
        Statement state;
        ResultSet rs;
        int ch;
      
            boolean flag=true;
            String decision;
            int no;
      
                       
        try
        {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            con = DriverManager.getConnection("jdbc:odbc:lab.dsn");

            System.out.println("Statement object created");

            do
            {
                System.out.println("\n");
                System.out.println("Menu:");
                System.out.println("1.create Table");
                System.out.println("2.Insert Record into the Table");
               System.out.println("3.Display all the Records from the Table");
                System.out.println("4.Exit");
                System.out.println("Enter your choice: ");

                BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                ch=Integer.parseInt(br.readLine());

                switch(ch)
                {
                         case 1:
                                                            state=con.createStatement();
                                                            String query="create table Teach"+"(no int ,"+" name varchar(20),"+" sal int,"+" design varchar(20))";
                                                            state.executeUpdate(query);
                                                            System.out.println("Table created");


                    case 2:
                    System.out.println("Enter Teacher Number: ");
                     no=Integer.parseInt(br.readLine());
                    
                   System.out.println("Enter Teacher Name: ");
                    String name=br.readLine();
                   
                                                            System.out.println("Enter Teacher Salary: ");
                    int sal=Integer.parseInt(br.readLine());

                    System.out.println("Enter Teacher designation: ");
                    String design=br.readLine();

                    String sql="insert into Teach values(?,?,?,?)";
                    PreparedStatement p=con.prepareStatement(sql);
                    p.setInt(1,no);
                    p.setString(2,name);
                    p.setInt(3,sal);
                    p.setString(4,design);

                    p.executeUpdate();
                    System.out.println("Record Added");
                    
                    break;
               

                    case 3:
                    state=con.createStatement();
                    sql="select * from Teach";
                    rs=state.executeQuery(sql);
                    while(rs.next())
                    {
                        System.out.println("\n");
                        System.out.print("\t" +rs.getInt(1));
                        System.out.print("\t" +rs.getString(2));
                        System.out.print("\t" +rs.getInt(3));
                        System.out.print("\t" +rs.getString(4));
                    }
                    break;

                    case 4:
                    System.exit(0);

                    default:
                    System.out.println("Invalid Choice");
                    break;
                }
            }while(ch!=4);
        }catch(Exception e)
        {
            System.out.println(e);
        }
    }
}

                                                                                                                           

Q2.  Write a MultiThreading program in java using Runnable interface to draw temple flag on an applet container.

 Method 1:-

import java.io.*;

import java.util.*;

import java.applet.Applet;

import java.awt.*;

 

 public class flag extends Applet implements Runnable

{

            Thread t;

            int x1,x2,x3,y3,x4,y4,x5,ln;

            public void init()

            {

                        t=new Thread(this);

                        t.start();

                        ln=1;

            }

            public void run()

            {

            try{      if(ln==1) {         for(x1=200;x1>100;)

                                               {

                                                       t.sleep(200);

                                                       repaint();

                                                }

                                       }

                        ln=2;

                        if(ln==2) {        for(x2=100;x2<150;)

                                                {

                                                          t.sleep(200);

                                                          repaint();

                                                }

                                      }

                        ln=3;

                        if(ln==3) {       for(x3=150,y3=100;x3>125&&y3<125;)

                                               {

                                                      t.sleep(200);

                                                      repaint();

                                              }

                                          }

                        ln=4;

                        if(ln==4) {     for(x4=125,y4=125;x4<150&&y4<150;)

                                            {

                                                t.sleep(200);

                                                repaint();

                                            }

                                       }

                        ln=5;

                        if(ln==5)  {     for(x5=150;x5>100;)

                                             {

                                                t.sleep(200);

                                                repaint();

                                              }

                                        }

                        ln=1;

            }catch(Exception e){

                                                      System.out.println(e);

                                             }

            run(); 

            }

            public void paint(Graphics g)

            {

                        if(ln==1&&x1>100)

                        {

                                    g.drawLine(100,200,100,x1-=5);

                        }

                        if(ln==2&&x2<150)

                        {

                                    g.drawLine(100,200,100,100);

                                    g.drawLine(100,100,x2+=5,100);

                        }

                        if(ln==3&&x3>125&&y3<125)

                        {

                                    g.drawLine(100,200,100,100);

                                    g.drawLine(100,100,150,100);

                                    g.drawLine(150,100,x3-=5,y3+=5);

                        }

                        if(ln==4&&x4<150&&y4<150)

                        {

                                    g.drawLine(100,200,100,100);

                                    g.drawLine(100,100,150,100);

                                    g.drawLine(150,100,125,125);

                                    g.drawLine(125,125,x4+=5,y4+=5);

                        }

                        if(ln==5&&x5>100)

                        {

                                    g.drawLine(100,200,100,100);

                                    g.drawLine(100,100,150,100);

                                    g.drawLine(150,100,125,125);

                                    g.drawLine(125,125,150,150);

                                    g.drawLine(150,150,x5-=5,150);

                        }                     

            }

}

 

flag.html

<html>

<body>

<applet code="flag.class" height=300 width=300>

</applet>

</body>

</html>

 

Method 2:-

 

B) Write a java program in multithreading using applet for drawing flag.

import java.awt.*;

public class Flag extends Frame{

int f = 0;

public Flag()

{

Signal s = new Signal();

s.start();

setSize(500,500);

setVisible(true);

}

public void paint (Graphics g)

{

switch (f)

{

case 0 :

g.drawLine(150, 50, 150, 300);

case 1 :

g.drawRect(150, 50, 100, 90);

}

}

class Signal extends Thread{

public void run(){

while(true){

f = (f+1)%2;

repaint();

try{

Thread.sleep(1000);

}catch(Exception e){

}

}

}

}

public static void main(String args[]){

new Flag();

}

}

Flag.html

<applet code="Flag.class" width="300" height="300">

</applet>