Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 82

PALINDROME CHECKING

Program to find the given string is palindrome or not.

PROGRAM:-
import java.lang.String;

class pal

public static void main(String arg[])

String st=new String();

st=arg[0];

int i=0,j=0;

int l=st.length()-1;

while(i<=1)

if(st.charAt(i)!=st.charAt(l-i))

j=1;

break;

i=i+1;

if(j==0)

System.out.println("This String is Palindrome");

Ilahia College of Arts and Science 1 M.Sc . Computer Science 2010-2012


}

else

System.out.println("This String is not Palindrome");

OUTPUT

C:\jdk1.3\bin>javac pal.java

C:\jdk1.3\bin>java pal good

This String is not Palindrome

C:\jdk1.3\bin>java pal madam

This String is Palindrome

Ilahia College of Arts and Science 2 M.Sc . Computer Science 2010-2012


ABBREVIATION OF STRING

Program to find the abbreviation of Life Insurance Corporation

PROGRAM:-
import java.lang.String.*;

class licabv

public static void main(String arg[])

String ob=new String("Life Insurance Coorporation");

int l=ob.length();

String ob1=new String();

ob1=ob1+ob.charAt(0);

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

if((ob.charAt(i)==' ')&&(ob.charAt(i+1)!=' '))

ob1=ob1+ob.charAt(i+1);

System.out.println("Abbreviation:"+ob1.toUpperCase());

Ilahia College of Arts and Science 3 M.Sc . Computer Science 2010-2012


OUTPUT

C:\jdk1.3\bin>javac licabv.java

C:\jdk1.2\bin>java licabv

Abbreviation: LIC

Ilahia College of Arts and Science 4 M.Sc . Computer Science 2010-2012


PRIME OR NOT

Program to check whether the given number is prime or not using


iostreams.

PROGRAM:-
import java.io.*;

class primeNo

public static void main(String arg[])throws IOException

try

int n,i,f=1;

InputStreamReader r=new InputStreamReader(System.in);

BufferedReader b1=new BufferedReader(r);

System.out.println("Enter the Number:");

n=Integer.parseInt(b1.readLine());

for(i=2;i<n;i++)

if(n%i==0)

f=0;

break;

if(f==1)

Ilahia College of Arts and Science 5 M.Sc . Computer Science 2010-2012


