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

HTML Program to illustrate basic tag of html

<HTML> <HEAD> <TITLE> First Program in HTML </TITLE> <BODY> WELCOME TO HTML </BODY> </HEAD> </HTML> OUTPUT:

Program to use attributes of Body tag


<html> <head> <title> first program </title> <body bgcolor=blue text=red> WELCOME </body> </head> </html> OUTPUT:

Program to show use of font,center tag and text syles


<html> <head> <body bgcolor=pink text=blue> <b> HTML </b> <br><br> <I> HTML </I> <br><br> <U> HTML </U> <br><br> <CENTER> HTML </CENTER> <br><br> <FONT COLOR=PINK> HTML </FONT> <br><br> </body> </head> </html> OUTPUT:

Program to show use of marquee and HR tag

<html> <head> <body bgcolor=yellow text=red> <center> LINE </center> <HR ALIGN="center" WIDTH="20" SIZE="100" color=black> </HR> </head> </body> </html> OUTPUT:

Program to show use of Sub and Sup tag


<html> <head> <body bgcolor=pink text=blue> <center>H<Sub>2</Sub>O </center> <br><br> <center>x<sup>2</sup>y<sup>3</sup></c enter> </head> </body> </html> OUTPUT:

Program to show input/output devices using lists


<html> <head> <body> <center><b> INPUT DEVICES </b> <OL type=1> <LI> Keyboard <LI> Mouse <LI> Touch pad </OL> <b> OUTPUT DEVICES </b> <OL type=1> <LI> Monitor <LI> Printer <LI> Scanner </OL> </center> </body> </head> </html> OUTPUT:

Program to show image with and without border

<html> <head> <body> <img src="C:\Users\Public\Pictures\Sample Pictures\Tulips.jpg" border=5> <img src="C:\Users\Public\Pictures\Sample Pictures\Tulips.jpg"> </head> </body> </html> OUTPUT:

Program to show use of table tag


<html> <head> <body> <TABLE border=5> <TH> <TD>Roll no <TD>Name <TD>Class </TH> <TR> <TD>1 <TD>Aman <TD>BCA3 </TR> <TR> <TD>2 <TD>Dimple <TD>BCA3 </TR> </TABLE> </head> </body> </html> OUTPUT:

Program to show frameset tag

<html> <body> <FRAMESET Rows="30%,*"> <FRAMESET Cols="50%,50%"> <FRAME Src="C:\flowers\Chrysanthemum.jpg"> <FRAME Src="C:\flowers\Hydrangeas.jpg"> </FRAMESET> <FRAMESET Cols="50%,50%"> <FRAME Src="C:\flowers\Chrysanthemum.jpg"> <FRAME Src="C:\flowers\Hydrangeas.jpg"> </FRAMESET> </FRAMESET> </body> </html>

10

Program to show Addmission Form


<html> <head> <body> <form name="f1"> <b> Admission Form </b> <br><br> <b> Name </b> <input type="text" name="t1" size=5> <br><br> <b> Class </b> <input type="text" name="t2" size=5> <br><br> <b> Rollno </b> <input type="text" name="t3" size=5> <br><br> <b> percentage </b> <input type="text" name="t4" size=5> <br><br> <input type="button" name="b1" value=SUBMIT> <br><br> <input type="button" name="b2" value=RESET> <br><br> </form> </head> </body> </html> OUTPUT:

11

12

Program to show resume

<html> <head> <body> <form name="f1"> <b> Resume </b> <br><br> <b> Name </b> <input type="text" name="t1" size=5> <br><br> <b> Class </b> <input type="text" name="t2" size=5> <br><br> <b> Rollno </b> <input type="text" name="t3" size=5> <br><br> <b> Percentage in 10th </b> <input type="text" name="t4" size=5> <br><br> <b> Percentage in 10+2th </b> <input type="text" name="t5" size=5> <br><br> <b> Percentage in BCA I </b> <input type="text" name="t6" size=5> <br><br> <b> Percentage in BCA II</b> <input type="text" name="t7" size=5> <br><br> <b> Father's Name </b< <input type="text" name="t8" size=5> <br><br>
13

<b> Address</b> <input type="text" name="t9" size=5> <br><br> <b> Contact No. </b> <input type="text" name="t10" size=5> <br><br> <b> Hobbies</b> <input type="text" name="t11" size=5> <br><br> <input type="button" name="b1" value=SUBMIT> <br><br> <input type="button" name="b2" value=RESET> <br><br> </form> </head> </body> </html> OUTPUT:

14

Program to show use of image map tag


<html> <body> <map name="m1"> <area shape="circle" coords="661,432,5" href="html1.html"> </map>
15

<img src="C:\Users\Public\Pictures\Sample Pictures\koala.jpg" usemap="m1" </body> </html> OUTPUT:

Program to show special tags


<html> <head> <body> <s> HELLO </s> <br><br> <strong> HELLO </strong> <br><br> <pre> HELLO & WELCOME </pre>
16

</head> </body> </html> OUTPUT:

DHTML Program to show use of external stylesheet


<html> <body> p1{color:pink} h1{color:red} </body> </html>

17

<html> <head> <link rel=stylesheet href="C:\Users\DELL\Desktop\hgrewal\html\dht ml1.css"> </head> <body> <p1>Welcome <h1>Hello </body> </html> OUTPUT:

Program to show use of external stylesheet by using class


<html> <body> p{color:pink} .abc{Text-decoration:Underline} </body> </html> <html>
18

<head> <link rel=stylesheet href="C:\Users\DELL\Desktop\hgrewal\html\dht ml2.css"> </head> <body> <p>Welcome <p class="abc">Hello </body> </html> OUTPUT:

Program to show use of Internal Stylesheet


<html> <head> <style type=text/css> p{color:red} h1{color:pink;font-style:italic} </style> </head> <body> hello
19

<p> Welcome <h1> First page </h1> End </body> </html> OUTPUT:

Program to show use of class


<head> <style type=text/css> p{color:red} .abc{color:pink;font-style:italic} .xyz{color:blue} </style> </head> <body> <p> hello <p class=abc> Welcome
20

<p class=xyz> HTML </h1> </body> </html> OUTPUT:

Program to show use of inline stylesheet


<html> <body> <h1 style=color:red;font-style:italic> Welcome </h1> <h1> Hello </h1> </body> </html> OUTPUT:

21

Program to show use of Span tag


<html> <head> <style type=text/css> p{color:red} .abc{color:pink;font-style:italic} .xyz{color:blue} </style> </head> <body> <p> hello
22

<p class=abc> Welcome to <SPAN class=xyz> HTML </SPAN> programming <p class=xyz> HTML <SPAN class=abc> Program </SPAN> </h1> </body> </html> OUTPUT:

JAVA Program to display text Welcome in the java world


class java1 { public static void main(String args[]) { System.out.println("Welcome in the Java World"); } }
23

OUTPUT:

Program to find sum of 2 numbers


class Add1 { public static void main(String args[]) { int a=10,b=20; int c; c=a+b; System.out.println("Sum of a & b" + c); } } OUTPUT:
24

25

Program to calculate Subtraction of 2 numbres


class Sub { public static void main(String args[]) { int a=20,b=10; int c; c=a-b; System.out.println("Sub of a & b" + c); } } OUTPUT:

