Showing posts with label JAVA. Show all posts
Showing posts with label JAVA. Show all posts

Tuesday, April 1, 2014

TO STUDY HASH FUNCTIONS IN JAVA


package hashfn;
import java.util.*;
import java.security.MessageDigest;
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)throws Exception {
        Scanner sc=new Scanner(System.in);
        String s;
            System.out.println("Enter the string");
            s=sc.nextLine();
            MessageDigest md=MessageDigest.getInstance("MD5");
            byte[] databytes=new byte[1024];
            databytes=s.getBytes();
            md.update(databytes);
            byte[] mdbytes=md.digest();
            StringBuffer hexString = new StringBuffer();
for (int i=0;i<mdbytes.length;i++) {
  String hex=Integer.toHexString(0xff & mdbytes[i]);
  if(hex.length()==1) hexString.append('0');
  hexString.append(hex);
}
            System.out.println(hexString);
    }

}

TO IMPLEMENT GENERAL CAESAR CIPHER IN JAVA

import java.util.*;

public class gencaesar
{
public static void main(String[] args) throws Exception
{
String input,output;
String a,b;
Scanner sc= new Scanner(System.in);

System.out.println("Enter the message");
input=sc.nextLine();
char array[]=input.toCharArray();
int key,i=0;
System.out.println("Enter the key");
key=sc.nextInt();
//adding the key
for(i=0;i<array.length;i++)
{
array[i]+=key;
if(array[i]>=90)
{
array[i]-=26;
}
if(array[i]>=129)
{
array[i]-=64;
}

}
//printing output
System.out.println("General Caesar Cipher: \n");
for(i=0;i<array.length;i++)
{
System.out.print(array[i]);

}
System.out.println();

//decyption
for(i=0;i<array.length;i++)
{
array[i]-=key;
if(array[i]>=129)
{
array[i]-=64;
}
if(array[i]>='Z' && array[i]<='a')
{
array[i]-=26;
}

}

//printing output
System.out.println("DECRYPTED PLAIN: \n");
for(i=0;i<array.length;i++)
{
//System.out.print(array[i]);

}
System.out.println(input);

//PROGRAM IS CODED BY 6483
}
}
/*
bash-3.00$ javac gencaesar.java
Enter the message
bash-3.00$ javac gencaesar.java
bash-3.00$ java gencaesar
Enter the message
xyz
Enter the key
5
General Caesar Cipher:

cde
DECRYPTED PLAIN:

xyz

*/

TO IMPLEMENT COVERT CHANNEL USING JAVA

package covert;
/*
THREAD1:
TAKE I/P FRM USR
WHEN USR ENTERS 1 A FILE SHUD BE CREATED
WHEN HE ENTERS 0 IT SHOULD BE DESTROYED

THREAD 2 :
KEEP CHECKING FOR FILE IF EXISTS DISPLAY 1
IF NOT DISPLAY 0


*/
import java.io.File;

import java.util.*;

/**
 *
 * @author 6483
 */
public class Covert {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
 
        A a=new A();
    a.start();
    B b=new B();
    b.start();
           
    }
}

class A extends Thread
{
        public static String ar="";
   public static Scanner sc=new Scanner(System.in);
boolean flag=true;
    File file;
    public void run ()
    {
       
        try
        {
           
        while(flag)
        {
           
            System.out.println("Enter BIT : ");              
            ar=sc.nextLine();
       
      if(ar.equals("1"))
      {
          file=new File("/home/student/2011/6483/Desktop/dummy.txt");
          file.createNewFile();
          ar="";
      }
      else if(ar.equals("0"))
      {
          file.delete();
          ar="";
      }
      else if(ar.equalsIgnoreCase("exit"))
      {
         
          System.exit(0);
      }
      sleep(5000);
     
      }
        }catch(Exception e){e.printStackTrace();}
        }
}

class B extends Thread
{
    void check(File file)
    {
     
     
       if(file.exists())
            {
            System.out.println("THE INCOMING BIT IS : 1");              
            }
            else
        {
            System.out.println("THE INCOMING BIT IS : 0");                  
        }
   
    }
    public void run ()
    {
        boolean flag=true;
        try
        {
           
        while(flag)
        {
           
       
            File file=new File("/home/student/2011/6483/Desktop/dummy.txt");
     
            check(file);
            sleep(5000);
        }
        }catch(Exception e){e.printStackTrace();}
        }
}

Thursday, October 31, 2013

FACADE DESIGN PATTERN IN JAVA

/*
FACADE DESIGN PATTERN IN JAVA
*/
class Client
{
public static void main(String args[]){

OrderFacade orderFacade = new OrderFacade();

orderFacade.placeOrder("OR123456");

System.out.println("Order processing completed");
}

}

 class OrderFacade {
private Payment pymt = new Payment();
private Inventory inventry = new Inventory();

public void placeOrder(String orderId) {

String step1 = inventry.checkInventory(orderId);

String step2 = pymt.deductPayment(orderId);

System.out.println("Following steps completed:" + step1+ " & " + step2);

}

}
 class Payment {
public String deductPayment(String orderID)
{
System.out.println("TOKEN "+orderID+" recieved\nProcessing payment...\n");
return "Payment deducted successfully";

}
}

class Inventory {
public String checkInventory(String OrderId)
{
System.out.println("TOKEN "+OrderId+" recieved\nProcessing Inventory...\n");
return "Inventory checked";
}
}






/*
OUTPUT:
TOKEN OR123456 recieved
Processing Inventory...

TOKEN OR123456 recieved
Processing payment...

Following steps completed:Inventory checked & Payment deducted successfully
Order processing completed
*/

