Vous êtes sur la page 1sur 202

HTML

VARIOUS TAGS USED IN HTML 1. <MARQUEE>. </MARQUEE>: This tag is used to scroll text horizontally across the screen. For example: <HTML> <HEAD> <TITLE> Marquee Tag </TITLE> </HEAD> <BODY> <h1><MARQUEE>MARQUEE TAG </MARQUEE></h1> </BODY> </HTML>

OUTPUT:

/*PROGRAM TO DISPLAY ATTRIBUTES OF BODY TAG*/ <html> <head> <title> body tags </title> </head> <body bgcolor = "black"> <i><b><center><fontcolor="red">Satnam Foundry</center></b></i> WELCOME in our Profile <font color="red"><h2> <br> <p><h1>Satnam Foundry </h1><h3>is a special manufacturer of iron and ironic products.This industry was setup under<h2> Jassal Enterprices</h2>This enterprice is located at Punjab,INDIA</center><br/> </body> </html> OUTPUT:

/*PROGRAM TO SHOW USE OF FONT & CENTER TAGS*/ <html> <head> <title> Delta Engineering Pvt.Ltd.</title> </head> <body background = "abc.jpg"> <font color="darkblue" size="5"><i><b><center>Delta Engineering Pvt.Ltd</center></b></i> <hr align="center" color="black"> <br> <center><fontcolor="PINK"><h1><MARQUEEDIRECTI ON="LEFT"> WELCOME</MARQUEE> <MARQUEE DIRECTION="RIGHT">IN OUR </MARQUEE><MARQUEEDIRECTION="LEFT">Profil e</MARQUEE></BLINK></h1></center> <font color="darkblue"><h2> <br> <p><h1><center>Delta Engineering Pvt.Ltd</center> </h1><h3>is a special manufacturer of wire and wire products..DEPL established in Mumbai,India is ideally located for shipments to any part of the world.Thhe main items of manufacture, in collaboration with Gulf Fencing Industry include protector gabions and protector fencing systems. </body> </html> OUTPUT:
4

/*PROGRAM TO SHOW USE OF MARQUEE, SUB & SUP TAG*/ <html> <head> <title>mathematical expression</title> </head> <body bgcolor=black> <font color=red> <center><h2><i><u> mathematical expression</u></i> <p> x<sup>2</sup>+y<sup>3</sup>=z <p> log<sub>10</sub> <p> a<sup>2</sup>+b log<sub>10</sub> </center> </body></html>

OUTPUT:

/*PROGRAM TO SHOW THE USE OF LIST TAG*/


<html> <head><title>assignment1</title> </head> <body> Parts Of The Computer <hr align="center"> <ol type="decimal"> <li>Input Devices <ol type=small-alpha> <li>Keyboard <li>Mouse <li>Magnetic Ink Character Reader <li>Optical Mark Reader <li>Trackball <li>Touch pad <li>Lightpen <li>Scanner </ol> <br> <br> <li>Output Devices <br> <ol type="a"> <li>Hard Copy Output Devices <ol type="i"> <u><li>Printer</u> <ol type=decimal> <li>Impact Printer <ol type="A"> <li>Character printer <ol type="a">
8

<li>Dot Matrix <li>Daisy Wheel </ol> <li>line Printer <ol type="a"> <li>Drum Printer <li>Chain Printer </ol> </ol> </ol> </ol></ol> <br> <li>Non Impact Printer <ol type="A"> <li>Inkjet Printer <li>Laser Printer <li>Plotter </ol> <br> <br> <li>Memory <br> <ol type="1"> <u><li>Primary Memory</u> <ol type="a"> <li>Cache <li>RAM <ol type="i"> <li>Static RAM <li>Dynamic RAM </ol> <li>ROM <ol type="i"> <li>PROM <li>EPROM
9

<li>EEPROM </ol> </ol> <br> <u><li>Secondary Memory</u> </ol> <ol type="a"> <li>Magnetic Storage <ol type="i"> <li>Magnetic Tape <li>Magnetic Disk </ol> <ol type="a"> <li>Floppy Disk <li>Hard Disk Drive </ol> <li>Optical storage </ol> <ol type="i"> <li>Optical Disk <li>Digital Versatic Disk <li>Blue Ray Disk </ol></body> </html>