{

System.out.println("Number is Prime");

else

System.out.println("Number is not Prime");

catch(Exception e){}

OUTPUT:-
C:\jdk1.3\bin>javac lprimeNO.java

C:\jdk1.2\bin>java primeNo

Enter the Number:12

Number is not Prime

C:\jdk1.2\bin>java primeNo

Enter the Number:13

Number is Prime

Ilahia College of Arts and Science 6 M.Sc . Computer Science 2010-2012


STRING SORTING

Write a program to sort given strings .

PROGRAM:-
import java.io.*;

class sname

public static void main(String arg[ ])

int n,i;

String temp;

String str[ ]=new String[10];

try

BufferedReader br=new BufferedReader(new


InputStreamReader(System.in));

System.out.println("Enter limit");

n=Integer.parseInt(br.readLine( ));

System.out.println("Enter names");

for(i=0;i<n;i++)

str[i]=br.readLine( );

for(i=0;i<n;i++)

for(int j=i+1;j<n;j++)

if (str[i].compareTo(str[j])>0)

Ilahia College of Arts and Science 7 M.Sc . Computer Science 2010-2012


temp=str[i];

str[i]=str[j];

str[j]=temp;

System.out.println(" Sorted names are:");

for(i=0;i<n;i++)

System.out.println(str[i]);

catch(Exception e){System.out.println("Error");}

Ilahia College of Arts and Science 8 M.Sc . Computer Science 2010-2012


OUTPUT

C:\jdk1.3\bin>javac sname.java

C:\jdk1.3\bin>java sname

Enter the limit

Enter names

Revathy

Anju

Soumya

Maya

Priya

Sorted names are:

Anju

Maya

Priya

Revathy

Soumya

Ilahia College of Arts and Science 9 M.Sc . Computer Science 2010-2012


MATRIX MULTIPLICATION

Write a program to multiply two matrices using iostream.

PROGRAM

import java.io.*;

class matrix

public static void main(String args[])

int m,n,p,q,i,j,k=0;

try

int a[][]=new int[10][10];

int b[][]=new int[10][10];

int c[][]=new int[10][10];

BufferedReader br=new BufferedReader(new


InputStreamReader(System.in));

System.out.println("Enter order of Ist matrix:");

m=Integer.parseInt(br.readLine());

n=Integer.parseInt(br.readLine());

System.out.println("Enter order of IInd matrix:");

p=Integer.parseInt(br.readLine());

q=Integer.parseInt(br.readLine());

if(n!=p)

Ilahia College of Arts and Science 10 M.Sc . Computer Science 2010-2012


System.out.println("Not possible");

else

System.out.println("Enter elmts: of first matrix");

for(i=0;i<m;i++)

for(j=0;j<n;j++)

a[i][j]=Integer.parseInt(br.readLine());

System.out.println("Ist matrix: is");

for(i=0;i<m;i++)

for(j=0;j<n;j++)

System.out.print(a[i][j]+" ");

System.out.println();

System.out.println("Enter elmts of second matrix:");

for(i=0;i<p;i++)

for(j=0;j<q;j++)

b[i][j]=Integer.parseInt(br.readLine());

System.out.println("IInd matrix: is");

for(i=0;i<p;i++)

Ilahia College of Arts and Science 11 M.Sc . Computer Science 2010-2012


for(j=0;j<q;j++)

System.out.print(b[i][j]+ " ");

System.out.println();

for(i=0;i<m;i++)

for(j=0;j<q;j++)

for(k=0;k<n;k++)

c[i][j]=c[i][j]+a[i][k]*b[k][j];

System.out.println("Resultant matrix:");

for(i=0;i<m;i++)

for(j=0;j<q;j++)

System.out.print(c[i][j]+" ");

System.out.println();

catch(Exception e){}

Ilahia College of Arts and Science 12 M.Sc . Computer Science 2010-2012


OUTPUT

C:\jdk1.3\bin>javac matrix.java

C:\jdk1.3\bin>java matrix

Enter order of Ist matrix:

Enter order of IInd matrix:

Enter elements of first matrix

2 2 2 2

Ist matrix is:

2 2

2 2

Enter elements of IInd matrix

2 2 2 2

IInd matrix is:

2 2

2 2

Resultant matrix:

8 8

8 8

Ilahia College of Arts and Science 13 M.Sc . Computer Science 2010-2012


METHOD OVERLOADING

To find area of circle, rectangle, square using method overloading

PROGRAM:-
import java.io.*;

class square

int a;

void get(int x)

a=x;

void area( )

System.out.println("Area of square="+(a*a));

}}

class Rectangle extends square

int l,b;

void get(int x,int y)

l=x;

b=y;

void area( )

Ilahia College of Arts and Science 14 M.Sc . Computer Science 2010-2012


System.out.println("Area of rectangle="+(l*b));

class Circle extends Rectangle

int m;

double pi=3.14;

void get(int p)

m=p;

void area( )

System.out.println("Area of circle="+(pi*m*m));

}}

class movr

public static void main (String args[])throws IOException

try

int m,n, p,q;

DataInputStream di=new DataInputStream(System.in);

System.out.println("enter length of Rectangle");

p=Integer.parseInt(di.readLine( ));

Ilahia College of Arts and Science 15 M.Sc . Computer Science 2010-2012


System.out.println("enter Breadth of Rectangle");

q=Integer.parseInt(di.readLine( ));

System.out.println("enter radius of Circle");

m=Integer.parseInt(di.readLine( ));

System.out.println("enter side of Squre");

n=Integer.parseInt(di.readLine( ));

Rectangle ob=new Rectangle( );

ob.get(p,q);

ob.area( );

Circle ob1=new Circle( );

ob1.get(m);

ob1.area();

square ob2=new square( );

ob2.get(n);

ob2.area( );

catch(Exception e)

}}

Ilahia College of Arts and Science 16 M.Sc . Computer Science 2010-2012


OUTPUT

c:\jdk1.3\bin>javac movr.java

c:\jdk1.3\bin>java movr

Enter length of Rectangle

Enter Breadth of Rectangle

Enter radius of circle

Enter side of square

Area of rectangle=10

Area of circle=78.5

Area of square=16

Ilahia College of Arts and Science 17 M.Sc . Computer Science 2010-2012


CONSTRUCTOR OVERLOADING

Program to find the volume of a box using constructor overloading

(value l,b,h).

PROGRAM:-
class c

int l,b,h;

double v=0.0;

public c()

l=0;

b=0;

h=0;

public c(int x,int y,int z)

l=x;

b=y;

h=z;

public void show()

v=l*b*h;

System.out.println("volume of box="+v);

Ilahia College of Arts and Science 18 M.Sc . Computer Science 2010-2012


}

class conoverid

public static void main(String arg[])

c ob=new c();