OBSERVER DESIGN PATTERN IN JAVA

/*
OBSERVER DESIGN PATTERN IN JAVA
*/
import java.util.*;
class subject
{

person p1,p2;

subject()
{
p1=new person();
p2=new person();
}
void notify(boolean a)
{
if(a)
{
p2.state=p1.state="changed";

System.out.println(p1);
System.out.println(p2);
}
else
{
System.out.println(p1);
System.out.println(p2);
}

}
public static void main(String[] args)
{
boolean executed=false;
String choice;
subject s=new subject();
Scanner sc=new Scanner(System.in);
while(!executed)
{
System.out.println("SHOULD WE CHANGE STATE ?  Y/N");
choice=sc.nextLine();
if(choice.equals("Y") || choice.equals("y"))
{
s.notify(true);
executed=true;
}
else
s.notify(false);
}
}
}
class person
{//person is the observer;
String state;
person()
{
state="unchanged";
}
public String toString()
{
return state;
}

}

/*
OUTPUT
SHOULD WE CHANGE STATE ?  Y/N
n
unchanged
unchanged
SHOULD WE CHANGE STATE ?  Y/N
n
unchanged
unchanged
SHOULD WE CHANGE STATE ?  Y/N
n
unchanged
unchanged
SHOULD WE CHANGE STATE ?  Y/N
n
unchanged
unchanged
SHOULD WE CHANGE STATE ?  Y/N
y
changed
changed
*/

Wednesday, October 30, 2013

TO IMPLEMENT SHORTEST JOB FIRST IN JAVA

/*
TOPIC : TO IMPLEMENT SHORTEST JOB FIRST IN JAVA
*/

import java.util.*;
public class SJF
{
  public static void main(String args[])throws Exception{
Scanner s=new Scanner(System.in);
int n=0;
System.out.println("Enter no of processes :");
n=s.nextInt();
Process[] p=new Process[n];
for(int i=0;i<n;i++)
{
   p[i]=new Process();
}
for(int i=0;i<n;i++)
{
  System.out.println("PROCESS ID :");
p[i].id=s.nextInt();
System.out.println("ARRIVAL TIME :");
p[i].atime=s.nextInt();
System.out.println("Burst TIME :");
p[i].btime=s.nextInt();
}
for(int i=0;i<n-1;i++)
{
   for(int j=0;j<n-1;j++)
{
   if(p[j].btime>p[j+1].btime)
{
  Process t=p[j];
   p[j]=p[j+1];
   p[j+1]=t;
}
}
}
System.out.println("Process id\tArrival time\tBurst time");
System.out.println("...............................");
for(int i=0;i<n;i++)
{
   System.out.println(""+p[i].id+"\t\t"+p[i].atime+"\t\t"+p[i].btime);
}
}
}
class Process {
  int id,btime,atime;
}
/* OUTPUT
Enter no of processes :
2
PROCESS ID :
99
ARRIVAL TIME :
20
Burst TIME :
12
PROCESS ID :
10
ARRIVAL TIME :
21
Burst TIME :
32
Process id      Arrival time    Burst time
...............................
99              20              12
10              21              32
*/

TO IMPLEMENT ROUND ROBIN TIME SLICING IN JAVA

/*
TO IMPLEMENT ROUND ROBIN TIME SLICING IN JAVA
*/

import java.util.*;
public class RR
{
public static void main(String arg[])throws Exception
{
Scanner s=new Scanner(System.in);
int n,quan,tat;
System.out.println("Enter no of processes:");
n=s.nextInt();
System.out.println("enter time quantum:");
quan=s.nextInt();
Process p[]=new Process[n];
for(int i=0;i<n;i++)
{
p[i]=new Process();
}
for(int i=0;i<n;i++)
{
System.out.println("PROCESS ID:");
p[i].id=s.nextInt();
System.out.println("ARRIVAL TIME:");
p[i].atime=s.nextInt();
System.out.println("Burst time:");
p[i].btime=s.nextInt();
}
for(int i=0;i<n-1;i++)
{
for(int j=0;j<n-1;j++)
{
if(p[j].atime>p[j+1].atime)
{
Process t=p[j];
p[j]=p[j+1];
p[j+1]=t;
}
}
}
System.out.println("Process id\tArrival time\tBurst time");
System.out.println("...................................");
tat=0;
boolean flag=true;
int pass=0;
while(pass!=2)
{
for(int i=0;i<n;i++)
{
if(flag==true)
{
p[i].rbt=p[i].btime;
p[i].rbt=p[i].rbt-quan;
}
else
{
p[i].rbt=p[i].rbt-quan;
}
if(p[i].rbt>-4)
{
if(p[i].rbt<0)
p[i].rbt=0;
System.out.println(""+p[i].id+"\t\t"+p[i].atime+"\t\t"+p[i].btime+"\t\t"+p[i].rbt);
}
}
flag=false;
pass++;
}
}
}
class Process
{
int id,btime,atime,rbt;
Process()
{
id=0;
atime=0;
btime=0;
rbt=0;
}
}

/* OUTPUT
Enter no of processes:
2
enter time quantum:
12
PROCESS ID:
32
ARRIVAL TIME:
21
Burst time:
125
PROCESS ID:
45
ARRIVAL TIME:
12
Burst time:
12
Process id      Arrival time    Burst time
...................................
45              12              12              0
32              21              125             113
32              21              125             101
*/

TO IMPLEMENT PROCESS PRIORITY IN JAVA