10

OUTPUT:

11

/*PROGRAM TO DISPLAY IMAGE WITH AND WITHOUT BORDER*/

<html> <head><title> working with images</title> </head> <body bgcolor="black"> <b> <font color="red">controlling image borders!<b> <center> <i>image without a border</i><br/> <r/> <img src="blue.jpg"><br/><br/> <i>image with border</i><br/> <br/> <img src="blue.jpg" height="50" width="50"> <br/> </center> </body> </html>

12

OUTPUT:

13

/*PROGRAM TO SHOW THE USE OF TABLE TAG*/ <html> <head><title>student.com</title></head> <body><b><center><h1>TABLE</h1></center></b> <center><table border="2"></center> <tr><th colspan="7"><center>student</center></th></tr> <tr><th rowspan="2">rollno</th> <th rowspan="2">name</th> <th colspan="3">marks</th> <th rowspan="2">total marks</th> <th rowspan="2">groups</th> </tr> <tr><th>c++</th> <th>graphics</th> <th>java</th> </tr> <tr><td>101</td> <td>gagan</td> <td>70</td> <td>80</td> <td>50</td> <td>200</td> <td rowspan="2">1</td> </tr> <tr><td>102</td> <td>gurpreet</td> <td>60</td> <td>80</td> <td>50</td> <td>190</td> </tr> <tr><td>103</td>
14

<td>suman</td> <td>60</td> <td>80</td> <td>40</td> <td>180</td> <td rowspan="2">2</td></tr> <tr><td>104</td> <td>harpreet</td> <td>60</td> <td>50</td> <td>50</td> <td>160</td> </tr> </table> </body> </html>

OUTPUT:

15

16

/*PROGRAM TO DISPLAY FRAME IN HTML*/ <html> <head><title>frame.com</title></head> <frameset rows="30%,50%,20%"> <frame src="gg7.HTML"> <frameset cols="50%,50%"> <frame name="f1" src="gg14.html"> <frame name="f2" src="f1"> </frameset> <frame src="gg22.html"> </frameset> </html>

//gg7
<html> <head><title>notesite.in</title></head> <body bgcolor="green"><marquee behaviour="alternate"><h1>welcome to all</h1></marquee></body> </html>

//gg14
<html> <head><title>linking.com</title></head> <body bgcolor="`pink" text="brown"><b><i><h1>SUBJECTS OF BCA FINAL</h1></i></b> <h2><a href="gg15.html" target="f2">computer graphics</h2></a> <h2><a href="gg16.html" target="f2">computer networks</h2></a> <h2><a href="gg17.html" target="f2">html</h2></a> <h2><a href="gg18.html" target="f2">edp</h2></a> <h2><a href="gg19.html" target="f2">maths</h2></a> </body>
17

</html>

//gg15
<html> <head><title>linking</title></head> <body bgcolor="pink"><b><i><h1>syllabus of graphics</h1></i></b> <h2>chapter 1=introduction of computer graphics</h2></br> <h2>chapter 2=video display unit</h2></br> <h2>chapter 3=input devices</h2></br> </body></html>

//gg16
<html> <head><title>network.com</title></head> <body bgcolor="pink"><b><i><h1>syllabus of computer networks</h1></i></b> <h1>chapter 1 =uses of computer network</h1></br> <h1>chapter 2= network hardware</h1></br> <h1>chapter 3=physical layer</h1></br> </body> </html>

//gg17
<html> <head><title>network.com</title></head> <body bgcolor="pink"><b><i><h1>syllabus of computer networks</h1></i></b> <h1>chapter 1 =uses of computer network</h1></br> <h1>chapter 2= network hardware</h1></br> <h1>chapter 3=physical layer</h1></br> </body> </html>

//gg18
<html>
18

<head><title>edp.com</title></head> <body bgcolor="pink"<b><i><h1>syllabus of edp</h1></i></b> <h2>chapter 1=project formulation</h2></br> <h2>chapter 2=reports</h2></br> <h2>chapter 3=ratios </h2></br> </body> </html>