c obj =new c(10,5,6);

obj.show();

OUTPUT

C:\jdk1.3\bin>javac conoverid.javac

C:\jdk1.3\bin>java conoverid

volume of box=300.0

Ilahia College of Arts and Science 19 M.Sc . Computer Science 2010-2012


METHOD OVER RIDING
import java.io.*;

class Square

Int a;

Void get(int x)

a=x;

Void Area()

System.out.println("Area of Square="+(a*a));

class Rectangle extends Square

int l,b;

void get(int x,int y)

l=x;

b=y;

Ilahia College of Arts and Science 20 M.Sc . Computer Science 2010-2012


}

void Area()

System.out.println("Area of Rectangle="+(l*b));

class Circle extends Rectangle

int m;

double PI=3.14;

void get(int p)

m=p;

void Area()

System.out.println("Area of Circle="+(PI*m*m));

class movp

Ilahia College of Arts and Science 21 M.Sc . Computer Science 2010-2012


public static void main(String args[])throws IOException

try{

int m,n,p,q;

InputStreamReader r=new InputStreamReader(System.in);

BufferedReader b=new BufferedReader(r);

System.out.println("Enter the Length of Rectangle");

p=Integer.parseInt(b.readLine());

System.out.println("Enter the Breadth of Rectangle");

q=Integer.parseInt(b.readLine());

System.out.println("Enter the Radius of Circle");

m=Integer.parseInt(b.readLine());

System.out.println("Enter the Side of Square");

n=Integer.parseInt(b.readLine());

Rectangle ob=new Rectangle();

ob.get(p,q);

ob.Area();

Circle ob1=new Circle();

ob1.get(m);

ob1.Area();

Square ob2=new Square();

ob2.get(n);

ob2.Area();

Ilahia College of Arts and Science 22 M.Sc . Computer Science 2010-2012


}catch(Exception e){}

OUTPUT

C:\jdk1.3\bin>javac movp.java

C:\jdk1.3\bin>java movp

Enter the length of the rectangle

Enter the breadth of the rectangle

Enter the radius of the circle

Enter side of square

Area of Rectangle=10

Area of Circle=78.5

Area of Square=16

Ilahia College of Arts and Science 23 M.Sc . Computer Science 2010-2012


MULTIPLE INHERITANCES
Write a program to implement multiple inheritances using interface

PROGRAM
import java.io.*;

interface sports

double sprtwt=6.0F;

void put();

class student

int rno;

void show1(int x)

rno=x;

System.out.println("rollno="+rno);

class test extends student

int m1,m2;

String st=new String("operating system");

String st1=new String("computer network");

void show2(int p,int q)

Ilahia College of Arts and Science 24 M.Sc . Computer Science 2010-2012


{

m1=p;

m2=q;

System.out.println("sub1="+st);

System.out.println("mark1="+m1);

System.out.println("sub2="+st1);

System.out.println("mark2="+m2);

class result extends test implements sports

public void put()

System.out.println("sports weightagemark="+sprtwt);

void total()

double tot=m1+m2+sprtwt;

System.out.println("total mark="+tot);

class interfacetest

public static void main(String args[])

result r=new result();

Ilahia College of Arts and Science 25 M.Sc . Computer Science 2010-2012


r.show1(101);

r.show2(98,95);

r.put();

r.total();

OUTPUT

C:\jdk1.3\bin >javac interfacetest.java

C:\jdk1.3\bin >java interfacetest

rollno=101

sub1=operating system

mark1=98

sub2=computer network

mark2=94

sports weightage mark=6.0

total mark=199.0

Ilahia College of Arts and Science 26 M.Sc . Computer Science 2010-2012


ABSTRACT CLASS

Write a program to implement abstract class .

PROGRAM:-
import java.io.*;

abstract class area

int p,q;

area(int a,int b)

p=a;

q=b;

abstract int area();

class rect extends area

rect(int x,int y)

super(x,y);

int area()

return(p*q);

Ilahia College of Arts and Science 27 M.Sc . Computer Science 2010-2012


}

class circle extends area

circle(int c1,int c2)

super(c1,c2);

int area()

return(p*q)/2;

class abstest

public static void main(String args[])

rect r=new rect(5,60);

circle c=new circle(20,10);

area a;

a=r;

int m=a.area();

System.out.println("area of rect:"+m);

a=c;

int n=a.area();

System.out.println("Area of circle:"+n);

Ilahia College of Arts and Science 28 M.Sc . Computer Science 2010-2012


}

OUTPUT

C:\jdk1.3\bin>javac abtest.java