/*

Title   :TO IMPLEMENT PROCESS PRIORITY IN JAVA
*/

import java.util.*;
public class Priority
{
  public static void main(String args[])throws Exception{
Scanner s=new Scanner(System.in);
int n=0;
System.out.println("Enter no of processes :");
n=s.nextInt();
Process[] p=new Process[n];
for(int i=0;i<n;i++)
{
   p[i]=new Process();
}
for(int i=0;i<n;i++)
{
  System.out.println("PROCESS ID :");
p[i].id=s.nextInt();
System.out.println("ARRIVAL TIME :");
p[i].atime=s.nextInt();
System.out.println("Burst TIME :");
p[i].btime=s.nextInt();
System.out.println("Priority :");
p[i].prior=s.nextInt();
}
for(int i=0;i<n-1;i++)
{
   for(int j=0;j<n-1;j++)
{
   if(p[j].prior>p[j+1].prior)
{
  Process t=p[j];
   p[j]=p[j+1];
   p[j+1]=t;
}
}
}
System.out.println("Process id\tArrival time\tBurst time\tPriority");
System.out.println("...............................");
for(int i=0;i<n;i++)
{
   System.out.println(""+p[i].id+"\t\t"+p[i].atime+"\t\t"+p[i].btime+"\t\tp"+p[i].prior);
}
}
}
class Process {
  int id,btime,atime,prior;
}

/*OUTPUT
Enter no of processes :
2
PROCESS ID :
12
ARRIVAL TIME :
14
Burst TIME :
45
Priority :
1
PROCESS ID :
21
ARRIVAL TIME :
23
Burst TIME :
45
Priority :
2
Process id      Arrival time    Burst time      Priority
...............................
12              14              45              p1
21              23              45              p2
*/

TO IMPLEMENT MULTITHREADING AND OBSERVE TIMESLICING

/*

TO IMPLEMENT MULTITHREADING AND OBSERVE TIMESLICING
*/
import java.util.*;
class q extends Thread
{
public void run()
{
try{
for(int i=0;i<12;i++)
{
System.out.println("12"+"x"+i+"="+(i*12));
Thread.sleep(2000);
}
}catch(Exception e){}
}
}
class p extends Thread
{
public void run()
{
try{
for(int i=0;i<12;i++)
{
System.out.println("10"+"x"+i+"="+(i*10));
Thread.sleep(2000);
}
}catch(Exception e){}
}
}
class multi
{
public static void main(String args[]) throws Exception
{
p a=new p();
q b=new q();
a.start();
b.start();
//THIS PROGRAM IS CODED BY TG;
}
}
/*
OUTPUT
 javac multi.java
bash-4.1$ java multi
10x0=0
12x0=0
10x1=10
12x1=12
10x2=20
12x2=24
10x3=30
12x3=36
10x4=40
12x4=48
10x5=50
12x5=60
10x6=60
12x6=72
10x7=70
12x7=84
10x8=80
12x8=96
10x9=90
12x9=108
10x10=100
12x10=120
10x11=110
12x11=132
*/

TO IMPLEMENT LRU IN JAVA

/*
TO IMPLEMENT LRU IN JAVA

*/
import java.util.*;

class Queue
{
int q[]=new int[3];
int f=0;
int count=0;
 void insert(int d)
{

if(f!=3)
{

if(find(d) && count==2)
{System.out.print("\nHIT");f++;}
else
q[f++]=d;
}

else
{
f=0;

       if(find(d) && count==2)
{System.out.print("\nHIT");f++;}
else
q[f++]=d;
}
System.out.print("\nTop:"+f);
}

 boolean find(int d)
  {
if(count==3) count=2;
for(int i=0;i<=count;i++)
{
if(d==q[i] && count>=2)
return true;


}


return  false;
}
 void traverse()
{
        if(count==3) count=2;
for(int i=0;i<=count;i++)
{

System.out.print("\n"+q[i]);
}
if(count<3) count++;
System.out.println();
}

 }


class LRU
{
public static void main(String args[])
{
Scanner src=new Scanner(System.in);

                int a[]={7,0,1,2,0,3,0,4,2,3,0,3,2,1};
Queue q=new Queue();
for(int i=0;i<14;i++)
{
q.insert(a[i]);
q.traverse();
}
}
}


TO IMPLEMENT FIFO-First In First Out

/*
TO IMPLEMENT FIFO-First In First Out
*/
import java.util.*;

class Queue
{
int q[]=new int[3];
int f=0;
int count=0;
 void insert(int d)
{
if(f!=3)
{

if(q[f]==d && count==2)
{System.out.print("\nHIT");}
else
q[f++]=d;
}

else
{
f=0;

       if(q[f]==d && count==2)
{System.out.print("\nHIT");}
else
q[f++]=d;
}
}


 void traverse()
{
for(int i=0;i<=count;i++)
{

System.out.print("\n"+q[i]);
}
if(count<2) count++;
System.out.println();
}

 }


class FIFO
{
public static void main(String args[])
{
Scanner src=new Scanner(System.in);

                int a[]={7,0,1,2,0,3,0,4,2,3,0,3,2,1};
Queue q=new Queue();
for(int i=0;i<14;i++)
{
q.insert(a[i]);
q.traverse();
}
}
}

/*OUTPUT

bash-3.00$ java FIFO

7

7
0

7
0
1

2
0
1

HIT
2
0
1

2
3
1

2
3
0

4
3
0

4
2
0

4
2
3

0
2
3

0
3
3

0
3
2

1
3
2

*/

TO IMPLEMENT First Come First Service

