1.Addtion
public class Add
{
public static void main (String args[])
{
int x=20;
int y=30 ;
int x3=x+y;
System.out.println("Sum is"+x3);
}
}
out put:-
C:\Program Files\Java\jdk1.6.0_03\bin\java.exe -classpath "C:\Program Files\Java\jdk1.6.0_03\jre\lib\rt.jar;C:\Program Files\Java\jdk1.6.0_03\lib\tools.jar;E:\java_programs" Add ruchir amit
Sum is50
Finished executing
2.Addion (Command line argument)
public class Add1
{
public static void main (String args[])
{
String s1= args[0];
String s2= args[1];
int x1=Integer.parseInt(s1);
int x2=Integer.parseInt(s2);
int x3;
x3=x1+x2;
System.out.println("Sum is"+x3);
}
}
output:- 5 7
C:\Program Files\Java\jdk1.6.0_03\bin\java.exe -classpath "C:\Program Files\Java\jdk1.6.0_03\jre\lib\rt.jar;C:\Program Files\Java\jdk1.6.0_03\lib\tools.jar;E:\java_programs" Add1 5 7
Sum is12
Finished executing
3.WAP to check Prime no.
public class Prime
{
public static void main(String args[])
{
String s1=args[0];
int n=Integer.parseInt(s1);
int i;
for(i=2;i<n;i++)
{
{if(n%i==0)
break;
}
}
if(i!=n)
System.out.println("not prime");
else
System.out.println("prime");
}
}
output:-
C:\Program Files\Java\jdk1.6.0_03\bin\java.exe -classpath "C:\Program Files\Java\jdk1.6.0_03\jre\lib\rt.jar;C:\Program Files\Java\jdk1.6.0_03\lib\tools.jar;E:\java_programs" Prime 4
not prime
Finished executing
4.WAP to check armstrong no.
public class Armstrong
{
public static void main(String args[])
{
int sum ,p,r;
String s1= args[0];
int y=Integer.parseInt(s1);
r=y;
sum=0;
while(y!=0)
{
p=y%10;
sum=sum+(p*p*p);
y=y/10;
}
if (sum==r)
{
System.out.println("Armstrong");
}
else
{
System.out.println(" not Armstrong");
}
}
}
output:-153
C:\Program Files\Java\jdk1.6.0_03\bin\java.exe -classpath "C:\Program Files\Java\jdk1.6.0_03\jre\lib\rt.jar;C:\Program Files\Java\jdk1.6.0_03\lib\tools.jar;E:\java_programs" Armstrong 153
Armstrong
Finished executing
5.WAP for heilstone series.
public class Heilstone
{
public static void main(String args[])
{
String s1=args[0];
int a=Integer.parseInt(s1);
while(a>1)
{
if (a%2==0)
a=a/2;
else
a=a*3+1;
System.out.print(+a+",");
}
System.out.println(a);
}
}
output:12
C:\Program Files\Java\jdk1.6.0_03\bin\java.exe -classpath "C:\Program Files\Java\jdk1.6.0_03\jre\lib\rt.jar;C:\Program Files\Java\jdk1.6.0_03\lib\tools.jar;E:\java_programs" Heilstone 12
6,3,10,5,16,8,4,2,1,1
Finished executing
6.public class Series
{
public static void main(String args[])
{
String s1=args[0];
int n=Integer.parseInt(s1);
int i,j;
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
System.out.print(j);
}
System.out.println(" ");
}
}
}
output:-5
C:\Program Files\Java\jdk1.6.0_03\bin\java.exe -classpath "C:\Program Files\Java\jdk1.6.0_03\jre\lib\rt.jar;C:\Program Files\Java\jdk1.6.0_03\lib\tools.jar;E:\java_programs" Series 5
1
12
123
1234
12345
Finished executing
7.public class Series2
{
public static void main(String args[])
{
String s1=args[0];
int n=Integer.parseInt(s1);
int i,j;
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
System.out.print(i);
}
System.out.println(" ");
}
}
}
output:5
C:\Program Files\Java\jdk1.6.0_03\bin\java.exe -classpath "C:\Program Files\Java\jdk1.6.0_03\jre\lib\rt.jar;C:\Program Files\Java\jdk1.6.0_03\lib\tools.jar;E:\java_programs" Series2 5
1
22
333
4444
55555
Finished executing
8.public class Series3
{
public static void main(String args[])
{
int i,j,k,s=30;
for(i=1;i<=5;i++)
{
for(j=1;j<=s;j++)
System.out.print(" ");
for(k=1;k<=2*i-1;k++)
{
System.out.print("*");
}
System.out.println();
s--;
}
}
}
C:\Program Files\Java\jdk1.6.0_03\bin\java.exe -classpath "C:\Program Files\Java\jdk1.6.0_03\jre\lib\rt.jar;C:\Program Files\Java\jdk1.6.0_03\lib\tools.jar;E:\java_programs" Series3 5
*
***
*****
*******
*********
Finished executing
7. WAP to print strigs according to asending order.
class stringorder
{
static String name[]={"RUCHIR", "NIKHIL", "SAUMYA", "ATUL", "AASTHA"};
public static void main(String args[ ])
{
int size=name.length;
String temp = null;
for (int i=0; i<size; i++)
{
for (int j=i+1 ;j<size;j++)
{
if (name[j].compareTo(name[i])<0)
{
temp= name[i];
name[i]=name[j];
name[j]=temp;
}
}
}
for (int i=0;i<size;i++)
{
System.out.println(name[i]);
}
output:C:\Program Files\Java\jdk1.6.0_03\bin\java.exe -classpath "C:\Program Files\Java\jdk1.6.0_03\jre\lib\rt.jar;C:\Program Files\Java\jdk1.6.0_03\lib\tools.jar;E:\java_programs" stringorder 5
AASTHA
ATUL
NIKHIL
RUCHIR
SAUMYA
Finished executing
8.WAP to print strigs according to asending order (no. of char).
public class Stringsort
{
public static void main(String args[])
{
int i,j;
String a[]={"anki","saumya","ruch","nikhi"};
for(i=0;i<a.length;i++)
{
for(j=0;j<i;j++)
{
if (a[i].length()>a[j].length())
{
String l=a[i];
a[i]=a[j];
a[j]=l;
}
}
}
for (i=0;i<a.length;i++)
{
System.out.println(a[i]);
}
}
}
output:-
C:\Program Files\Java\jdk1.6.0_03\bin\java.exe -classpath "C:\Program Files\Java\jdk1.6.0_03\jre\lib\rt.jar;C:\Program Files\Java\jdk1.6.0_03\lib\tools.jar;E:\java_programs" Stringsort 5
saumya
nikhi
ruch
anki
Finished executing
9. Default Constructor
class Student
{
int roll;
String nm;
Student()
{
roll=101;
nm="ruchir";
}
void display()
{
System.out.println("Name="+nm+"And roll="+roll);
}
}
public class Consdemo
{
public static void main(String args[])
{
Student t1=new Student();
t1.display();
}
}
output:-
C:\Program Files\Java\jdk1.6.0_03\bin\java.exe -classpath "C:\Program Files\Java\jdk1.6.0_03\jre\lib\rt.jar;C:\Program Files\Java\jdk1.6.0_03\lib\tools.jar;E:\java_programs" Consdemo 5
Name=ruchirAnd roll=101
Finished executing
10. Parametrise Constructor
class Student
{
int roll;
String nm;
Student(int r, String s)
{
roll=r;
nm=s;
}
void display()
{
System.out.println("Name="+nm+"And roll="+roll);
}
}
public class Conspara
{
public static void main(String args[])
{
Student t1=new Student(101,"Ruchir");
t1.display();
}
}
output:-
C:\Program Files\Java\jdk1.6.0_03\bin\java.exe -classpath "C:\Program Files\Java\jdk1.6.0_03\jre\lib\rt.jar;C:\Program Files\Java\jdk1.6.0_03\lib\tools.jar;E:\java_programs" Conspara 5
Name=RuchirAnd roll=101
Finished executing
11.Function Overloading
class area
{
final float pi=3.14f;
void calculate (int a)
{
int t=a*a;
System.out.println("Area of square="+t);
}
void calculate (int a,int b)
{
int t= a*b;
System.out.println("Area of reactangle="+t);
}
void calculate (float c)
{
float t= pi*c*c;
System.out.println("Area of circle="+t);
}
}
class function1
{
public static void main(String args[])
{
area a1=new area();
a1.calculate(7);
a1.calculate(4.2f);
a1.calculate(8,3);
}
}
output:-
C:\Program Files\Java\jdk1.6.0_03\bin\java.exe -classpath "C:\Program Files\Java\jdk1.6.0_03\jre\lib\rt.jar;C:\Program Files\Java\jdk1.6.0_03\lib\tools.jar;E:\java_programs" function1 5
Area of square=49
Area of circle=55.389595
Area of reactangle=24
Finished executing
12.Constructor overloading
class area
{
final float pi=3.14f;
void area (int a)
{
int t=a*a;
System.out.println("Area of square="+t);
}
area (int a,int b)
{
int t= a*b;
System.out.println("Area of reactangle="+t);
}
area (float c)
{
float t= pi*c*c;
System.out.println("Area of circle="+t);
}
}
class Functioncons
{
public static void main(String args[])
{
area a1 = new area(7);
area a2 = new area(7,8);
area a3 = new area(5.2f);
}
}
output:-
C:\Program Files\Java\jdk1.6.0_03\bin\java.exe -classpath "C:\Program Files\Java\jdk1.6.0_03\jre\lib\rt.jar;C:\Program Files\Java\jdk1.6.0_03\lib\tools.jar;E:\java_programs" Functioncons 5
Area of circle=153.86002
Area of reactangle=56
Area of circle=84.905594
Finished executing
13.Single inheritance
class item
{
int pr,qty;
void getitem(int a,int b)
{
pr=a;
qty=b;
}
void display()
{
System.out.println( "price="+pr+"and quality="+qty);
}
}
class total extends item
{
int total;
void calculate()
{
total=pr*qty;
System.out.print("total value="+total);
}
}
class inherit
{
public static void main(String args[])
{
total t1= new total();
t1.getitem(6,25);
t1.display();
t1.calculate();
}
}
output:
C:\Program Files\Java\jdk1.6.0_03\bin\java.exe -classpath "C:\Program Files\Java\jdk1.6.0_03\jre\lib\rt.jar;C:\Program Files\Java\jdk1.6.0_03\lib\tools.jar;E:\java_programs" inherit 5
price=6and quality=25
total value=150Finished executing
14.Interface
interface Area
{
final float pi = 3.14f;
void calculate(int a);
}
class Circle implements Area
{
public void calculate(int a)
{
float t=a*a*pi;
System.out.println("Area of circle="+t);
}
}
class Square implements Area
{
public void calculate(int a)
{
int t=a*a;
System.out.println("Area of square="+t);
}
}
public class Interfacedemo
{
public static void main(String args[])
{
Area a1;
Circle c1=new Circle ();
Square s1=new Square ();
a1=c1;
a1.calculate(6);
a1=s1;
a1.calculate(4);
}
}
output:-
C:\Program Files\Java\jdk1.6.0_03\bin\java.exe -classpath "C:\Program Files\Java\jdk1.6.0_03\jre\lib\rt.jar;C:\Program Files\Java\jdk1.6.0_03\lib\tools.jar;E:\java_programs" inherit 5
price=6 and quality=25
total value=150Finished executing
15. Multilevel Inheritance
class item
{
String na;
int pr,qty;
item (int p,int q,String s)
{
na=s;
pr=p;
qty=q;
}
void display()
{
System.out.println("name="+na + "price="+ pr + "quality="+qty);
}
}
class user extends item
{
int to_user,to_price;
int no;
user ( String a,int b,int c,int d)
{
super(c,d,a);
no=b;
}
void display()
{
super.display();
to_price=no*pr*qty;
to_user=qty;
System.out.print("total price="+to_price+ "and total user="+to_user );
}
}
class ruc
{
public static void main(String args[])
{
user a1=new user( "pen",10,105,25);
a1.display();
}
}
output:-
C:\Program Files\Java\jdk1.6.0_03\bin\java.exe -classpath "C:\Program Files\Java\jdk1.6.0_03\jre\lib\rt.jar;C:\Program Files\Java\jdk1.6.0_03\lib\tools.jar;E:\java_programs" ruc 5
name=penprice=105quality=25
total price=26250and total user=25Finished executing
16.Multilevel and Multiple Inheritance
class student
{
int roll;
String nm;
void getdata(int r,String s)
{
roll=r;
nm=s;
}
void dispdata()
{
System.out.println("name="+nm+ "roll no.="+roll);
}
}
class marks extends student
{
int m1,m2;
void getmarks(int a,int b)
{
m1=a;
m2=b;
}
void dispmarks()
{
System.out.print("sub_1="+m1+ "and sub_2="+m2 );
}
}
interface bonus
{
int b=20;
void dispbonus();
}
class result extends marks implements bonus
{
int tot;
public void dispbonus()
{
System.out.print("bonus marks="+b);
}
void dispall()
{
dispdata();
dispmarks();
dispbonus();
tot=m1+m2+b;
System.out.print("Total marks="+tot);
}
}
class ruch
{
public static void main(String args[])
{
result r1=new result();
r1.getdata(101,"ruchir");
r1.getmarks(50,60);
r1.dispall();
}
}
output:-
C:\Program Files\Java\jdk1.6.0_03\bin\java.exe -classpath "C:\Program Files\Java\jdk1.6.0_03\jre\lib\rt.jar;C:\Program Files\Java\jdk1.6.0_03\lib\tools.jar;E:\java_programs" ruch 5
name=ruchirroll no.=101
sub_1=50and sub_2=60bonus marks=20Total marks=130Finished executing
17. Multiple Inheritance
class A extends Thread
{
public void run()
{
int i;
for (i=1 ;i<=5;i++)
{
System.out.println("from thread A="+i);
}
System.out.println("exit from thread A");
}
}
class B extends Thread
{
public void run()
{
int j;
for (j=1 ;j<=5;j++)
{
System.out.println("from thread B="+j);
}
System.out.println("exit from thread B");
}
}
class c extends Thread
{
public void run()
{
int k;
for (k=1 ;k<=5;k++)
{
System.out.println("from thread C="+k);
}
System.out.println("exit from thread C");
}
}
class prioritythread
{
public static void main(String args[])
{
A t1=new A();
B t2=new B();
c t3=new c();
t1.start();
t2.start();
t3.start();
System.out.println("exiting from main");
}
}
output:-
C:\Program Files\Java\jdk1.6.0_03\bin\java.exe -classpath "C:\Program Files\Java\jdk1.6.0_03\jre\lib\rt.jar;C:\Program Files\Java\jdk1.6.0_03\lib\tools.jar;E:\java_programs" prioritythread 5
exiting from main
from thread B=1
from thread B=2
from thread B=3
from thread B=4
from thread B=5
exit from thread B
from thread A=1
from thread A=2
from thread A=3
from thread A=4
from thread A=5
exit from thread A
from thread C=1
from thread C=2
from thread C=3
from thread C=4
from thread C=5
exit from thread C
Finished executing
18. Multiple Inheritance with exceptional handleing
class A extends Thread
{
public void run()
{
int i;
for (i=1 ;i<=5;i++)
{
System.out.println("from thread A="+i);
if(i==2)
yield();
}
System.out.println("exit from thread A");
}
}
class B extends Thread
{
public void run()
{
int j;
for (j=1 ;j<=5;j++)
{
System.out.println("from thread B="+j);
}
System.out.println("exit from thread B");
}
}
class c extends Thread
{
public void run()
{
int k;
for (k=1 ;k<=5;k++)
{
System.out.println("from thread c="+k);
if (k==2)
try
{ Thread.sleep(10);}
catch (Exception e1)
{}
}
System.out.println("exit from thread C");
}
}
class thread2
{
public static void main(String args[])
{
A t1=new A();
B t2=new B();
c t3=new c();
t1.start();
t2.start();
t3.start();
System.out.println("exiting from main");
}
}
Output:-
C:\Program Files\Java\jdk1.6.0_03\bin\java.exe -classpath "C:\Program Files\Java\jdk1.6.0_03\jre\lib\rt.jar;C:\Program Files\Java\jdk1.6.0_03\lib\tools.jar;E:\java_programs" thread2 5
from thread A=1
from thread A=2
exiting from main
from thread c=1
from thread c=2
from thread A=3
from thread A=4
from thread B=1
from thread A=5
from thread B=2
exit from thread A
from thread B=3
from thread B=4
from thread B=5
exit from thread B
from thread c=3
from thread c=4
from thread c=5
exit from thread C
Finished executing
19.Thread priority
class A extends Thread
{
public void run()
{
int i;
for (i=1 ;i<=5;i++)
{
System.out.println("from thread A="+i);
}
System.out.println("exit from thread A");
}
}
class B extends Thread
{
public void run()
{
int j;
for (j=1 ;j<=5;j++)
{
System.out.println("from thread B="+j);
}
System.out.println("exit from thread B");
}
}
class c extends Thread
{
public void run()
{
int k;
for (k=1 ;k<=5;k++)
{
System.out.println("from thread C="+k);
}
System.out.println("exit from thread C");
}
}
class priority_th
{
public static void main(String args[])
{
A t1=new A();
B t2=new B();
c t3=new c();
t1.setPriority(Thread.MIN_PRIORITY);
t3.setPriority(Thread.MAX_PRIORITY);
t1.start();
t2.start();
t3.start();
System.out.println("exiting from main");
}
}
output:-
C:\Program Files\Java\jdk1.6.0_03\bin\java.exe -classpath "C:\Program Files\Java\jdk1.6.0_03\jre\lib\rt.jar;C:\Program Files\Java\jdk1.6.0_03\lib\tools.jar;E:\java_programs" priority_th 5
exiting from main
from thread C=1
from thread C=2
from thread C=3
from thread B=1
from thread C=4
from thread A=1
from thread C=5
from thread B=2
exit from thread C
from thread A=2
from thread A=3
from thread A=4
from thread B=3
from thread A=5
from thread B=4
exit from thread A
from thread B=5
exit from thread B
Finished executing
20 runnable interface:-
class A extends Thread
{
public void run()
{
int i;
for (i=1 ;i<=5;i++)
{
System.out.println("from thread A="+i);
}
System.out.println("exit from thread A");
}
}
class B extends Thread
{
public void run()
{
int j;
for (j=1 ;j<=5;j++)
{
System.out.println("from thread B="+j);
}
System.out.println("exit from thread B");
}
}
class c extends Thread
{
public void run()
{
int k;
for (k=1 ;k<=5;k++)
{
System.out.println("from thread C="+k);
}
System.out.println("exit from thread C");
}
}
class runnableinter
{
public static void main(String args[])
{
A t1=new A();
B t2=new B();
c t3=new c();
Thread a1=new Thread(t1);
Thread a2=new Thread(t2);
Thread a3=new Thread(t3);
a1.start();
a2.start();
a3.start();
System.out.println("exiting from main");
}
}
output:
C:\Program Files\Java\jdk1.6.0_03\bin\java.exe -classpath "C:\Program Files\Java\jdk1.6.0_03\jre\lib\rt.jar;C:\Program Files\Java\jdk1.6.0_03\lib\tools.jar;E:\java_programs" runnableinter 5
from thread A=1
from thread B=1
from thread B=2
from thread B=3
exiting from main
from thread B=4
from thread B=5
exit from thread B
from thread C=1
from thread A=2
from thread C=2
from thread A=3
from thread C=3
from thread A=4
from thread C=4
from thread A=5
from thread C=5
exit from thread A
exit from thread C
Finished executing
21.Synchronization:-
class One
{
synchronized void display(int num)
{
System.out.println(" " + num);
try
{
Thread.sleep(1000);
}
catch(InterruptedException e)
{
System.out.println("Interrupted");
}
System.out.println("Exiting");
}
}
class Two implements Runnable
{
int n;
One o1;
Thread t;
public Two(One a1,int t1)
{
n=t1;
o1=a1;
t=new Thread(this);
t.start();
}
public void run()
{
o1.display(n);
}
}
class Synchronize
{
public static void main(String args[])
{
One t1=new One();
int digit=10;
Two t2=new Two(t1,digit++);
Two t3=new Two(t1,digit++);
Two t4=new Two(t1,digit++);
try
{
t2.t.join();
t3.t.join();
t4.t.join();
}
catch(InterruptedException e)
{
}
}
}
output:-
C:\Program Files\Java\jdk1.6.0_03\bin\java.exe -classpath "C:\Program Files\Java\jdk1.6.0_03\jre\lib\rt.jar;C:\Program Files\Java\jdk1.6.0_03\lib\tools.jar;E:\java_programs" Synchronize 5
10
Exiting
12
Exiting
11
Exiting
Finished executing
22.Daemon (type of thread)
class Daemon implements Runnable
{
static Thread t1,t2;
public Daemon()
{
t1=new Thread(this);
//t1.start();
t2=new Thread(this);
t2.setDaemon(true);
t1.setDaemon(true);
}
public void run()
{
System.out.println("Number of threads running " +Thread.activeCount());
System.out.println(t1.isDaemon());
System.out.println(t2.isDaemon());
}
public static void main(String args[])
{
new Daemon();
System.out.println("Number of threads running " +Thread.activeCount());
System.out.println(t1.isDaemon());
System.out.println(t2.isDaemon());
}
}
output:-
C:\Program Files\Java\jdk1.6.0_03\bin\java.exe -classpath "C:\Program Files\Java\jdk1.6.0_03\jre\lib\rt.jar;C:\Program Files\Java\jdk1.6.0_03\lib\tools.jar;E:\java_programs" Daemon 5
Number of threads running 1
true
true
Finished executing
23. Static class
class abc
{
void display()
{
System.out.println("non static mathod");
}
static void hello()
{
System.out.println("static mathod");
}
}
class staticdemo
{
public static void main(String args[])
{
abc t1 = new abc();
t1.display();
abc.hello();
}
}
output:-
C:\Program Files\Java\jdk1.6.0_03\bin\java.exe -classpath "C:\Program Files\Java\jdk1.6.0_03\jre\lib\rt.jar;C:\Program Files\Java\jdk1.6.0_03\lib\tools.jar;E:\java_programs" staticdemo 5
non static mathod
static mathod
Finished executing
24.Exceptional handling
class Exceptiondemo
{
public static void main(String args[])
{
try
{
String s=args[0];
int a= Integer.parseInt(s);
int c=a*a;
System.out.println(c);
}
catch (ArrayIndexOutOfBoundsException e1)
{
System.out.println("No arument passed");
}
catch(NumberFormatException e2)
{
System.out.println("Invalid type");
}
catch (Exception e3)
{
System.out.println(e3);
}
System.out.println("Program ending");
}
}
output 1:-5
C:\Program Files\Java\jdk1.6.0_03\bin\java.exe -classpath "C:\Program Files\Java\jdk1.6.0_03\jre\lib\rt.jar;C:\Program Files\Java\jdk1.6.0_03\lib\tools.jar;E:\java_programs" Exceptiondemo 5
25
Program ending
Finished executing
output 2:-5.2
C:\Program Files\Java\jdk1.6.0_03\bin\java.exe -classpath "C:\Program Files\Java\jdk1.6.0_03\jre\lib\rt.jar;C:\Program Files\Java\jdk1.6.0_03\lib\tools.jar;E:\java_programs" Exceptiondemo 5.3
Invalid type
Program ending
Finished executing
25. Different with help of Applet :-
import java.applet.Applet;
import java.awt.*;
public class Red extends Applet
{
public void paint(Graphics g)
{
g.drawRect(50,50,150,400);
g.drawOval(100,110,50,50);
g.setColor(Color.red);
g.fillOval(100,110,50,50);
g.drawOval(100,190,50,50);
g.setColor(Color.yellow);
g.fillOval(100,190,50,50);
g.drawOval(100,270,50,50);
g.setColor(Color.green);
g.fillOval(100,270,50,50);
}
}
26. Applet (text box ,click box)
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
public class Appdemo extends Applet implements ActionListener
{ TextField t1,t2,t3;
Button b1,b2,b3,b4;
public void init()
{
Label l1=new Label ("enter a no.");
Label l2=new Label ("Another no");
Label l3=new Label("Answer");
t1=new TextField(20);
t2=new TextField(20);
t3=new TextField ("30");
b1=new Button("Add");
b2=new Button("Sub");
b3=new Button("multi");
b4=new Button("div");
add(l1);
add(t1);
add(l2);
add(t2);
add(l3);
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 e1)
{
String s1,s2,s3;
int n1,n2,n3;
s1=t1.getText();
s2=t2.getText();
n1=Integer.parseInt(s1);
n2=Integer.parseInt(s2);
if(e1.getSource()==b1)
{
n3=n1+n2;
}
else if(e1.getSource()==b2)
{
n3=n1-n2;
}
else if(e1.getSource()==b3)
{
n3=n1*n2;
}
else if(e1.getSource()==b4)
{
n3=n1/n2;
}
s3=String.valueOf(t3);
t3.setText(s3);
}
}
HTML page:-Appdemo.html
<HTML>
<HEAD>
<TITLE></TITLE>
</HEAD>
<BODY>
<Applet code="Appdemo.class" width ="600" height="800">
</Applet>
</BODY>
</HTML>
27. traffic light with help of Applet
import java.applet.Applet;
import java.awt.*;
public class Traffic extends Applet
{
public void init ()
{
setBackground(Color.white);
}
public void paint (Graphics g)
{
g.setColor(Color.black);
g.fillRect(0,0,50,90);
g.setColor(Color.red);
g.fillOval(10,20,20,20);
g.setColor(Color.yellow);
g.fillOval(10,40,20,20);
g.setColor(Color.green);
g.fillOval(10,60,20,20);
}
}
html page:- traffic.html
<HTML>
<HEAD>
<TITLE></TITLE>
</HEAD>
<BODY>
<Applet Code ="Traffic.class" width ="300">
</Applet>
</BODY>
</HTML>
2.traffic light (blinking)
import java.applet.Applet;
import java.awt.*;
public class Trafficlight extends Applet
{
public void init ()
{
setBackground(Color.white);
}
public void paint (Graphics g)
{
g.setColor(Color.black);
g.drawRect(0,0,50,90);
for( ; ;)
{
g.setColor(Color.red);
g.fillOval(10,20,20,20);
g.setColor(Color.black);
g.fillOval(10,40,20,20);
g.fillOval(10,60,20,20);
try
{ Thread.sleep(2000);}
catch(Exception e)
{}
g.fillOval(10,20,20,20);
g.setColor(Color.yellow);
g.fillOval(10,40,20,20);
g.setColor(Color.black);
g.fillOval(10,60,20,20);
try
{ Thread.sleep(2000);}
catch(Exception e)
{}
g.setColor(Color.black);
g.fillOval(10,20,20,20);
g.fillOval(10,40,20,20);
g.setColor(Color.green);
g.fillOval(10,60,20,20);
try
{ Thread.sleep(2000);}
catch(Exception e)
{}
}
}
}
html page:-traffic.html
<HTML>
<HEAD>
<TITLE></TITLE>
</HEAD>
<BODY>
<Applet Code ="Trafficlight.class" width ="300">
</Applet>
</BODY>
</HTML>
No comments:
Post a Comment