C:\jdk1.3\bin>java abstest

Area of rectangle:300

Area of circle:100

Ilahia College of Arts and Science 29 M.Sc . Computer Science 2010-2012


IMPLEMENTATION OF PACKAGE

Program to find factorial of any number using package

PROGRAM:-
package pk2;

public class factorial

public void fact(int x)

int f=1;

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

f=f*i;

System.out.println(" factorial ="+f);

import pk2.*;

import java.io.*;

class mainfact

public static void main(String arg[])

DataInputStream di=new DataInputStream(System.in);

try{

System.out.println("enter no");

int n=Integer.parseInt(di.readLine());

Ilahia College of Arts and Science 30 M.Sc . Computer Science 2010-2012


factorial obj=new factorial();

obj.fact(n);

}catch(Exception e){}

OUTPUT

C:\jdk1.3\bin>javac mainfact.java

C:\jdk1.3\bin>java mainfact

Enter no

Factorial = 120

Ilahia College of Arts and Science 31 M.Sc . Computer Science 2010-2012


MULTI THREADING

Write a program to print multiplication table of 5,7,13 using


multithreading

PROGRAM
class a extends Thread

public void run()

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

if(i==5)

yield();

else

int k=i*5;

System.out.println(i+"*"+"5="+k);

}}}}

class b extends Thread

public void run()

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

if(i==3)

yield();

Ilahia College of Arts and Science 32 M.Sc . Computer Science 2010-2012


else

int k= i*7;

System.out.println(i+"*"+"7="+k);

}}}}

class c extends Thread

public void run()

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

if(i==1)

try

sleep(1000);

catch (Exception e)

else

int k=i*13;

System.out.println(i+"x"+"13="+k);

}}}}

class multi

Ilahia College of Arts and Science 33 M.Sc . Computer Science 2010-2012


public static void main(String args[])

new a().start();

new b().start();

new c().start();

}}

OUTPUT

C:\jdk1.3\bin >javac multi.java

C:\jdk1.3\bin> java muti

1*5=5

1*7=7

2*5=10

2*7=14

3*5=15

3*7=21

4*5=20

4*7=28

5*5=25

5*7=35

6*5=30

6*7=42

Ilahia College of Arts and Science 34 M.Sc . Computer Science 2010-2012


7*5=35

7*7=49

8*5=40

8*7=56

9*5=45

9*7=63

10*5=50

10*7=70

2*13=26

3*13=39

4*13=52

5*13=65

6*13=78

7*13=91

8*13=104

9*13=117

10*13=130

Ilahia College of Arts and Science 35 M.Sc . Computer Science 2010-2012


HUMAN FACE

Draw Human face using Applet

PROGRAM:-
import java.applet.*;

import java.awt.*;

public class san extends Applet

public void paint(Graphics g)

g.drawArc(120,120,20,15,-180,-180);

g.drawArc(155,120,20,15,-180,-180);

g.drawOval(93,100,120,100);

g.fillOval(125,130,15,10);

g.fillOval(160,130,15,10);

g.drawOval(143,140,10,20);

g.drawOval(134,168,30,10);

g.drawArc(100,100,120,115,-180,-180);

g.drawArc(90,100,110,118,-180,-180);

g.drawArc(85,100,115,115,-180,-180);

g.drawArc(140,182,20,10,180,180);

g.drawArc(137,165,25,10,180,180);

g.drawOval(85,140,10,20);

g.drawOval(215,140,10,20);

}}

Ilahia College of Arts and Science 36 M.Sc . Computer Science 2010-2012


//<applet code="san.class" width=500 height=500>

//</applet>

OUTPUT

C:\jdk1.3\bin >javac san.java

C:\jdk1.3\bin>appletviewer san.java

Ilahia College of Arts and Science 37 M.Sc . Computer Science 2010-2012


IMAGE LOADING

Program to load an image in an applet

PROGRAM:-
import java.awt.*;

import java.applet.*;

public class appimg extends Applet

public void paint(Graphics g)

Image img;

img=getImage(getDocumentBase(),"59l.jpg");

g.drawImage(img,0,0,this);

//<applet code="appimg.class" width=1000 height=700>

//</applet>

Ilahia College of Arts and Science 38 M.Sc . Computer Science 2010-2012


OUT PUT

Ilahia College of Arts and Science 39 M.Sc . Computer Science 2010-2012


AWT CONTROLS

Program to implement awt controls.

PROGRAM
import java.awt.*;

import java.applet.*;

public class ctrls extends Applet

Button b1;

Label l1;

Checkbox c1;

Choice d1;