/*
TO IMPLEMENT First Come First Service

*/
import java.util.*;
public class FCFS
{
  public static void main(String args[])throws Exception{
Scanner s=new Scanner(System.in);
int n=0;
System.out.print("Enter no of processes :");
n=s.nextInt();
Process[] p=new Process[n];
for(int i=0;i<n;i++)
{
   p[i]=new Process();
}
for(int i=0;i<n;i++)
{
  System.out.print("PROCESS ID :");
p[i].id=s.nextInt();
System.out.println("ARRIVAL TIME :"+i);
p[i].atime=i;
System.out.print("Burst TIME :");
p[i].btime=s.nextInt();
}
/*for(int i=0;i<n-1;i++)
{
   for(int j=0;j<n-1;j++)
{
   if(p[j].atime>p[j+1].atime)
{
  Process t=p[j];
   p[j]=p[j+1];
   p[j+1]=t;
}
}
}
System.out.println("Process id\tArrival time\tBurst time");
System.out.println("...............................");
for(int i=0;i<n;i++)
{
   System.out.println(""+p[i].id+"\t\t"+p[i].atime+"\t\t"+p[i].btime);
}
*/
System.out.print("\nStarting to execute process");
for(int i=0;i<n;i++)
{
System.out.print("\nProcess "+p[i].id+" under execution");
Thread.sleep((p[i].btime)*1000);
System.out.println("\nProcess "+p[i].id+" executed");
}

}
}
class Process {
  int id,btime,atime;
}
/*OUTPUT  
bash-3.00$ java FCFS
bash-3.00$ java FCFS
Enter no of processes :3
PROCESS ID :1
ARRIVAL TIME :0
Burst TIME :7
PROCESS ID :2
ARRIVAL TIME :1
Burst TIME :1
PROCESS ID :3
ARRIVAL TIME :2
Burst TIME :3

Starting to execute process
Process 1 under execution
Process 1 executed

Process 2 under execution
Process 2 executed

Process 3 under execution
Process 3 executed

*/

SINGLETON PATTERN IN JAVA

/*
TEJAS J GHALSASI
TEIT
SINGLETON PATTERN IN JAVA
*/
import java.util.*;
public class NoticeBoard {
   
    private static NoticeBoard a;
    String message;
    /**
     * Create private constructor
     */
    private NoticeBoard(){
       
    }
    /**
     * Create a static method to get instance.
     */
    public static NoticeBoard getInstance(){
        if(a == null){
            a = new NoticeBoard();
        }
        return a;
    }
   
    public void getMessage(String msg){
        message=msg;
        System.out.println("message updated \n");
    }
   
    public void postMessage(){
       
        System.out.println("Current message is: "+message);
    }
   
    public static void main(String a[]){
    Scanner sc=new Scanner(System.in);
    NoticeBoard nb = NoticeBoard.getInstance();
        NoticeBoard nb1 = NoticeBoard.getInstance();
        System.out.println("enter message");
        String msg=sc.nextLine();
       
nb.getMessage(msg);
System.out.println("message stored in nb");
nb.postMessage();

System.out.println("message stored in nb1");
nb1.postMessage();
     
      System.out.println("enter a new message ");
       msg=sc.nextLine();
     
nb.getMessage(msg);
System.out.println("message stored in nb");
nb.postMessage();

System.out.println("message stored in nb1");
nb1.postMessage();
    }
}
/*
THIS PROGRAM IS CODED BY TG
*/

Tuesday, October 29, 2013

IMPLEMENTATION OF BRESNHAM LINE DRAWING ALGORITHM IN JAVA

/*
DATE:29TH JULY 2013
TITLE:IMPLEMENTATION OF BRESNHAM LINE DRAWING ALGORITHM IN JAVA
*/

import java.applet.*;
import java.awt.*;
import java.util.*;

public class bresnham extends Applet
{
    double x1,y1,x2,y2;
    double dx,dy,steps,x,y,k;

    Scanner sc=new Scanner(System.in);
    double xc,yc;
   
    public bresnham(){
    System.out.println("ENTER X1 Y1");
    x1=sc.nextDouble();
    y1=sc.nextDouble();
    System.out.println("ENTER X2 Y2");
    x2=sc.nextDouble();
    y2=sc.nextDouble();
    }
  
    public void paint(Graphics g)
    {
    int ch=6;
            dx=Math.abs(x2-x1);
        dy=Math.abs(y2-y1);
        x=x1;
        y=y1;
        double e=(2*dy)-(dx);
        int i=1;
        //THIS PROGRAM IS CODED BY TG6483
        one: for(int j=10;ch==6;j++)
        {
            g.fillOval((int)x,(int)y,5,5);
            while(e>=0)
            {
                y=y+1;
                e=e-(2*dx);
            }
            x=x+1;
            e=e+(2*dy);
            i++;
            if(i<=dx)
            ch=6;
            else
            break one;
        }
    }
}
/*<applet code="bresnham.class" width="1000" height="1000">
 </applet>*/
 /*
 OUTPUT:
 bash-3.00$ appletviewer bresnham.java
ENTER X1 Y1
500
200
ENTER X2 Y2
200
600
*/


Monday, October 28, 2013

DIJKSTRA'S ALGORITHM IN JAVA

/*
TOPIC: dijkstra's algorithm in java
*/
import java.util.*;