26

Program to calculate multiplication of 2 numbers


class Mul { public static void main(String args[]) { int a=20,b=10; int c; c=a*b; System.out.println("Mul of a & b = " + c); } } OUTPUT:

27

Program to calculate division of 2 numbers

class Div { public static void main(String args[]) { int a=20,b=10; int c; c=a/b; System.out.println("Div of a & b = " + c); } } OUTPUT:

28

Program to calculate sum of 2 numbers using readline function

import java .io.DataInputStream; class Reading { public static void main(String args[]) { DataInputStream in=new DataInputStream(System.in); int intnumber=0; float floatnumber=0.0f; try { System.out.println("enter an integer "); intnumber=Integer.parseInt(in.readLine()); System.out.println("enter a float number : "); floatnumber=Float.valueOf(in.readLine()).floa tValue(); } catch (Exception e) { } System.out.println("intnumber=" + intnumber); System.out.println("floatnumber=" + floatnumber); } } OUTPUT:
29

30

Program to perform arithmetic operations using command line arguments

class Sumcmd { public static void main(String args[]) { int x,y,s; x=Integer.parseInt(args[0]); y=Integer.parseInt(args[1]); s=x+y; System.out.println("sum of x and y is" + s); s=x-y; System.out.println("sub of x and y is" + s); s=x*y; System.out.println("mul of x and y is" + s); s=x/y; System.out.println("div of x and y is" + s); } } OUTPUT:

31

Program to find average of two numbers

class Avg { public static void main(String args[]) { int a=10,b=20; int sum,avg; sum=a+b; System.out.println("Sum = " + sum); avg=sum/2; System.out.println("Average = " + avg); } } OUTPUT:

32

Program to implement relational operator

class Reloperator { public static void main(String args[]) { int a=10,b=20,c=10; System.out.println("a<b = " + (a<b)); System.out.println("a>b = " + (a>b)); System.out.println("a==b = " + (a==b)); System.out.println("a<=b = " + (a<=b)); System.out.println("a>=b = " + (a>=b)); System.out.println("a!=b = " + (a!=b)); } } OUTPUT:

33

Program to perform assignment operator


class Assignoperator { public static void main(String args[]) { int a=10,b=20; int c=2; c+=a+1; System.out.println("c= " + c); } } OUTPUT:

34

Program to implement increment operator


class Incre { public static void main(String args[]) { int m=10,n=20; System.out.println("m= " + m); System.out.println("n= " + n); System.out.println("m= " + ++m); System.out.println("n= " + ++n); System.out.println("m= " + m++); System.out.println("n= " + n++); System.out.println("m= " + m); System.out.println("n= " + n); } } OUTPUT:

35

Program to find greatest among 3 numbers using conditional operator

class Largest3 { public static void main(String args[]) { int largest , a, b, c ; a=10; b=20; c=30; largest = a > b ? (a > c ? a : (b > c ? b : c)) : (b > c ? b : (a > c ? a : c)); System.out.println("Largest of 10,20,30 is " +largest); } } OUTPUT:

36

Program to display use of casting operator


class Typecast1 { public static void main(String args[]) { System.out.println("variables are"); char c='a'; byte b= 50; short s=1993; int i=123; long l=123L; float f1=3.14F; float f2=1.2e-5F; double d2=0.0000123; System.out.println("c = " + c); System.out.println("b = " + b); System.out.println("s = " + s); System.out.println("i = " + i); System.out.println("l = " + l); System.out.println("f1 = " + f1); System.out.println("f2 = " + f2); System.out.println("d2 = " + d2); System.out.println(" "); System.out.println("Type converted "); short s1=(short)b; short s2=(short)i; float n1=(float)l; int m1=(int)f1;
37

System.out.println("(short)b= " + s1); System.out.println("(short)i= " + s2); System.out.println("(float)l= " + n1); System.out.println("(int)f1= " + m1); } } OUTPUT:

38

Program to illustrate math function supported by java

class Math1 { public static void main(String args[]) { double a; a=Math.max(22,33); System.out.println("The maximum among 22 and 33 is" + a); a=Math.sqrt(16); System.out.println("The square root of 16 is" + a); a=Math.abs(-20); System.out.println("The absolute value of 20 is" + a); a=Math.pow(16,4); System.out.println("The power of 16 and 4 is" + a); a=Math.min(22,33); System.out.println("The minimum among 22 and 33 is" + a); a=Math.log(2); System.out.println("The lod value of 2 is" + a); } }

39

OUTPUT:

40

Program to calculate square and cube of a number

class Sqcube { public static void main(String args[]) { int num=2 , square , cube ; System.out.println("\nThe squares and cubes of a number is :\n"); System.out.println("================ ======================\n\n"); System.out.println("Number\tSquare\tCube\n"); square = num * num ; cube = num * square ; System.out.println(num+"\t"+square+"\t"+cub e); } } OUTPUT:

41

Program to display first 10 even numbers

class Even1 { public static void main(String args[]) { int i,c=0; System.out.println("First 10 even numbers are:"); for(i=1;i<=20;i++) { if(i%2==0) { c=c+1; System.out.print("\t" + i); } if(c==10) break; } } } OUTPUT:

42

Program to display first 10 odd numbers

class Odd1 { public static void main(String args[]) { int i,c=0; System.out.println("First 10 odd numbers are:"); for(i=1;i<=20;i++) { if(i%2!=0) { c=c+1; System.out.print("\t" + i); } if(c==10) break; } } } OUTPUT:

43

Program to use Nested-if-else statement

class Nestedifelse { public static void main(String args[]) { int a=10,b=20,c=10; if(a>b) { if(a>c){ System.out.println("a is the greatest "+a); } else { System.out.println("c is the greatest " + c); } } else{ if(c>b) { System.out.println("c is the greatest" + c); } else { System.out.println("b is the greatest " + b); }
44

} } }

45

OUTPUT:

46

Program to show use of else-if-ladder


class Elseifladder { public static void main(String args[]) { int marks=75; if(marks>79) System.out.println("grade= Honours"); else if(marks>59) System.out.println("grade= I division"); else if(marks>49) System.out.println("grae= II division"); else if(marks>39) System.out.println("grade= III division"); else System.out.println("Fail"); } } OUTPUT:

Program to find factorial of a numbers using for loop


47

class Fact { public static void main(String args[]) { int num = 5; int fact=1; for(int i=num;i>=1;i--) { fact=fact*i; } System.out.println("Factorial of Given no. is : "+ fact); } } OUTPUT:

Program to print 10 to 1 using do while


class Print10to1 {

48

public static void main(String args[]) { int i=11; do { i--; System.out.println(i); }while(i>=2); } } OUTPUT:

Program to print triangle


class tri1 { public static void main(String args[])
49

{ int i,j,n=4; for(i=0;i<n;i++) { for(j=0;j<=i;j++) { System.out.print("*"); } System.out.println("\n"); } } } OUTPUT:

Program to print triangle

class tri2 { public static void main(String args[])


50

{ int i,j,n=4; for(i=1;i<=n;i++) { for(j=n;j>=i;j--) { System.out.print("*"); } System.out.println("\n"); } } } OUTPUT:

Program to display table upto entered number