//gg19
<html> <head><title>maths.com</title></head> <body bgcolor="pink"><b><i><h1>syllabus of maths</h1></i></b> <h2>chapter 1=sets</h2></br> <h2>chapter 2=relation</h2></br> <h2>chapter 3=functions</h2></br> </body> </html>

//gg22
<html> <head><title>notesite.in</title></head> <body bgcolor="red"> <b><i><h1>THANKYOU FOR VISITING</b></i></h1></body> </html>

19

OUTPUT:

20

/*PROGRAM TO DISPLAY FORM*/


<html> <head><title>facebook.com</title></head> <body bgcolor="powderblue" > <h1>facebook</h1> <form action="gg7.html"> First name<input type="text" name="f1" size="20" value=" " maxlength=" 15"></br> </br> Last name<input type="text" name="f2" size="20" value=" " maxlength=" 15"></br> </br> Sex</br> male<input type="radio" name="m1" > female<input type="radio" name="m1" ></br> </br> Status</br> married<input type="radio" name="m2" > unmarried<input type="radio" name="m2" ></br> </br> DOB<input type="text" name="d1" value="dd-mm-yyyy" size="20"></br> </br> Email ID<input type="text" name="e1" size="20" value=" " maxlength=" 25" ></br> </br> New Password<input type="password" name="p1" size="20" value=" " maxlength=" 15"></br> </br> Confirm password<input type="password" name="p2" size="20" value=" " maxlength=" 15"></br> </br> <input type="reset" name="r1" value="reset"> </br> <input type="submit" name="r1" value="submit"> </br></form>
21

</body> </html>

OUTPUT:

22

/*PROGRAM TO DISPLAY RESUME*/


<html> <head> <STYLE Type="text/css"> body{margin-top:8%} .M{font-size:120%; color:red; font-weight:bold,italic; fontfamily:elephant; } .Mm{font-size:120%;color:white; font-weight:bold,italic; fontfamily:elephant;} </style> </head> <body bgcolor="black"> <fieldset> <legend><a class="Mm">resume</a></legend> <br> <br> <a class="M"><u> Name:-<br><input type="text" size="40"> <br><u> Address:-<br><input type="text" size="90"> <br><u> Telephone no.:-<br><input type="text" size="90"> <br> objective:<br> <br> <br><u> Qualification <table height="120%" width="100%" border="2%" > <tr> <td><a class="Mm">class</td> <td><a class="Mm">percentage</td> <td><a class="Mm">school/university</td> <td><a class="Mm">year</td>
23

</tr> <tr> <td>&nbsp</td> <td>&nbsp</td> <td>&nbsp</td> <td>&nbsp</td> </tr> <tr> <td>&nbsp</td> <td>&nbsp</td> <td>&nbsp</td> <td>&nbsp</td> </tr> <tr> <td>&nbsp</td> <td>&nbsp</td> <td>&nbsp</td> <td>&nbsp</td> </tr> <tr> <td>&nbsp</td> <td>&nbsp</td> <td>&nbsp</td> <td>&nbsp</td> </tr> </table><br> Strength:</a> </fieldset> </body> </html> OUTPUT;-

24

25

/*IMAGE MAP*/
Image maps are image links in which a single image can have multiple links. For example: <HTML> <BODY <A> <IMG SRC="DOLL.JPG" HEIGHT=200 WIDTH=200 USEMAP="#M1"> </A> <MAP NAME="M1"> <AREA SHAPE="RECTANGLE" COORDS=10, 20, 50, 60 HREF="DOLL.JPG"> <AREA SHAPE="CIRCLE" COORDS=100, 100, 40 HREF="DOLL1.JPG"> <AREA SHAPE="POLYGON" COORDS=50, 50, 90, 60, 70, 80, 50, 70, 30, 50 HREF="DOLL.JPG"> <AREA SHAPE=DEFAULT HREF="DOLL1.JPG" ALT="DEFAULT CASE"> </MAP> </BODY> </HTML>

26

OUTPUT:

27

Program to show special tags

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

DHTML
28

Program to show use of external stylesheet

<html> <body> p1{color:pink} h1{color:red} </body> </html> <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


29