public class dij
{
        public static void main(String args[])
        {
                int d[][]={{0,9,3,0,0,0},
                           {2,0,6,5,3,0},
                           {3,6,0,0,1,0},
                           {0,5,0,0,2,2},
                           {0,3,1,1,0,4},
                           {0,0,0,2,4,0}};
                int vi[][]=new int[6][6];

                Vector v=new Vector();

                int s=0,de=5,c=0,cost=-1,co=0,temp=0;

                while(de!=c)
                {
                        for(int i=-1;i<=5;i++)
                        {
                                while(cost<=0)
                                {cost=d[++i][c]; temp=i;}

                                if(d[de][c]>0 && vi[de][c]==0)
                                { cost=d[de][c];  vi[de][c]=1; temp=de;   break;}

                                else
                                {
                                if(d[i][c]!=0 && d[i][c]<cost && vi[i][c]==0)
                                        { cost=d[i][c]; vi[i][c]=1;  temp=i; }
                                }

                        }
                        c=temp;
                        co=co+cost;
                        cost=-1;
                        v.add(c+1);

                }

                System.out.print("\nCost is : "+co);
                System.out.println("\nPath is: ");
                System.out.print(s+1+"  ");
                                Enumeration e=v.elements();
                                while(e.hasMoreElements())
                                System.out.print(e.nextElement()+"  ");
                                System.out.print("");
        }

}
/*
Cost is : 9
Path is:
1  2  5  6
*/

Saturday, October 26, 2013

SUTHERLAND HODGEMAN POLYGON CLIPPING ALGORITHM IN JAVA

/*
TOPIC: SUTHERLAND HODGEMAN POLYGON CLIPPING
*/

import java.awt.*;
import java.applet.*;
import java.math.*;
import java.io.*;
import java.util.*;

public class Polyclip extends Applet
{

Scanner d=new Scanner(System.in);
public void paint(Graphics g)
{
int a[];
int b[];
int k=0;
System.out.println("Enter no. of points ");
int n=d.nextInt();
a=new int[n];
b=new int[n];
for(int i=0;i<n;i++)
{
System.out.println("Enter value of x : ");
a[i]=d.nextInt();
System.out.println("Enter value of y : ");
b[i]=d.nextInt();
}
g.drawPolygon(a,b,n);

System.out.println("Enter value of x(min) : ");
int xmn=d.nextInt();
System.out.println("Enter value of y(min) : ");
int ymn=d.nextInt();
System.out.println("Enter value of x(max) : ");
int xmx=d.nextInt();
System.out.println("Enter value of y(max) : ");
int ymx=d.nextInt();

g.drawLine(xmn,ymn,xmx,ymn);
g.drawLine(xmn,ymx,xmx,ymx);
g.drawLine(xmn,ymn,xmn,ymx);
g.drawLine(xmx,ymn,xmx,ymx);

for(int i=0;i<4;i++)
{
if(b[i]>ymx)
{
int temp=b[i];
b[i]=ymx;
a[i]= a[i] +(((a[i+1]-a[i])/(b[i+1]-temp))*(b[i]-temp));
}
else if(b[i]<ymn)
{
int temp=b[i];
b[i]=ymn;
a[i]= a[i] +(((a[i+1]-a[i])/(b[i+1]-temp))*(b[i]-temp));

 }
else if(a[i]>xmx)
{
 int temp=a[i];
 a[i]=xmx;
 b[i]=b[i]+(((b[i+1]-b[i])/(a[i+1]-a[i]))*(a[i]-temp));
}
else if(a[i]<xmn)
{
 if(a[i]==a[i+1])
 {
 k=a[0];
a[i]=xmn;
b[i]=b[i];
 }
else  if(a[i+1]<xmn)
  {
   a[i]=xmn;
b[i]=ymx;
}
 else
{
int temp=a[i];
 a[i]=xmn;
 b[i]=b[i]+(((b[i+1]-b[i])/(a[i+1]-a[i]))*(a[i]-temp));
 }
}
 }
if(b[n-1]>ymx)
{
int temp=b[n-1];
b[n-1]=ymx;
a[n-1]= a[n-1]+(((a[0]-a[n-1])/(b[0]-temp))*(b[n-1]-temp));
}
else if(b[n-1]<ymn)
{
int temp=b[n-1];
b[n-1]=ymn;
a[n-1]= a[n-1] +(((a[0]-a[n-1])/(b[0]-temp))*(b[n-1]-temp));

 }
else if(a[n-1]>xmx)
{
 int temp=a[n-1];
 a[n-1]=xmx;
 b[n-1]=b[n-1]+(((b[0]-b[n-1])/(a[0]-a[n-1]))*(a[n-1]-temp));
}
else if(a[n-1]<xmn)
{
 if(a[n-1]==a[0])
 {
 int temp=a[n-1];
a[n-1]=xmn;
b[n-1]=ymn;
 }
else if(a[0]<xmn)
  {
   a[n-1]=xmn;
   b[n-1]=ymx;
}
 else
{
 int temp=a[n-1];
 a[n-1]=xmn;
 b[n-1]=b[n-1]+(((b[0]-b[n-1])/(k-a[n-1]))*(a[n-1]-temp));
 }
 }

for(int i=0;i<n;i++)
{
a[i]=a[i]+400;
b[i]=b[i]+400;
}
g.drawPolygon(a,b,n);
ymn+=400;
ymx+=400;


xmn+=400;
xmx+=400;


g.drawLine(xmn,ymn,xmx,ymn);
g.drawLine(xmn,ymx,xmx,ymx);
g.drawLine(xmn,ymn,xmn,ymx);
g.drawLine(xmx,ymn,xmx,ymx);

}
}

/*
<applet code="Polyclip.class" width="1000" height="1000">
</applet>
*/