class table { public static void main(String args[]) {
51

int n=10,k,i=1,j=1; System.out.println("table upto 10 is"); do { do { k=i*j; System.out.print(" " + k); j++; }while(j<=n); j=1; i++; System.out.print("\n"); }while(i<=n); } } OUTPUT:

Program to check number is Armstrong or not

class ArmstrongNum { public static void main(String args[]) { int n=151,sum = 0, temp, r;
52

temp = n; while( temp != 0 ) { r = temp%10; sum = sum + r*r*r; temp = temp/10; } if ( n == sum ) System.out.println("Entered number is an armstrong number."); else System.out.println("Entered number is not an armstrong number."); } } OUTPUT:

Program to check number is palindrome or not


class Palindrome1 { public static void main(String args[]) { int rem=0,rev=0,c=0;
53

int num,z=123; num=z; while(num>0) { rem=num%10; num=num/10; rev=(rev*10)+rem; } System.out.println("reverse of a number is" + rev); if(z==rev) { System.out.println("number is palindrome"); } else { System.out.println("number is not palindrome"); } } }

54

OUTPUT:

55

Program to find whether the entered number is prime or not


class Prime1 { public static void main(String args[]) { int num=5,i=2; while(i<=num-1) { if(num%i==0) { System.out.println("not a prime number"); break; } i++; } if(i==num) { System.out.println("Prime number"); } } } OUTPUT:

56

Program to display reverse of a number

class Reverse1 { public static void main(String args[]) { int rem=0,rev=0,c=0; int num,z=12345; num=z; while(num>0) { rem=num%10; num=num/10; rev=(rev*10)+rem; } System.out.println("reverse of a number is" + rev); } } OUTPUT:

57

Program to display 1 234 56789

class Print1to10 { public static void main(String args[]) { int i,n=3,j,k,b=1; for(i=1;i<=n;i++) { for(j=1;j<=(n-i);j++) System.out.print(" "); for(k=1;k<=2*i-1;k++) { System.out.print(b); b++; } System.out.print("\n"); } } } OUTPUT:

58

Program to calculate grade of a student

class Grade { public static void main(String args[]) { int marks=85; if(marks>79) System.out.println("grade= Honours"); else if(marks>59) System.out.println("grade= I division"); else if(marks>49) System.out.println("grae= II division"); else if(marks>39) System.out.println("grade= III division"); else System.out.println("Fail"); } } OUTPUT:

59

Program to perform arithmetic operations using switch


import java.*; import java.io.*; class Switchcase1 { public static String ReadString() { try { InputStreamReader input = new InputStreamReader(System.in); BufferedReader reader = new BufferedReader(input); return reader.readLine(); } catch (Exception e) { e.printStackTrace(); return ""; } } public static int ReadInteger() { try { InputStreamReader input = new InputStreamReader(System.in); BufferedReader reader = new BufferedReader(input);
60

return Integer.parseInt(reader.readLine()); } catch (Exception e) { e.printStackTrace(); return 0; } } public static void main(String[] args) { int a, b, opcode, result; System.out.println("Program for Addition, Subtraction, Multiplication and Division\n"); while (true) { System.out.print("Enter Your Choice: 1 - Add, 2 - Sub, 3 - Mul, 4 - Div: "); opcode = ReadInteger(); if (opcode < 1 || opcode > 4) return; System.out.print("Enter First Number:"); a = ReadInteger(); System.out.print("Enter Second Number:"); b = ReadInteger(); switch (opcode) { case 1: result = a + b; System.out.println(result); break;
61

case 2: result = a - b; System.out.println(result); break; case 3: result = a * b; System.out.println(result); break; case 4: result = a / b; System.out.println(result); break; default: break; } } } }

62

OUTPUT:

63

Program to print Fibonacci series

class Fibbo { public static void main(String args[]) { int x,y,z=0; System.out.println("The Fibonacci Series :\n"); System.out.println("=============== =====\n"); System.out.println("0\n1"); for (x=0,y=1 ; y<1000 ; x=z) { z=y; y=x+y; System.out.println(y); } } }

64

OUTPUT:

65

Program to calculate area of two boxes

class box { double width; double height; double length; } class mybox1 { public static void main( String args[ ] ) { box b1=new box(); b1.width=12; b1.height=10; b1.length=10; double vol1=b1.width*b1.height*b1.length; System.out.println("volume=" + vol1); b1.width=10; b1.height=10; b1.length=10; vol1=b1.width*b1.height*b1.length; System.out.println("volume=" + vol1); } }

66

OUTPUT:

67

Program to display employee information

import java.io.*; import java.util.*; class Employee { public int id; public String name; public String address; public static int count = 0; public Employee() { } public Employee(int id, String name,String address) { super(); this.id = id; this.name = name; this.address=address; count++; } public int getId() { return id; } public String getName() { return name; }
68

} public class Employee1 { public static void main(String[] args) throws Exception { ListEmployee list = new ArrayListEmployee(); list.add(new Employee(1, "A","Delhi")); list.add(new Employee(2, "B","Mumbai")); list.add(new Employee(3, "C","Chennai")); list.add(new Employee(4, "D","Kolkata")); System.out.println(" "); System.out.print("Enter Employee Id: "); Scanner input=new Scanner(System.in); int id = input.nextInt(); for (Employee s : list){ if(id == s.getId()) { System.out.println("Name and Address of employee is: "); System.out.print(s.getName()+" " +s.getAddress()); } } }
69

public String getAddress() { return address; }

} OUTPUT: Name and Address of employee is: 1 , A , Delhi 2 , B , Mumbai 3 , C , Chennai 4 , D , Kolkata

70

Program to show multiple inheritance


class student { int rollno; void getnumber(int n) { rollno=n; } void putnumber() { System.out.println("Roll no is" + rollno); } } class test extends student { float part1,part2; void getmarks(float p1,float p2) { part1=p1; part2=p2; } void putmarks() { System.out.println("Marks obtained"); System.out.println("part1= " + part1); System.out.println("part2= " + part2); } } interface sports {
71

float sportwt=6.0F; void putwt(); } class result extends test implements sports { float total; public void putwt() { System.out.println("sports wt=" + sportwt); } void display() { total=part1+part2+sportwt; putnumber(); putmarks(); putwt(); System.out.println("Total score=" + total); } } class Multiple1 { public static void main(String args[]) { result r1=new result(); r1.getnumber(10); r1.getmarks(20.5F,30.5F); r1.display(); } }

72

OUTPUT:

Program to show multilevel inheritance


class Box { private double width;

73

} class Boxweight extends Box


74

private double height; private double depth; Box(Box ob) { width=ob.width; height=ob.height; depth=ob.depth; } Box(double w,double h,double d) { width=w; height=h; depth=d; } Box() { width=-1; height=-1; depth=-1; } Box(double l) { width=height=depth=l; } double volume() { return width*height*depth; }