List l;

TextField t;

TextArea t1;

Scrollbar s;

public void init()

b1=new Button("OK");

b1.setBounds(100,100,50,30);

l1=new Label("click here");

c1=new Checkbox("welcome");

d1=new Choice();

d1.add("india");

d1.add("pack");

Ilahia College of Arts and Science 40 M.Sc . Computer Science 2010-2012


l=new List();

l.add("Anu");

t=new TextField("hai");

t1=new TextArea("achu",1,20);

s=new Scrollbar();

setBackground(Color.green);

add(b1);

add(l1);

add(c1);

add(d1);

add(l);

add(t);

add(t1);

add(s);

}}

//<applet code = "ctrls.class" width=300 height=200>

//</applet>

Ilahia College of Arts and Science 41 M.Sc . Computer Science 2010-2012


OUTPUT
C:\jdk1.3\bin>javac ctrl.java

C:\jdk1.3\bin>appletviewer ctrl.java

Ilahia College of Arts and Science 42 M.Sc . Computer Science 2010-2012


CALCULATOR
Program to display a Calculator.

PROGRAM:-
import java.awt.*;

import java.applet.*;

import java.awt.event.*;

public class calculator extends Applet implements ActionListener

TextField t1,t2,t3;

Button b1,b2,b3,b4;

public void init()

setLayout(null);

t1=new TextField();

t2=new TextField();

t3=new TextField();

b1=new Button("add");

b2=new Button("sub");

b3=new Button("mul");

b4=new Button("div");

t1.setBounds(100,100,50,50);

t2.setBounds(200,100,50,50);

t3.setBounds(100,260,50,50);

b1.setBounds(200,180,50,50);

Ilahia College of Arts and Science 43 M.Sc . Computer Science 2010-2012


b2.setBounds(300,190,50,50);

b3.setBounds(350,240,50,50);

b4.setBounds(400,180,150,50);

add(t1);

add(t2);

add(t3);

add(b1);

add(b2);

add(b3);

add(b4);

b1.addActionListener(this);

b2.addActionListener(this);

b3.addActionListener(this);

b4.addActionListener(this);

public void actionPerformed(ActionEvent e)

int c=0;

int x=Integer.parseInt(t1.getText());

int y=Integer.parseInt(t2.getText());

if(e.getSource()==b1)

c=x+y;

else if(e.getSource()==b2)

Ilahia College of Arts and Science 44 M.Sc . Computer Science 2010-2012


c=x-y;

else if(e.getSource()==b3)

c=x*y;

else if(e.getSource()==b4)

c=x/y;

}t3.setText(" "+c);

//<applet code="calculator.class"width=500 height=500>

//</applet>

Ilahia College of Arts and Science 45 M.Sc . Computer Science 2010-2012


OUTPUT

C:\jdk1.3\bin>javac calculator.java

C:\jdk1.3\bin>appletviewer calculator.java

Ilahia College of Arts and Science 46 M.Sc . Computer Science 2010-2012


FLAG

Program to draw flag using Applet

PROGRAM:-
import java .awt.*;

import java.applet.*;

public class flag extends Applet

public void paint(Graphics g)

g.setColor(Color.orange);

g.fillRect(100,100,300,75);

g.setColor(Color.black);

g.fillRect(90,100,10,470);

g.drawRect(100,175,300,75);

g.setColor(Color.blue);

g.fillOval(220,185,50,50);

g.setColor(Color.green);

g.fillRect(100,250,300,75);

//<applet code=flag.class width=500 height=500>

//</applet>

Ilahia College of Arts and Science 47 M.Sc . Computer Science 2010-2012


OUTPUT

C:\jdk1.3\bin>Javac flag.java

C:\jdk1.3\bin>appletviewer flag.java

Ilahia College of Arts and Science 48 M.Sc . Computer Science 2010-2012


STRING INSIDE A RECTANGLE

Program to display a string inside a rectangle.

PROGRAM:-
Import java.awt.*;

import java.applet.*;

public class rect extends Applet

String ob;

public void init()

ob=new String("HELLO");

public void paint(Graphics g)

g.drawRect(50,50,280,230);

g.drawRect(110,110,320,260);

g.drawLine(50,50,110,110);

g.drawLine(50,280,110,370);

g.drawLine(330,50,430,110);

g.drawLine(330,280,430,370);

g.drawString(ob,200,200);

}//<applet code="rect.class" height=500 width=500>

//</applet>

Ilahia College of Arts and Science 49 M.Sc . Computer Science 2010-2012


OUTPUT

C:\jdk1.3\bin>javac rect.java