<html> <body> p{color:pink} .abc{Text-decoration:Underline} </body> </html> <html> <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>
30

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

Program to show use of class


<head> <style type=text/css>

31

p{color:red} .abc{color:pink;font-style:italic} .xyz{color:blue} </style> </head> <body> <p> hello <p class=abc> Welcome <p class=xyz> HTML </h1> </body> </html> OUTPUT:

Program to show use of inline stylesheet


<html> <body>
32

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

Program to show use of Span tag


<html> <head> <style type=text/css> p{color:red}
33

.abc{color:pink;font-style:italic} .xyz{color:blue} </style> </head> <body> <p> hello <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 {

34

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

Program to find sum of 2 numbers


class Add1 { public static void main(String args[]) { int a=10,b=20;
35

} OUTPUT:

int c; c=a+b; System.out.println("Sum of a & b" + c); }

36

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:

37

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:

38

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:

39

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

41

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:

42

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:

43

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:

44

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:

45

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:

46

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:

47

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;
48

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:

49

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); } }

50

OUTPUT:

51

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:

52

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:

53

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:

54

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); }
55

} } }

56

OUTPUT:

57

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


58

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 {

59

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[])
60

{ 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[])


61

{ 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[]) {
62

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;
63

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;
64

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"); } } }

65

OUTPUT:

66

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:

67

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:

68

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:

69

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:

70

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);
71

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;
72

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; } } } }

73

OUTPUT:

74

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); } } }

75

OUTPUT:

76

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); } }

77

OUTPUT:

78

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; }
79

} 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()); } } }
80

public String getAddress() { return address; }

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

81

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

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(); } }

83

OUTPUT:

Program to show multilevel inheritance


class Box { private double width;

84

} class Boxweight extends Box


85

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;
86

} 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);

87

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

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)
89

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

90

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)); }
91

} OUTPUT:

92

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); }
93

} } OUTPUT:

94

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
95

{ 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); } }

96

OUTPUT:

97

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");
98

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

Program to access private members


class Box {
99

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; } }
100

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[]) {

101

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;
102

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[]) {
103

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(" "); } }

104

OUTPUT:

Program to show alphabetic order of strings


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

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

106

Program to show Vectors and Arrays


import java.util.*; class Languagevector

107

{ 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]); } } }

108

OUTPUT:

109

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); }
110

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:

111

112

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;
113

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


114

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(); } }

115

OUTPUT:

116

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:

117

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(); } }

118

OUTPUT:

119

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:

120

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


121

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(); } }

122

OUTPUT:

123

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


124

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


125

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); }
126

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
127

{ 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);
128

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:

129

Program to display use of priority in thread


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

130

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[])
131

{ 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"); } }

132

OUTPUT:

133

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" ); } }

134

OUTPUT:

135

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;
136

} } } } 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
137

{ 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");
138

} OUTPUT:

} }

139

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

140

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:

141

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:

142

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); } }

143

OUTPUT:

144

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"); } } }
145

OUTPUT:

146

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); } }

147

OUTPUT:

148

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
149

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

Program to show use of throws


150

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


151

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); } }

152

OUTPUT:

153

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;
154

} OUTPUT:

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

Program to display numerical values with applet


<html>
155

<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); } } `

156

OUTPUT:

157

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

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:

159

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

160

OUTPUT:

161

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) {
162

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:

163

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); } }

164

OUTPUT:

165

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); }
166

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); } }

167

OUTPUT:

168

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); }
169

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); } }

170

OUTPUT:

171

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);
172

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); } }

173

OUTPUT:

174

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);
175

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); } }

176

OUTPUT:

177

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");
178

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); } }

179

OUTPUT:

180

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");
181

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); } }

182

OUTPUT:

183

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);
184

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()); } }

185

OUTPUT:

186

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);
187

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); } }

188

OUTPUT:

189

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();
190

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); } }

191

OUTPUT:

192

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); } }

193

OUTPUT:

Program to draw triangle in an applet


<html>
194

<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>
195

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

Program to show use of control loops in an applet


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

</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) ; } } }

198

OUTPUT:

199

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


200

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); } } }

201

OUTPUT:

202

Vous aimerez peut-être aussi