LIYAN BASKY LINE CLIPPING ALGORITHM IN JAVA

/*
Topic-LIYAN BASKY LINE CLIPPING ALGORITHM IN JAVA
*/
import java.awt.*;
import java.applet.*;
import java.math.*;
import java.io.*;
import java.util.*;

public class LB extends Applet
{
Scanner d=new Scanner(System.in);
 public void paint(Graphics g)
 {
  System.out.println("enter value of xmin");
  int xmin=d.nextInt();
   System.out.println("enter value of ymin");
  int ymin=d.nextInt();
  System.out.println("enter value of xmax");
  int xmax=d.nextInt();
  System.out.println("enter value of ymax");
  int ymax=d.nextInt();
  int p[]=new int[4];
  int q[]=new int[4];
  System.out.println("enter value of x1");
  int x1=d.nextInt();
System.out.println("enter value of y1");
  int y1=d.nextInt();
  System.out.println("enter value of x2");
  int x2=d.nextInt();
  System.out.println("enter value of y2");
  int y2=d.nextInt();
  g.drawLine(xmin,ymin,xmin,ymax);
  g.drawLine(xmin,ymax,xmax,ymax);
  g.drawLine(xmax,ymax,xmax,ymin);
  g.drawLine(xmax,ymin,xmin,ymin);
  g.drawLine(x1,y1,x2,y2);
  p[0]=-(x2-x1);
  p[1]=x2-x1;
  p[2]=-(y2-y1);
  p[3]=y2-y1;
  q[0]=x1-xmin;
  q[1]=xmax-x1;
  q[2]=y1-ymin;
  q[3]=ymax-y1;
  float t1=0,t2=1;
  int flag=0;
  for(int i=0;i<4;i++)
  {
   if(p[i]==0)
   {
    if(q[i]>=0)
    {
     if(i<2)
     {
      if(y1<ymin)
       y1=ymin;
      if(y2>ymax)
       y2=ymax;
     }
     if(i>1)
     {
      if(x1<xmin)
      x1=xmin;
      if(x2>xmax)
      x2=xmax;
     }
     flag=1;
     g.drawLine(x1,y1,x2,y2);
    }
   }
  }
  if(flag==0)
  {
   for(int i=0;i<4;i++)
   {
    float t=(float)q[i]/(float)p[i];
    if(p[i]<0)
    {
     if(t1<t)
     t1=t;
    }
    else
    {
     if(t2>t)
     t2=t;
    }
   }
  if(t1<t2)
  {
   int xx1=(int)(x1+t1*p[1]);
   int xx2=(int)(x1+t2*p[1]);
   int yy1=(int)(y1+t1*p[3]);
   int yy2=(int)(y1+t2*p[3]);
  xmin =xmin + 200;
   ymin =ymin + 400;
  xmax =xmax + 200;
  ymax =ymax + 400;
   xx1 =xx1 + 200;
   xx2 =xx2 + 200;
   yy1 =yy1 + 400;
   yy2 =yy2 + 400;
    g.drawLine(xmin,ymin,xmin,ymax);
  g.drawLine(xmin,ymax,xmax,ymax);
  g.drawLine(xmax,ymax,xmax,ymin);
  g.drawLine(xmax,ymin,xmin,ymin);
  g.drawLine(xx1,yy1,xx2,yy2);
  }
  }
 }
}
/*
<applet code="LB.class" width=700 height=700>
</applet>
*/



TO IMPLEMENT CHARACTER GENERATION IN JAVA

/*
TOPIC:TO IMPLEMENT CHARACTER GENERATION IN JAVA
WRITE A PROGRAM TO GENERATE INDIA
*/
import java.awt.*;
import java.util.*;
import java.applet.*;