C:\jdk1.3\bin>appletviewer rect.java

Ilahia College of Arts and Science 50 M.Sc . Computer Science 2010-2012


MOVE OVAL

Program to move an oval from left to right.

PROGRAM:-
import java.awt.*;

import java.lang.*;

import java.applet.*;

public class appthr extends Applet implements Runnable

Thread t;

int x;

public void init()

t=new Thread(this);

x=10;

t.start();

public void run()

try

while(x<=1000)

x=x+5;

t.sleep(50);

Ilahia College of Arts and Science 51 M.Sc . Computer Science 2010-2012


repaint();

while(x>0)

x=x-5;

t.sleep(50);

repaint();

}catch(Exception e){}

public void paint(Graphics g)

g.fillOval(x,200,50,50);

//<applet code="appthr.class" width=100 height=150>

//</applet>

Ilahia College of Arts and Science 52 M.Sc . Computer Science 2010-2012


OUTPUT

C:\jdk1.3\bin>javac appthr.java

C:\jdk1.3\bin>appletviewer appthr.java

Ilahia College of Arts and Science 53 M.Sc . Computer Science 2010-2012


ACTION EVENT

Program to display message inside a textfield when button is pressed.

PROGRAM:-
import java.applet.*;

import java.awt.*;

import java.awt.event.*;

public class cli extends Applet implements ActionListener

TextField t;

Button b;

public void init()

setLayout(null);

t=new TextField();

b=new Button();

t.setBounds(100,100,100,30);

b.setBounds(230,100,50,30);

add(t);

add(b);

b.addActionListener(this);

public void actionPerformed(ActionEvent e)

t.setText("welcome");

Ilahia College of Arts and Science 54 M.Sc . Computer Science 2010-2012


}

//<applet code="cli.class" width=500 height=500>

//</applet>

OUTPUT

C:\jdk1.3\bin >Javac cli.java

C:\jdk1.3\bin >Appletviewer cli.java

Ilahia College of Arts and Science 55 M.Sc . Computer Science 2010-2012


MOUSE EVENT

Program to implement mouse event by placing text field which display


the corresponding action of a mouse with a button.

PROGRAM:-
import java.awt.*;

import java.awt.event.*;

import java.applet.*;

/*<applet code="mouseevent" width=300 height=100>

</applet>*/

public class mouseevent extends Applet implements MouseListener

String msg=" ";

int mouseex=0,mouseey=0;

public void init()

addMouseListener(this);

public void mouseEntered(MouseEvent me)

mousex=0;

mousey=10;

msg="mouse entered";

setBackground(Color.pink);

repaint();

Ilahia College of Arts and Science 56 M.Sc . Computer Science 2010-2012


public void mousePressed(MouseEvent me)

mousex=me.getX();

mousey=me.getY();

msg="down";

setBackground(Color.orange);

repaint();

public void mouseReleased(MouseEvent me)

mousex=me.getX();

mousey=me.getY();

msg="up";

repaint();

public void paint(Graphics g)

g.drawString(msg,mousex,mousey);

Ilahia College of Arts and Science 57 M.Sc . Computer Science 2010-2012


OUTPUT

Ilahia College of Arts and Science 58 M.Sc . Computer Science 2010-2012


KEY EVENT
Program to display which key is pressed in a text field using key event

PROGRAM:-
import java.applet .*;

import java.awt.*;

import java.awt.event.*;

public class show1 extends Applet implements KeyListener

TextField t1,t2;

public void init()

t1=new TextField();

t2=new TextField();

t1.setBounds(100,100,50,30);

t2.setBounds(100,200,50,30);

add(t1);

add(t2);

t1.addKeyListener(this);

public void KeyPressed(KeyEvent e)

t2.setText(" "+e.getKeyChar());

public void KeyTyped(KeyEvent e)

Ilahia College of Arts and Science 59 M.Sc . Computer Science 2010-2012


{}

public void keyReleased(KeyEvent e)

{}

public static void main(String arg[])

Show1 ob=new show1();

}}

//<applet code="show1.class" width=300 height=200>

//</applet>

OUTPUT
C:\jdk1.3\bin>Javac show1.java

C:\jdk1.3\bin>Appletviewer show1.java

Ilahia College of Arts and Science 60 M.Sc . Computer Science 2010-2012


CHOICE LIST

Program to display selected text from choice list to a text field.

PROGRAM:-
import java.awt.*;

import java.applet.*;

import java.awt.event.*;

public class as2 extends Applet implements ItemListener

TextField t=new TextField();

Choice ch=new Choice();

