Programs
Programs
<HTML> <HEAD> <TITLE> First Program in HTML </TITLE> <BODY> WELCOME TO HTML </BODY> </HEAD> </HTML> OUTPUT:
<html> <head> <body bgcolor=yellow text=red> <center> LINE </center> <HR ALIGN="center" WIDTH="20" SIZE="100" color=black> </HR> </head> </body> </html> OUTPUT:
<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:
<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
11
12
<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
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:
<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:
<p> Welcome <h1> First page </h1> End </body> </html> OUTPUT:
21
<p class=abc> Welcome to <SPAN class=xyz> HTML </SPAN> programming <p class=xyz> HTML <SPAN class=abc> Program </SPAN> </h1> </body> </html> OUTPUT:
OUTPUT:
25
26
27
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
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
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
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
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
34
35
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
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
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
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
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
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
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
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:
48
public static void main(String args[]) { int i=11; do { i--; System.out.println(i); }while(i>=2); } } OUTPUT:
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:
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:
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
56
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
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
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
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
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
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
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
} OUTPUT: Name and Address of employee is: 1 , A , Delhi 2 , B , Mumbai 3 , C , Chennai 4 , D , Kolkata
70
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:
73
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 = " +
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
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
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
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:
{ 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
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:
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:
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:
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:
{ 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
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
System.out.println("area of rectangle is " + a1.compute(10,20)); a1=cir; System.out.println("area of circle =" + a1.compute(10,0)); } } OUTPUT:
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
106
107
OUTPUT:
108
109
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
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
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
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
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
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
130
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
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
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
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
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:
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
} OUTPUT:
<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
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
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
<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
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
<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
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
<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
<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
<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
<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
182
OUTPUT:
<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:
<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
</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
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:
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