public class Chargen extends Applet
{
  public void paint(Graphics g)
  {

int d[][] = {

 { 0, 0, 0, 0, 1, 0, 0, 0, 0},

 { 0, 0, 0, 0, 1, 0, 0, 0, 0},

 { 0, 0,0 , 0, 1, 0,0 , 0, 0},

 { 0, 0, 0, 0, 1, 0, 0, 0, 0},

 { 0, 0, 0, 0, 1, 0, 0, 0, 0},

 { 0, 0, 0, 0, 1, 0, 0, 0, 0},

 { 0, 0, 0, 0, 1, 0, 0, 0, 0},

 { 0, 0, 0, 0, 1, 0, 0, 0, 0},

 { 0, 0, 0, 0, 1, 0, 0, 0, 0},

 { 0, 0, 0, 0, 1, 0, 0, 0, 0},

 { 0, 0, 0, 0, 1, 0, 0, 0, 0},

 { 0, 0, 0, 0, 1, 0, 0, 0, 0},

 { 0, 0, 0, 0, 1, 0, 0, 0, 0},

      };

for(int P=0;P<13;P++)
 {
   for(int W=0;W<9;W++)
   {
   if(d[P][W]==1)
    {
    g.fillOval(200+(15*W),200+(15*P),5,5);
    }
   else
    continue;
   }
  }

int e[][] = {

 { 0, 1, 0, 0, 0, 0, 0, 1, 0},

 { 0, 1, 1, 0, 0, 0, 0, 1, 0},

 { 0, 1, 1, 0, 0, 0, 0, 1, 0},

 { 0, 1, 0, 1, 0, 0, 0, 1, 0},

 { 0, 1, 0, 0, 0, 0, 0, 1, 0},

 { 0, 1, 0, 0, 1, 0, 0, 1, 0},

 { 0, 1, 0, 0, 0, 0, 0, 1, 0},

 { 0, 1, 0, 0, 0, 1, 0, 1, 0},

 { 0, 1, 0, 0, 0, 0, 0, 1, 0},

 { 0, 1, 0, 0, 0, 0, 1, 1, 0},

 { 0, 1, 0, 0, 0, 0, 0, 1, 0},

 { 0, 1, 0, 0, 0, 0, 1, 1, 0},

 { 0, 1, 0, 0, 0, 0, 0, 1, 0},

      };

for(int q=0;q<13;q++)
 {
   for(int x=0;x<9;x++)
   {
   if(e[q][x]==1)
    {
    g.fillOval(300+(15*x),200+(15*q),5,5);
    }
   else
    continue;
   }
  }


   int b[][] = {

 { 1, 1, 1, 1, 1, 1, 1, 1, 0},

 { 0, 1, 0, 0, 0, 0, 0, 1, 0},

 { 0, 1, 0, 0, 0, 0, 0, 1, 0},

 { 0, 1, 0, 0, 0, 0, 0, 1, 0},

 { 0, 1, 0, 0, 0, 0, 0, 1, 0},

 { 0, 1, 0, 0, 0, 0, 0, 1, 0},

 { 0, 1, 0, 0, 0, 0, 0, 1, 0},

 { 0, 1, 0, 0, 0, 0, 0, 1, 0},

 { 0, 1, 0, 0, 0, 0, 0, 1, 0},

 { 0, 1, 0, 0, 0, 0, 0, 1, 0},

 { 0, 1, 0, 0, 0, 0, 0, 1, 0},

 { 0, 1, 0, 0, 0, 0, 0, 1, 0},

 { 1, 1, 1, 1, 1, 1, 1, 1, 0},

      };

for(int k=0;k<13;k++)
 {
   for(int n=0;n<9;n++)
   {
   if(b[k][n]==1)
    {
    g.fillOval(450+(15*n),200+(15*k),5,5);
    }
   else
    continue;
   }
  }

 int c[][] = {

 { 0, 0, 0, 0, 1, 0, 0, 0, 0},

 { 0, 0, 0, 0, 1, 0, 0, 0, 0},

 { 0, 0,0 , 0, 1, 0,0 , 0, 0},

 { 0, 0, 0, 0, 1, 0, 0, 0, 0},

 { 0, 0, 0, 0, 1, 0, 0, 0, 0},

 { 0, 0, 0, 0, 1, 0, 0, 0, 0},

 { 0, 0, 0, 0, 1, 0, 0, 0, 0},

 { 0, 0, 0, 0, 1, 0, 0, 0, 0},

 { 0, 0, 0, 0, 1, 0, 0, 0, 0},

 { 0, 0, 0, 0, 1, 0, 0, 0, 0},

 { 0, 0, 0, 0, 1, 0, 0, 0, 0},

 { 0, 0, 0, 0, 1, 0, 0, 0, 0},

 { 0, 0, 0, 0, 1, 0, 0, 0, 0},

      };

for(int l=0;l<13;l++)
 {
   for(int o=0;o<9;o++)
   {
   if(c[l][o]==1)
    {
    g.fillOval(550+(15*o),200+(15*l),5,5);
    }
   else
    continue;
   }
  }
int a[][] = {

 { 0, 0, 0, 0, 1, 0, 0, 0, 0},

 { 0, 0, 0, 1, 0, 1, 0, 0, 0},

 { 0, 0, 1, 0, 0, 0, 1, 0, 0},

 { 0, 1, 0, 0, 0, 0, 0, 1, 0},

 { 0, 1, 0, 0, 0, 0, 0, 1, 0},

 { 0, 1, 0, 0, 0, 0, 0, 1, 0},

 { 0, 1, 1, 1, 1, 1, 1, 1, 0},

 { 0, 1, 0, 0, 0, 0, 0, 1, 0},

 { 0, 1, 0, 0, 0, 0, 0, 1, 0},

 { 0, 1, 0, 0, 0, 0, 0, 1, 0},

 { 0, 1, 0, 0, 0, 0, 0, 1, 0},

 { 0, 1, 0, 0, 0, 0, 0, 1, 0},

 { 0, 1, 0, 0, 0, 0, 0, 1, 0},

      };

for(int j=0;j<13;j++)
 {
   for(int m=0;m<9;m++)
   {
   if(a[j][m]==1)
    {
    g.fillOval(650+(15*m),200+(15*j),5,5);
    }
   else
    continue;
   }
  }

 }
}

/*
<applet code="Chargen.class" width="2000" height="2000">
</applet>
*/

TO IMPLEMENT BEIZER CURVE IN JAVA

/*
TOPIC:TO IMPLEMENT BEIZER CURVE IN JAVA
*/
import java.awt.*;
import java.lang.*;
import java.util.*;
import java.applet.*;