public void init()

setLayout(null);

ch.add("india");

ch.add("usa");

ch.add("china");

t.setBounds(70,70,100,25);

ch.setBounds(70,140,100,40);

ch.addItemListener(this);

add(t);

add(ch);

Public void itemStateChanged(ItemEvent e)

Ilahia College of Arts and Science 61 M.Sc . Computer Science 2010-2012


t.setText(" " +e.getItem());

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

//</applet>

OUTPUT

C:\jdk1.3\bin>javac as2.java

C:\jdk1.3\bin>appletviewer as2.java

Ilahia College of Arts and Science 62 M.Sc . Computer Science 2010-2012


ADJUSTMENT EVENT

Program to display value of a scroll bar in to a text field using


adjustment event .

PROGRAM
import java.awt.*;

import java.applet.*;

import java.awt.event.*;

public class value extends Applet implements AdjustmentListener

TextField t;

Scrollbar s;

public void init( )

setLayout(null);

t=new TextField( );

s=new Scrollbar(Scrollbar.HORIZONTAL,0,125,0,1000);

t.setBounds(5,100,50,20);

s.setBounds(0,50,280,20);

add(t);

add(s);

s.addAdjustmentListener(this);

public void adjustmentValueChanged(AdjustmentEvent e)

t.setText(" "+e.getValue( ));

Ilahia College of Arts and Science 63 M.Sc . Computer Science 2010-2012


}

//<applet code="value.class" width=300 height=200>

//</applet>

OUTPUT

C:\jdk1.3\bin>javac value.java

C:\jdk1.3\bin>appletviewer value.java

Ilahia College of Arts and Science 64 M.Sc . Computer Science 2010-2012


MOVE LABEL

Program to move a label from left to right along with the scroll bar
movement.

PROGRAM:-
import java.awt.event.*;

public class bb extends Applet implements AdjustmentListener

Label l;

Scrollbar s;

public void init( )

setLayout(null);

l=new Label("HAI");

s=new Scrollbar(Scrollbar.HORIZONTAL,1,24,1,300);

s.setBounds(0,100,298,20);

l.setBounds(0,30,280,50);

add(s);

add( l );

s.addAdjustmentListener(this);

public void adjustmentValueChanged(AdjustmentEvent e)

l.setBounds(e.getValue(),50,280,50);

Ilahia College of Arts and Science 65 M.Sc . Computer Science 2010-2012


//<applet code="bb.class" width=300 height=200>

//</applet>

OUTPUT

c:\jdk1.3\bin>javac bb.java

c:\jdk1.3\bin>appletviewer bb.java

Ilahia College of Arts and Science 66 M.Sc . Computer Science 2010-2012


COLOR PATTERN

Program to set background color of an applet using 3 scrollbar


corresponding to red, green, blue color

PROGRAM:-
import java.applet.*;

import java.awt.*;

import java.awt.event.*;

public class color extends Applet implements AdjustmentListener

Scrollbar s1;

Scrollbar s2;

Scrollbar s3;

public void init( )

setLayout(null);

s1=new Scrollbar( Scrollbar.HORIZONTAL, 0,25,0,1000);

s2=new Scrollbar( Scrollbar.HORIZONTAL,0,25,0,1000);

s3=new Scrollbar( Scrollbar.HORIZONTAL,0,25,0,1000);

s1.setBounds(0,300,1000,50);

s2.setBounds(0,200,1000,50);

s3.setBounds(0,100,1000,50);

add(s1);

add(s2);

add(s3);

s1.addAdjustmentListener(this);

Ilahia College of Arts and Science 67 M.Sc . Computer Science 2010-2012


s2.addAdjustmentListener(this);

s3.addAdjustmentListener(this);

public void adjustmentValueChanged(AdjustmentEvent e)

Color ob=new Color(s1.getValue( ),s2.getValue( ),s3.getValue( ));

setBackground(ob);

//<applet code="color.class" width=500 height=500>

//</applet>

Ilahia College of Arts and Science 68 M.Sc . Computer Science 2010-2012


OUTPUT

C:\jdk1.3\bin> Javac color. java

C:\jdk1.3\bin> Appletviewer color.java

Ilahia College of Arts and Science 69 M.Sc . Computer Science 2010-2012


ITEM EVENT

Program to set background color of an applet when color is selected in


the choice list.

PROGRAM:-
import java.applet.*;

import java.awt.*;

import java.awt.event.*;

public class as3 extends Applet implements ItemListener

Choice ch=new Choice( );

public void init( )

setLayout(null);

ch.add(" ");