{ double weight; Boxweight(Boxweight ob) { super(ob); weight=ob.weight; } Boxweight(double w,double h,double d,double m) { super(w,h,d); weight=m; } Boxweight() { super(); weight=-1; } Boxweight(double l,double m) { super(l); weight=l; } class Boxcolor extends Boxweight { double color; Boxcolor(Boxcolor ob) { super(ob); color=ob.color;
75

} Boxcolor(double w,double h,double d,double m,double c) { super(w,h,d,m); color=c; } Boxcolor() { super(); color=-1; } Boxcolor(double l,double m,double c) { super(l,m); color=l; } } } class Multilevel1 { public static void main(String args[]) { Boxcolor c1=new Boxcolor(20,30,40,50,60); Boxcolor c2=new Boxcolor(30,60,70,40,90); double vol; vol=c1.volume(); System.out.println("the volume of c1 = " + vol);

76

System.out.println("the c1.weight); System.out.println("the c1.color); vol=c2.volume(); System.out.println("the vol); System.out.println("the c2.weight); System.out.println("the c2.color); } }

weight of c1 = " + color of c1 = " + volume of c2 = " + weight of c2 = " + color of c2 = " +

Program to show run time polymorphism using method overriding


class Figure {
77

double dim1; double dim2; Figure(double a,double b) { dim1=a; dim2=b; } double area() { System.out.println("Area of figure is undefined"); return 0; } } class Rect extends Figure { Rect(double a,double b) { super(a,b); } double area() { System.out.println("Inside area of rectangle is"); return dim1*dim2; } } class Tri extends Figure { Tri(double a,double b)
78

{ super(a,b); } double area() { System.out.println("Inside area of Tri is "); return (dim1*dim2)/2; } } class Runtimepoly { public static void main(String args[]) { Figure f=new Figure(10,10); Rect r=new Rect(9,5); Tri t=new Tri(10,12); Figure figref; figref=r; System.out.println("Area is " + figref.area()); figref=t; System.out.println("Area is " + figref.area()); figref=f; System.out.println("Area is " + figref.area()); } } OUTPUT:

79

Program to pass object as parameters

class Test { int a,b; Test(int i,int j) { a=i; b=j; } boolean equals(Test o) { if(o.a==a && o.b==b) return true; else return false; } } class Testob { public static void main (String args[]) { Test ob1=new Test(100,200); Test ob2=new Test(100,200); Test ob3=new Test(-1,-1); System.out.println("ob1==ob2" + ob1.equals(ob2)); System.out.println("ob1==ob2" + ob1.equals(ob3)); }
80

} OUTPUT:

81

Program to calculate sum of area of rectangles


import java.io.*; class Rectarea { public static void main(String[] args) {

int l=0; int w=0; try { BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter length of rectangle : "); l = Integer.parseInt(br1.readLine()); System.out.println("Enter width of rectangle : "); w = Integer.parseInt(br1.readLine()); int area = l*w; System.out.println("Area of Rectangle : "+area); } catch(Exception e) { System.out.println("Error : "+e); }
82

} } OUTPUT:

83

Program to show call by value

class test { void method(int i,int j) { i *= 2; j /= 2; } } class Callvalue { public static void main(String args[]) { int a=15,b=20; System.out.println("a & b="+ a + " "+ b); test obj=new test(); obj.method(a,b); System.out.println("a & b="+ a +" "+ b); } } OUTPUT:

Program to show call by reference


class test
84

{ int a,b; test (int i,int j) { a=i; b=j; } void method1(test ob) { ob.a*=2; ob.b/=2; } } class Callrefrence { public static void main(String args[]) { test ob1 = new test(15,20); ob1.method1(ob1); System.out.println("ob1.a=" + ob1.a); System.out.println("ob1.b=" + ob1.b); } }

85

OUTPUT:

86

Program to calculate sum of two complex numbers

import java.io.*; class Complex { int real1,imag1,real2,imag2; void get() throws IOException { BufferedReader Br = new BufferedReader(new InputStreamReader(System.in)); System.out.print("\n Enter the real part of first complex number: "); real1=Integer.parseInt(Br.readLine()); System.out.print(" Enter the imaginary part of first complex number: "); imag1=Integer.parseInt(Br.readLine()); System.out.print("\n Enter the real part of second complex number: "); real2=Integer.parseInt(Br.readLine()); System.out.print(" Enter the imaginary part of second complex number : "); imag2=Integer.parseInt(Br.readLine()); } void display() { System.out.println(" \n Sum of complex numbers " + real1 + "+" + imag1 + "i and " + real2 + "+" + imag2 + "i is " + (real1+real2) + "+" + (imag1+imag2) + "i");
87

} public static void main(String p[]) throws IOException { Complex cmp = new Complex(); cmp.get(); cmp.display(); } } OUTPUT:

Program to access private members


class Box {
88

private double width; private double height; private double depth; Box(Box ob) { width=ob.width; height=ob.height; depth=ob.depth; } Box(double w,double h,double d) { width=w; height=h; depth=d; } Box() { width=-1; height=-1; depth=-1; } Box(double l) { width=height=depth=l; } double volume() { return width*height*depth; } }
89

class Boxweight extends Box { double weight; Boxweight(Boxweight ob) { super(ob); weight=ob.weight; } Boxweight(double w,double h,double d,double m) { super(w,h,d); weight=m; } Boxweight() { super(); weight=-1; } Boxweight(double l,double m) { super(l); weight=m; } } class Privatemember { public static void main(String args[]) {

90

Boxweight b1=new Boxweight(10,20,30,40.5); Boxweight b2=new Boxweight(2,3,4,5.5); Boxweight b3=new Boxweight(); Boxweight b4=new Boxweight(3,2); Boxweight b=new Boxweight(b1); double vol; vol=b1.volume(); System.out.println("volume of b1=" + vol); System.out.println("weight of b1=" + b1.weight); System.out.println(); } } OUTPUT:

Program to use static keyboard


class Usestatic { static int a=3;
91

static int b; static void method(int x) { System.out.println("x=" + x); System.out.println("a=" + a); System.out.println("b=" + b); } static { System.out.println("static block"); b=a*4; } public static void main(String args[]) { method(40); } } OUTPUT:

Program to sort a list of numbers


class Numbersorting { public static void main(String args[]) {
92

int number[]={55,40,80,65,71}; int n; n=number.length; System.out.println("Given list :"); for(int i=0;i<n;i++) { System.out.println(" " + number[i]); } System.out.println("/n"); for(int i=0;i<n;i++) { for(int j=i+1;j<n;j++) { if(number[i]<number[j]) { int temp=number[i]; number[i]=number[j]; number[j]=temp; } } } System.out.println("Sorted list"); for(int i=0;i<n;i++) { System.out.println(" " +number[i]); } System.out.println(" "); } }

93

OUTPUT:

Program to show alphabetic order of strings


class Stringordering { static String name[]={"Punjab","Haryana","Himachal","Delh i"}; public static void main(String args[])
94

{ 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:

95

Program to show Vectors and Arrays


import java.util.*; class Languagevector

96

{ public static void main(String args[]) { Vector list=new Vector(); int length=args.length; for(int i=0;i<length;i++) { list.addElement(args[i]); } list.insertElementAt("COBOL",1); int size=list.size(); String listArray[]=new String[size]; list.copyInto(listArray); System.out.println("List of languages"); for(int i=0;i<size;i++) { System.out.println(listArray[i]); } } }

97

OUTPUT:

98

Program to show use of Wrapper class import java.io.*; class Wrapper1 { public static void main(String args[]){ Float principalAmount=new Float(0); Float interestRate=new Float(0); int numYears=0; try{ DataInputStream in=new DataInputStream(System.in); System.out.print("Enter principal amount"); System.out.flush(); String principalString=in.readLine(); principalAmount=Float.valueOf(principalStri ng); System.out.print("Enter interest rate"); System.out.flush(); String interestString=in.readLine(); interestRate=Float.valueOf(interestString); System.out.print("Enter number of years"); System.out.flush(); String yearsString=in.readLine(); numYears=Integer.parseInt(yearsString); } catch(IOException e) { System.out.println("I/O Error"); System.exit(1); }
99

float value=loan(principalAmount.floatValue(),intere stRate.floatValue(),numYears); printline(); System.out.println("Final values =" + value); printline(); } static float loan(float p,float r,int n) { int year=1; float sum=p; while(year<=n) { sum=sum*(1+r); year=year+1; } return sum; } static void printline() { for(int i=1;i<=30;i++) { System.out.print("="); } System.out.println(" "); } } OUTPUT:

100

101

Program to illustrate the concept of interface


interface Area { final static float pi=3.14F; float compute (float x,float y); } class Rectangle implements Area { public float compute(float x,float y) { return(x*y); } } class Circle implements Area { public float compute(float x,float y) { return(pi*x*x); } } class Interfacetest { public static void main(String args[]) { Rectangle rect=new Rectangle(); Circle cir=new Circle(); Area a1; a1=rect;
102

System.out.println("area of rectangle is " + a1.compute(10,20)); a1=cir; System.out.println("area of circle =" + a1.compute(10,0)); } } OUTPUT:

Program to find sum of each row and column of a matrix


103

public class Matrix1 { private static int column; public static void main(String[] args) { int [][]array1={{1,2,3,4},{5,6,7},{8,9}, {10}}; System.out.println("Values in array1 by row are"); OutputArray(array1); } public static void OutputArray(int[][] array1) { int sum=0; for(int row=0;row<array1.length;row++) { for( column=0;column<array1[row].length;c olumn++) System.out.print("\t"+"\t" + array1[row] [column]); System.out.println(); } System.out.println(); sum=sum+array1.length; System.out.print("sum=\t"+sum+"\t"); System.out.println(); } }

104

OUTPUT:

105

Program to show concept of packages


package package1; class ClassA { public void displayA() { System.out.println("class A"); } } import package1.ClassA; public class packagetest { public static void main(String args[]) { ClassA A=new ClassA(); ClassA.displayA(); } } OUTPUT:

106

Program to import classess from other packages


package package2; public class ClassB { protected int m=10; public void displayB() { System.out.println("Class B"); System.out.println("m=" + m); } } import package1.ClassA; import package2.*; class Packagetest2 { public static void main(String args[]) { ClassA objectA=new ClassA(); ClassB objectB=new ClassB(); objectA.displayA(); objectB.displayB(); } }

107

OUTPUT:

108

Program to subclassing an importing class


import package2.ClassB; class ClassC extends ClassB { int n=20; void displayC() { System.out.println("Class C"); System.out.println("m =" + m); System.out.println("n =" + n); } } class Packagetest3 { public static void main(String args[ ]) { ClassC objectC=new ClassC( ); objectC.displayB( ); objectC.displayC( ); } } OUTPUT:

109

Program to add class to packages


package package1; public class Classe { public void displayE() { System.out.println("class E"); } } import package1.ClassA; import package1.*; public class Packagetest1 { public static void main(String args[]) { ClassA objectA=new ClassA(); Classe objectE=new Classe(); objectA.displayA(); objectE.displayE(); } } OUTPUT:

Program to perform all arithmetic operation in package


110

package package4; public class Arithmetic { public void arith(){ int a=30,b=20,c; c=a+b; System.out.println("Addition of a & b is " + c); c=a-b; System.out.println("Sub of a & b is " +c); c=a*b; System.out.println("Mul of a & b is " + c); c=a/b; System.out.println("Division of a & b is " + c); } } import package4.Arithmetic; class Packagetest5{ public static void main(String args[]) { Arithmetic a1=new Arithmetic(); a1.arith(); } }

111

OUTPUT:

112

Program to find cube in an package


package package3; public class Cube1 { public void cubee() { int a=3,b; b=a*a*a; System.out.println("cube of a is " + b); } } import package3.Cube1; class Packagetest4 { public static void main(String args[]) { Cube1 c1=new Cube1(); c1.cubee(); } } OUTPUT:

Program to use static import


113

import static java.lang.Math.*; public class Mathop { public void circle(double r) { double area=PI*r*r; System.out.println("The area of circle is " + area); } public static void main(String args[]) { Mathop obj=new Mathop(); obj.circle(2.3); } } OUTPUT: The area of circle is 16.619025137490002

Program to create threads using the thread classes


114

class A extends Thread { public void run() { for(int i=0;i<=5;i++) { System.out.println(" From Thread A :" + i); } System.out.println("exit from A"); } } class B extends Thread { public void run() { for(int i=0;i<=5;i++) { System.out.println(" From Thread B :" + i); } System.out.println("exit from B"); } } class C extends Thread { public void run() { for(int i=0;i<=5;i++) { System.out.println(" From Thread C :" + i); }
115

System.out.println("exit from C"); } } class Threadtest { public static void main(String args[]) { new A().start(); new B().start(); new C().start(); } } OUTPUT:

Program to display use of yield(),stop() and sleep() methods class A extends Thread
116

{ public void run() { for(int i=1;i<=5;i++) { if(i==1) yield(); System.out.println(" From Thread A :" + i); } System.out.println("exit from A"); } } class B extends Thread { public void run() { for(int j=1;j<=5;j++) { System.out.println(" From Thread B :" + j); if(j==3) stop(); } System.out.println("exit from B"); } } class C extends Thread { public void run() { for(int k=1;k<=5;k++) { System.out.println(" From Thread C :" + k);
117

if(k==1) try { sleep(1000); } catch(Exception e) { } } System.out.println("exit from C"); } } class Threadmethod { public static void main(String args[]) { A threadA=new A(); B threadB=new B(); C threadC=new C(); System.out.println("start thread A"); threadA.start(); System.out.println("start thread B"); threadB.start(); System.out.println("start thread C"); threadC.start(); System.out.println("End of main thread"); } } OUTPUT:

118

Program to display use of priority in thread


class A extends Thread { public void run() { for(int i=1;i<=4;i++) {

119

System.out.println(" From Thread A :" + i); } System.out.println("exit from A"); } } class B extends Thread { public void run() { for(int j=1;j<=4;j++) { System.out.println(" From Thread B :" + j); } System.out.println("exit from B"); } } class C extends Thread { public void run() { for(int k=0;k<=4;k++) { System.out.println(" From Thread C :" + k); } System.out.println("exit from C"); } } class Threadpriority { public static void main(String args[])
120

{ A threadA=new A(); B threadB=new B(); C threadC=new C(); threadC.setPriority(Thread.MAX_PRIORITY); threadC.setPriority(threadA.getPriority()+1); threadC.setPriority(Thread.MIN_PRIORITY); System.out.println("Start thread A"); threadA.start(); System.out.println("Start thread B"); threadB.start(); System.out.println("Start thread C"); threadC.start(); System.out.println("End of main thread"); } }

121

OUTPUT:

122

Program to show use of runnable interface in thread

class A implements Runnable { public void run() { for(int i=1;i<=10;i++) { System.out.println("/tThreadA : " +i); } System.out.println("End of ThreadA"); } } class Runnabletest { public static void main(String args[]) { A runnable=new A(); Thread threadA=new Thread(runnable); threadA.start(); System.out.println("End of main thread" ); } }

123

OUTPUT:

124

Program to illustrate suspend(),resume() and stop() operation

class sus_res_stop implements Runnable { Thread Th; boolean suspend_flag,stop_flag; sus_res_stop(String tN) { Th=new Thread(this,tN); suspend_flag=false; stop_flag=false; Th.start(); } public void run() { try { int j=1; while(++j<20) { synchronized(this) { while(suspend_flag) { wait(); } if(stop_flag) { break;
125

} } } } catch(InterruptedException IE) { System.out.println("thread interrupted"); } } synchronized void my_suspend() { suspend_flag=true; } synchronized void my_resume() { suspend_flag=false; notify(); } synchronized void my_stop() { suspend_flag=false; stop_flag=true; notify(); } } public class eg_SRS { public static void main(String[] args) { try
126

{ sus_res_stop S_R_S_T=new sus_res_stop("SRS"); System.out.println("Thread S_R_S_T is created and started"); Thread.sleep(2000); S_R_S_T.my_suspend(); System.out.println("Thread S_R_S_T is suspended"); Thread.sleep(2000); S_R_S_T.my_resume(); System.out.println("Thread S_R_S_T is resumed"); Thread.sleep(2000); S_R_S_T.my_suspend(); System.out.println("Thread S_R_S_T is suspended"); Thread.sleep(2000); S_R_S_T.my_resume(); System.out.println("Thread S_R_S_T is resumed"); Thread.sleep(2000); S_R_S_T.my_stop(); System.out.println("Thread S_R_S_T is stopped"); } catch(InterruptedException IE) { System.out.println("Generated interrupted exception");
127

} OUTPUT:

} }

128

Program to display compile time error class Error1 { public static void main(String args[]){ System.out.println("Hello Java") } }
OUTPUT:

129

Program to display Run time errors


class Error2 { public static void main(String args[]) { int a=10; int b=20; int c=20; int x=a/(b-c); System.out.println("x=" + x); int y=a/(b+c); System.out.println("y=" + y);} } OUTPUT:

130

Program to show use of try and catch for exception handling


class Error3 { public static void main(String args[]) { int a=10; int b=5; int c=5; int x,y; try { x=a/(b-c); } catch(ArithmeticException e) { System.out.println("Division by zero"); } y=a/(b+c); System.out.println("y=" + y); } } OUTPUT:

131

Program of catching invalid command line arguments class Clineinput { public static void main(String args[]) { int invalid = 0; int number ,count=0; for(int i=0;i<args.length;i++) { try { number=Integer.parseInt(args[i]); } catch(NumberFormatException e) { invalid =invalid +1; System.out.println("invalid number :" + args[i]); continue; } count=count+1; } System.out.println("valid number=" + count); System.out.println("invalid number=" + invalid); } }

132

OUTPUT:

133

Program to show nested try statements

class Nestedtry1 { public static void main(String args[]) { try { int a=2,b=4,c=2,x=7,z; int p[]={2}; p[3]=33; try { z=x/((b*b)-(4*a*c)); System.out.println("the value of z is=" + z); } catch(ArithmeticException e) { System.out.println("division by zero in arithmetic"); } } catch(ArrayIndexOutOfBoundsException e) { System.out.println("Array index is out of bound"); } } }
134

OUTPUT:

135

Program to show use of multiple catch blocks

class Error4 { public static void main(String args[]) { int a[]={5,10}; int b=5; try { int x=a[2]/b-a[1]; } catch(ArithmeticException e) { System.out.println("Division by zero"); } catch(ArrayIndexOutOfBoundsException e) { System.out.println("Array index error"); } catch(ArrayStoreException e) { System.out.println("Wrong data type"); } int y=a[1]/a[0]; System.out.println("y=" + y); } }

136

OUTPUT:

137

Program of throwing our own exception

import java.lang.Exception; class MyException extends Exception { MyException(String message) { super(message); } } class Testmyexception { public static void main(String args[]) { int x=5,y=1000; try { float z=(float)x/(float)y; if(z < 0.01) { throw new MyException("number is too small"); } } catch(MyException e) { System.out.println("caught my exception"); System.out.println(e.getMessage()); } finally
138

{ System.out.println("I am always here"); } } } OUTPUT:

Program to show use of throws


139

class Throws1 { static void divide_m() throws ArithmeticException { int x=22,y=0,z; z=x/y; } public static void main9String args[]) { try { divide_m(); } catch(ArithmeticException e) { System.out.println("Caught the exception" + e); } } } OUTPUT:

Program to display the Hellojava Applet


140

Applet file <html> <applet code=Hellojava.class width=300 height=300 > </applet> </html> Java file import java.awt.*; import java.applet.*; public class Hellojava extends Applet { public void paint(Graphics g) { String str; str="HELLO JAVA"; System.out.println(str); g.drawString(str,10,100); } }

141

OUTPUT:

142

Program to display hellojava using param in applet


<html> <head> <title> Welcome to java Applet </title> </head> <body> <applet code=Hellojavaparam.class width=400 height=200> <PARAM NAME="string" value="Applet"> </applet> </body> </html> import java.awt.*; import java.applet.*; public class Hellojavaparam extends { String str; public void init() { str=getParameter("String"); if(str==null) { str="Java"; } else { str="Hello" + str;
143

} OUTPUT:

} } public void paint(Graphics g) { g.drawString(str,10,100); }

Program to display numerical values with applet


<html>
144

<applet code=Numvalues.class width=300 height=300> </applet> </html> import java.awt.*; import java.applet.*; public class Numvalues extends Applet { public void paint(Graphics g) { int values1=10; int values2=20; int sum=values1+values2; String s="sum:" +String.valueOf(sum); g.drawString(s,100,100); } }

145

OUTPUT:

146

Program to give interactive input to an applet


<html> <applet code=Userin.class width=300 height=200> </applet> </html> import java.awt.*; import java.applet.*; public class Userin extends Applet { TextField text1,text2; public void init() { text1=new TextField(8); text2=new TextField(8); add (text1); add (text2); text1.setText("0"); text2.setText("0"); } public void paint(Graphics g) { int x=0,y=0,z=0; String s1,s2,s; g.drawString("Input a number in each box",10,50); try {
147

s1=text1.getText(); x=Integer.parseInt(s1); s2=text2.getText(); y=Integer.parseInt(s2); } catch(Exception ex) { } z=x+y; s=String.valueOf(z); g.drawString("The Sum is:" ,10,75); } public boolean action (Event event,Object object) { repaint(); return true; } } OUTPUT:

148

Program to show working of setbackground color

import java.applet.Applet; import java.awt.Color; import java.awt.Graphics; public class Setbkgcolor extends Applet { public void paint(Graphics g) { setBackground(Color.red); } } <html> <body> <applet code=Setbkgcolor.class width=200 height=200> </applet> </body> </html

149

OUTPUT:

150

Program to show keyboard event in an applet

<html> <body> <applet code=Event1.class width=200 height=200> </applet> </body> </html> import java.applet.*; import java.awt.*; import java.awt.event.*; public class Event1 extends Applet implements KeyListener { public void init() { addKeyListener(this); } public void keyTyped(KeyEvent KB) { } public void keyReleased(KeyEvent KB) { showStatus("Key on the keyboard is released"); } public void keyPressed(KeyEvent KB) {
151

showStatus("A key on the keyboard is pressed"); } Font f1=new Font("courier new",Font.BOLD,20); public void paint(Graphics GA) { GA.setFont(f1); GA.setColor(Color.blue); GA.drawString("This applet sense the up/down motion of keys",20,120); } } OUTPUT:

152

Program to demonstrate Labels <html> <body> <applet code=Labeldemo.class width=200 height=300> </applet> </body> </html> import java.awt.*; import java.applet.*; public class Labeldemo extends Applet{ public void init() { Label one=new Label("One"); Label two=new Label("Two"); Label three=new Label("Three"); add(one); add(two); add(three); } }

153

OUTPUT:

154

Program to demonstrate buttons in an applet <html> <body> <applet code=Buttondemo.class width=200 height=300> </applet> </body> </html> import java.awt.*; import java.awt.event.*; import java.applet.*; public class Buttondemo extends Applet implements ActionListener { String msg=""; Button yes,no,maybe; public void init() { yes=new Button("Yes"); no=new Button("No"); maybe=new Button("Undecided"); add(yes); add(no); add(maybe); yes.addActionListener(this); no.addActionListener(this); maybe.addActionListener(this); }
155

public void actionPerformed(ActionEvent ae) { String str=ae.getActionCommand(); if(str.equals("Yes")) { msg="u pressed Yes"; } else if(str.equals("No")) { msg="U pressed No"; } else { msg="u pressed Undecided"; } repaint(); } public void paint(Graphics g) { g.drawString(msg,6,100); } }

156

OUTPUT:

157

Program to perform arithmetic operations in buttons <html> <body> <applet code=Buttonarith.class width=200 height=200> </applet> </body> </html import java.awt.*; import java.awt.event.*; import java.applet.*; public class Buttonarith extends Applet implements ActionListener { String msg=""; Button add,sub,divide; public void init() { add=new Button("Addition"); sub=new Button("Subtract"); divide=new Button("Division"); add(add); add(sub); add(divide); add.addActionListener(this); sub.addActionListener(this); divide.addActionListener(this); }
158

public void actionPerformed(ActionEvent ae) { String str=ae.getActionCommand(); int a=10,b=10,c; if(str.equals("add")) { c=a+b; System.out.println("the additio is" + c); } else if(str.equals("sub")) { c=a*b; msg="the sub is"; } else { c=a/b; msg="the divide is "; } repaint(); } public void paint(Graphics g) { g.drawString(msg,6,100); } }

159

OUTPUT:

160

Program to demonstrate checkbox in an applet


<html> <body> <applet code=Checkboxdemo.class width=200 height=300> </applet> </body> </html> import java.awt.*; import java.awt.event.*; import java.applet.*; public class Checkboxdemo extends Applet implements ItemListener { String msg=""; Checkbox Win98,winNT,solaris,mac; public void init() { Win98=new Checkbox("Windows 98",null,true); winNT=new Checkbox("Windows NT"); solaris=new Checkbox("Solaris"); mac=new Checkbox("MacOS"); add(Win98); add(winNT); add(solaris); add(mac); Win98.addItemListener(this);
161

winNT.addItemListener(this); solaris.addItemListener(this); mac.addItemListener(this); } public void itemStateChanged(ItemEvent ie) { repaint(); } public void paint(Graphics g) { msg="Current state:"; g.drawString(msg,6,80); msg="Windows 98: " + Win98.getState(); g.drawString(msg,6,100); msg="Windows NT: " + winNT.getState(); g.drawString(msg,6,120); msg="Solaris: " + solaris.getState(); g.drawString(msg,6,140); msg="MacOS: " + mac.getState(); g.drawString(msg,6,160); } }

162

OUTPUT:

163

Program to demonstrate checkboxgroup

<html> <body> <applet code =Checkboxgroup.class width=200 height=300> </applet> </body> </html> import java.awt.*; import java.awt.event.*; import java.applet.*; public class Checkboxgroup extends Applet implements ItemListener { String msg=""; Checkbox Win98,winNT,solaris,mac; CheckboxGroup cbg; public void init() { cbg=new CheckboxGroup(); Win98=new Checkbox("Windows 98",cbg,true); winNT=new Checkbox("Windows NT",cbg,false); solaris=new Checkbox("Solaris",cbg,false); mac=new Checkbox("MacOS",cbg,false); add(Win98); add(winNT);
164

add(solaris); add(mac); Win98.addItemListener(this); winNT.addItemListener(this); solaris.addItemListener(this); mac.addItemListener(this); } public void itemStateChanged(ItemEvent ie) { repaint(); } public void paint(Graphics g) { msg="Current selection:"; msg += cbg.getSelectedCheckbox().getLabel(); g.drawString(msg,6,100); } }

165

OUTPUT:

166

Program to demonstrate choice list


<html> <body> <applet code=ChoiceDemo.class width=200 height=300> </applet> </body> </html> import java.awt.*; import java.awt.event.*; import java.applet.*; public class ChoiceDemo extends Applet implements ItemListener { Choice os,browser; String msg = " "; public void init() { os = new Choice(); browser = new Choice(); os.add("Windows 98"); os.add("Windows NT"); os.add("Solaris"); os.add("MacOS"); browser.add("Netscape 1.1"); browser.add("Netscape 2.x"); browser.add("Netscape 3.x"); browser.add("Netscape 4.x"); browser.add("Internet Explorer 2.0");
167

browser.add("Internet Explorer 3.0"); browser.add("Internet Explorer 4.0"); browser.add("Lynx 2.4"); browser.select("Netscape 4.x"); add(os); add(browser); os.addItemListener(this); browser.addItemListener(this); } public void itemStateChanged(ItemEvent ie) { repaint(); } public void paint (Graphics g) { msg = "Current OS: "; msg += os.getSelectedItem(); g.drawString(msg, 6, 120); msg = "Current Browser: "; msg += browser.getSelectedItem(); g.drawString(msg, 6, 140); } }

168

OUTPUT:

169

Program to demonstrate lists

<html> <body> <applet code=ListDemo.class width=200 height=300> </applet> </body> </html> import java.awt.*; import java.awt.event.*; import java.applet.*; public class ListDemo extends Applet implements ActionListener { List os,browser; String msg = " "; public void init() { os = new List(4,true); browser = new List(4,false); os.add("Windows 98"); os.add("Windows NT"); os.add("Solaris"); os.add("MacOS"); browser.add("Netscape 1.1"); browser.add("Netscape 2.x"); browser.add("Netscape 3.x"); browser.add("Netscape 4.x"); browser.add("Internet Explorer 2.0");
170

browser.add("Internet Explorer 3.0"); browser.add("Internet Explorer 4.0"); browser.add("Lynx 2.4"); browser.select(1); add(os); add(browser); os.addActionListener(this); browser.addActionListener(this); } public void actionPerformed(ActionEvent ae) { repaint(); } public void paint (Graphics g) { msg = "Current OS: "; msg += os.getSelectedIndexes(); g.drawString(msg, 6, 120); msg = "Current Browser: "; msg += browser.getSelectedItem(); g.drawString(msg, 6, 140); } }

171

OUTPUT:

172

Program to demonstrate scroll bars

<html> <body> <applet code=SBdemo.class width=200 height=300> </applet </body> </html> import java.awt.*; import java.awt.event.*; import java.applet.*; public class SBdemo extends Applet implements AdjustmentListener,MouseMotionListener { String msg=""; Scrollbar vertSB,horzSB; public void init() { int width=Integer.parseInt(getParameter("width")) ; int height=Integer.parseInt(getParameter("height" )); vertSB = new Scrollbar(Scrollbar.VERTICAL,0,1,0,height); horzSB = new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,width); add(vertSB);
173

add(horzSB); vertSB.addAdjustmentListener(this); horzSB.addAdjustmentListener(this); addMouseMotionListener(this); } public void adjustmentValueChanged(AdjustmentEvent ae) { repaint(); } public void mouseDragged(MouseEvent me) { int x=me.getX(); int y=me.getY(); vertSB.setValue(y); horzSB.setValue(x); repaint(); } public void mouseMoved(MouseEvent me) { } public void paint(Graphics g){ msg="Vertical:"+ vertSB.getValue(); msg+="Horizontal:" + horzSB.getValue(); g.drawString(msg,6,160); g.drawString("*",horzSB.getValue(),vertSB.g etValue()); } }

174

OUTPUT:

175

Program to demonstrate left-aligned layout

<html> <body> <applet code=Flowlayoutdemo.class width=200 height=300> </applet> </body> </html> import java.awt.*; import java.awt.event.*; import java.applet.*; public class Flowlayoutdemo extends Applet implements ItemListener { String msg=""; Checkbox Win98,winNT,solaris,mac; public void init() { setLayout(new FlowLayout(FlowLayout.LEFT)); Win98=new Checkbox("Windows 98",null,true); winNT=new Checkbox("Windows NT"); solaris=new Checkbox("Solaris"); mac=new Checkbox("MacOS"); add(Win98); add(winNT); add(solaris); add(mac);
176

Win98.addItemListener(this); winNT.addItemListener(this); solaris.addItemListener(this); mac.addItemListener(this); } public void itemStateChanged(ItemEvent ie) { repaint(); } public void paint(Graphics g) { msg="Current state:"; g.drawString(msg,6,80); msg="Windows 98: " + Win98.getState(); g.drawString(msg,6,100); msg="Windows NT: " + winNT.getState(); g.drawString(msg,6,120); msg="Solaris: " + solaris.getState(); g.drawString(msg,6,140); msg="MacOS: " + mac.getState(); g.drawString(msg,6,160); } }

177

OUTPUT:

178

Program to show all graphics functions in an applet

<html> <body> <applet code=Graphicmethods.class width=300 height=300> </applet> </body> </html> import java.awt.*; import java.applet.*; public class Graphicmethods extends Applet { String s=new String(); String s1=new String(); String s2=new String(); Font f1=new Font("Courier New ", Font.BOLD,20); public void paint(Graphics GA) { GA.setFont(f1); GA.setColor(Color.blue); GA.drawString("Illustration of methods of graphics class",200,250); Font f2=GA.getFont(); s=f2.toString(); GA.drawString(s,5,540); GA.setColor(Color.green); Color col=GA.getColor(); s2=col.toString();
179

GA.drawString(s2,5,560); GA.fillRect(500,15,70,90); GA.drawRect(160,5,60,60); GA.drawOval(10,120,155,95); GA.setColor(Color.yellow); GA.fillOval(700,140,50,150); GA.setColor(Color.black); GA.drawLine(380,100,200,180); GA.drawArc(400,150,180,280,90,70); int x2[]={200,120,280,240}; int z2=4; int y2[]={260,370,370,270}; GA.setColor(Color.blue); GA.fillPolygon(x2,y2,z2); GA.setColor(Color.red); GA.drawRect(15,15,30,50); FontMetrics f3=GA.getFontMetrics(); s1=f3.toString(); GA.drawString(s1,5,580); GA.setColor(Color.magenta); GA.fillRoundRect(510,400,90,80,20,20); } }

180

OUTPUT:

181

Program to draw a Human face in an applet


<html> <body> <applet code=Face.class width=200 height=300> </applet> </body> </html> import java.awt.*; import java.applet.*; public class Face extends Applet { public void paint(Graphics g) { g.drawOval(40,40,120,150); g.drawOval(57,75,30,20); g.drawOval(110,75,30,20); g.fillOval(68,81,10,10); g.fillOval(121,81,10,10); g.drawOval(85,100,30,30); g.fillArc(60,125,80,40,180,180); g.drawOval(25,92,15,30); g.drawOval(160,92,15,30); } }

182

OUTPUT:

Program to draw triangle in an applet


<html>
183

<body> <applet code=Triangle2.class width=300 height=300> </applet> </body> </html> import java.awt.*; import java.applet.*; public class Triangle2 extends Applet { public void paint(Graphics g) { g.drawLine(200,150,160,300); g.drawLine(200,150,260,300); g.drawLine(160,300,260,300); } }OUTPUT:

Program to draw star in an applet


<html>
184

<body> <applet code=Star2.class width=300 height=300> </applet> </body> </html> import java.awt.*; import java.applet.*; public class Star2 extends Applet { public void paint(Graphics g) { Point[] points = new Point[9]; points[0] = new Point(28, 5); points[1] = new Point(27, 21); points[2] = new Point(13, 24); points[3] = new Point(27, 27); points[4] = new Point(28, 41); points[5] = new Point(31, 27); points[6] = new Point(42, 24); points[7] = new Point(31, 21); points[8] = new Point(28, 5); int i; for (i=1; i<points.length; i++) g.drawLine(points[i-1].x, points[i-1].y, points[i].x, points[i].y); } } OUTPUT:
185

Program to show use of control loops in an applet


<html> <body> <applet code=ControlLoop.class width=200 height=300>
186

</applet> </body> </html> import java.awt.*; import java . applet.*; public class ControlLoop extends Applet { public void paint (Graphics g) { for (int i=0; i<=4; i++) { if((i%2)==0) g.drawOval (120, i*60+10, 50, 50); else g.fillOval (120, 1*60+10, 50, 50) ; } } }

187

OUTPUT:

188

Program to store data to a file

import java.io.*; class WriteBytes { public static void main (String args[ ] ) { byte cities [ ] = {'D', 'E','L', 'H', 'I', 'M', 'A', 'D','R', 'A', 'S' , 'L','O','N','D','O','N' }; FileOutputStream outfile = null; try { outfile= new FileOutputStream ("city.txt") ; outfile. write (cities) ; outfile.close(); } catch (IOException ioe) { System.out.println (ioe) ; System.exit (-1) ; } } } OUTPUT:

Program to read data from a file


189

import java.io.*; class ReadBytes { public static void main(String args[]) { FileInputStream infile=null; int b; try { infile=new FileInputStream(args[0]); while((b=infile.read()) != -1) { System.out.println((char) b); } infile.close(); } catch(IOException ioe) { System.out.println(ioe); } } }

190

OUTPUT:

191

You might also like