public class Beizer extends Applet
{
  public void paint(Graphics g)
  {
    Scanner s=new Scanner(System.in);
int i;
int temp1,temp2;

int c[][]=new int[4][2];

for(i=0;i<4;i++)
{
 System.out.println("Enter x coordinate of point");
 c[i][0]=s.nextInt();
 c[i][0]=100*c[i][0];
 System.out.println("Enter y coordinate of point");
 c[i][1]=s.nextInt();
 c[i][1]=100*c[i][1];
}
for(int m=0;m<3;m++)
{
 g.drawLine(c[m][0],c[m][1],c[m+1][0],c[m+1][1]);
}

int a[] = new int [52];
int b[] = new int [52];

int j=0;
double u=0;

try{
do
{
double d = (((1-u)*(1-u)*(1-u))*c[0][0]) + ((3*u)*(1-u)*(1-u)*c[1][0]) + ((3*u*u)*(1-u)*c[2][0]) + (u*u*u*c[3][0]);
double e = (((1-u)*(1-u)*(1-u))*c[0][1]) + ((3*u)*(1-u)*(1-u)*c[1][1]) + ((3*u*u)*(1-u)*c[2][1]) + (u*u*u*c[3][1]);
a[j] = (int)Math.round(d);
b[j] = (int)Math.round(e);
u=u +.02;
j=j+1;

}while(u!=1);
}
catch(Exception e){}
for(int k=0;k<51;k++)
 {
  g.setColor(Color.red);
  g.drawLine(a[k],b[k],a[k+1],b[k+1]);
}
 }
}
/*
<applet code="Beizer.class" width="1000" height="1000">
</applet>
*/

COHEN-SUTHERLAND LINE CLIPPING ALGORITHM IN JAVA

/*
COHEN-SUTHERLAND ALGORITHM
*/
import java.applet.*;
import java.awt.*;
import java.util.*;
public class CohenS extends Applet
{
    Scanner sc=new Scanner(System.in);
            int xmax,ymax,xmin,ymin;
            public int[] set(int x,int y)
            {
                        int a[]=new int[4];
                        if(x<xmin)
                                    a[3]=1;
                        else
                                    a[3]=0;
                        if(x>xmax)
                                    a[2]=1;
                        else
                                    a[2]=0;
                        if(y<ymin)
                                    a[0]=1;
                        else
                                    a[0]=0;
                        if(y>ymax)
                                    a[1]=1;
                        else
                                    a[1]=0;
                        return a;
            }
            void setminmax()
            {
            System.out.println("Enter xmax of the Rectangle");
            xmax=sc.nextInt();
            System.out.println("Enter ymax of the Rectangle");
            ymax=sc.nextInt();
            System.out.println("Enter xmin of the Rectangle");
            xmin=sc.nextInt();
            System.out.println("Enter ymin of the Rectangle");
            ymin=sc.nextInt();
           
            }
            boolean check(int a[])
            {
                        for(int i=0;i<a.length;i++)
                                    if(a[i]==1)
                                                return false;
                        return true;
            }
            int[] produceXY(int i,int x1,int y1,float m)
            {
                        int a[]=new int [2];
                        float x=0,y=0;
                        switch(i)
                        {
                                    case 0:
                                                x=xmin;
                                                y=y1+m*(x-x1);
                                                break;
                                  
                                    case 1:
                                                x=xmax;         
                                                y=y1+m*(x-x1);
                                                break;
                                    case 3:
                                                y=ymin;
                                                x=x1+(y-y1)/m;
                                                break;
                                    case 2:
                                                y=ymax;
                                                x=x1+(y-y1)/m;
                                                break;
                        }
                        a[0]=(int)x;
                        a[1]=(int)y;
                        return a;
            }
            boolean doAnd(int a[],int b[])
            {
                        for(int i=0;i<a.length;i++)
                        {
                                    int k=a[i]&b[i];
                                    if(k==1)
                                                return false;
                        }
                        return true;
            }
            public void paint(Graphics g)
            {
                setminmax();
                        g.drawRect(xmin,ymin,xmax-xmin,ymax-ymin);
                       g.drawRect(xmin+100,ymin,xmax-xmin,ymax-ymin);
                        int a[][]=new int[2][4];
                        int b[][]=new int[2][4];
                        int c[]=new int[2];
                        int c1=20;
                        int x1,y1,x2,y2;
                        System.out.println("Enter x1 of the line");
                x1=sc.nextInt();
                    System.out.println("Enter y1");
                    y1=sc.nextInt();
                    System.out.println("Enter x2");
                    x2=sc.nextInt();
                    System.out.println("Enter y2");
                y2=sc.nextInt();
           
                        float m=(y2-y1)*1.0f/(x2-x1);
                       
                        g.drawLine(x1,y1,x2,y2);
                        a[0]=set(x1,y1);
                        a[1]=set(x2,y2);
                        if(check(a[0])&&check(a[1]))
                        {
                                    g.drawLine(x1,y1,x2,y2);
                        }
                        else
                        {
                                    if( doAnd(a[0]  , b[1]) )
                                    {
                                                for(int i=a[0].length-1;i>=0;i--)
                                                {
                                                            if(a[0][i]==1)

                                                            {
                                                                        c=produceXY(a[0].length-1-i,x1,y1,m);
                                                                        b[0]=set(c[0],c[1]);
                                                         
                                                                        if(check(b[0]))
                                                                        {
                                                                                    x1=c[0];
                                                                                    y1=c[1];
                                                                                    break;
                                                                        }
                                                                        c1+=20;
                                                            }
                                                }
                                                for(int i=a[0].length-1;i>=0;i--)
                                                {
                                                            if(a[1][i]==1)
                                                            {
                                                                        c=produceXY(a[0].length-1-i,x1,y1,m);
                                                                        b[1]=set(c[0],c[1]);
                                              
                                                                        if(check(b[1]))
                                                                        {
                                                                                    x2=c[0];
                                                                                    y2=c[1];
                                                                                    break;
                                                                        }
                                                                        c1+=20;
                                                            }
                                                }
                                                g.drawLine(x1+100,y1,x2+100,y2);
                                    }
                        }
            }
}
/*<applet code="CohenS.class" width=1000 height=1000>
</applet>*/