ch.add("blue");

ch.add("green");

ch.add("red");

ch.setBounds(100,100,100,540);

ch.addItemListener(this);

add(ch);

public void itemStateChanged(ItemEvent e)

if(e.getItem( )=="blue")

Ilahia College of Arts and Science 70 M.Sc . Computer Science 2010-2012


setBackground(Color.blue);

else if(e.getItem( )=="green")

setBackground(Color.green);

else if(e.getItem( )=="red")

setBackground(Color.red);

//<applet code="as3.class" width=600 height=600>

//</applet>

Ilahia College of Arts and Science 71 M.Sc . Computer Science 2010-2012


OUTPUT

C:\jdk1.3\bin>javac as3.java

C:\jdk1.3\bin>appletviewer as3.java

Ilahia College of Arts and Science 72 M.Sc . Computer Science 2010-2012


KEY PRESSED

Program to find the sum of two no:s entered in to two text fields
&display the result in the third text field when enter key is pressed.

PROGRAM
import java.awt.*;

import java.applet.*;

import java.awt.event.*;

public class key extends Applet implements KeyListener

TextField t1,t2,t3;

public void init()

setLayout(null);

t1=new TextField();

t2=new TextField();

t3=new TextField();

t1.setBounds(50,50,50,30);

t2.setBounds(50,100,50,30);

t3.setBounds(50,150,50,30);

add(t1);

add(t2);

add(t3);

t2.addKeyListener(this);

public void keyPressed(KeyEvent e)

Ilahia College of Arts and Science 73 M.Sc . Computer Science 2010-2012


{

int a,b,c;

if(e.getKeyChar()==KeyEvent.VK_ENTER)

a=Integer.parseInt(t1.getText());

b=Integer.parseInt(t2.getText());

c=a+b;

t3.setText(""+c);

public void keyReleased(KeyEvent e)

public void keyTyped(KeyEvent e)

//<applet code="key.class" width=300 height=200>

//</applet>

Ilahia College of Arts and Science 74 M.Sc . Computer Science 2010-2012


OUTPUT

Ilahia College of Arts and Science 75 M.Sc . Computer Science 2010-2012


SUNRISE

import java.applet.*;

import java.awt.*;

public class sunri extends Applet implements Runnable

Thread t;

int x;

public void init()

t=new Thread(this);

x=10;

t.start();

public void run()

try

while(true)

x=x+5;

t.sleep(2000);

Ilahia College of Arts and Science 76 M.Sc . Computer Science 2010-2012


repaint();

catch(Exception e){}

public void paint(Graphics g)

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

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

g.drawLine(340,300,600,100);

g.drawLine(600,100,800,300);

g.drawLine(200,300,150,250);

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

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

g.drawLine(115,300,115,350);

g.drawLine(180,300,180,350);

g.drawLine(115,350,180,350);

g.drawLine(180,350,215,325);

g.drawLine(215,325,215,290);

g.drawLine(230,280,200,300);

Ilahia College of Arts and Science 77 M.Sc . Computer Science 2010-2012


g.drawLine(230,280,170,242);

g.drawLine(170,242,150,250);

g.setColor(Color.red);

g.fillOval(360,0+x,60,60);

//<applet code="sunri.class" width=800 height=400>

//</applet>

Output

Ilahia College of Arts and Science 78 M.Sc . Computer Science 2010-2012


Digital Clock

import java.awt.*;

import java.applet.*;

public class clock extends Applet implements Runnable

Thread t;

int h,m,s;

public void init()

int h=0,m=0,s=0;

t=new Thread(this);

t.start();

Label ob=new Label("Digital Clock");

add(ob);

public void run()

try

while(true)

Ilahia College of Arts and Science 79 M.Sc . Computer Science 2010-2012


{

s=s+1;

t.sleep(50);

if(s>60)

s=1;

m=m+1;

if(m>=60)

m=1;

h=h+1;

if(h>12)

h=1;

repaint();

Ilahia College of Arts and Science 80 M.Sc . Computer Science 2010-2012


catch(Exception e){}

public void paint(Graphics g)

String str=Integer.toString(h);

String str1=Integer.toString(m);

String str2=Integer.toString(s);

g.drawString(str,260,40);

g.drawString(str,280,40);

g.drawString(str,300,40);

// <applet code="clock.class" height=600 width=600>

//</applet>

//javac clock.java

//appletviewer clock.java

Ilahia College of Arts and Science 81 M.Sc . Computer Science 2010-2012


Ilahia College of Arts and Science 82 M.Sc . Computer Science 2010-2012

You might also like