0% found this document useful (0 votes)
215 views

Java Record

This document contains summaries of 14 Java programs that demonstrate various Java concepts: 1) Programs that demonstrate variable scope and lifetime using classes Scope and Lifetime. 2) A program Conversion that demonstrates type casting between primitive data types. 3) A program Promote that illustrates type promotion rules. 4) A program Twoda that implements a 2D matrix using arrays. 5) A program Threedmatrix that implements a 3D matrix using nested for loops.

Uploaded by

Santhosh Soma
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
215 views

Java Record

This document contains summaries of 14 Java programs that demonstrate various Java concepts: 1) Programs that demonstrate variable scope and lifetime using classes Scope and Lifetime. 2) A program Conversion that demonstrates type casting between primitive data types. 3) A program Promote that illustrates type promotion rules. 4) A program Twoda that implements a 2D matrix using arrays. 5) A program Threedmatrix that implements a 3D matrix using nested for loops.

Uploaded by

Santhosh Soma
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 96

1) A Program to demonstrate the scape and lifetime of a variable

Class scope{ Public static void main(string args[]) { It x=10; If(x==10) { Int y=20; System.out.println(x and y:+x++y); X=y*2; } Y=100; System.out.println(x is :+x); } } class lifetime { Public static void main(String args[]) { Int x;

For(x=0;x<3;x++) { Int y=-1; System.out.println(y is :+y); Y=100; System.out.println(y is now:+y); } } }

OUT PUT:Y is : -1 Y is now : 100 Y is : -1 Y is now : 100 Y is : -1 Y is now : 100

2) A program to demonstrate type casting.

Class conversion { Public static void main(String args[]) { Byte b; Int i=257; Double d=323.142 System.out.println(\n conversion of int to byte); B=(byte)i; System.out.println(i and b +i+ +b); System.out.println(\n conversion of double to int); I=(int )d; System.out.println(d and I+d+ +i); System.out.println(\n conversion of double to byte); B=(byte)d; System.out.println(d end b+d+ +b); } }

OUTPUT:Conversion of int byte I and b 257 1

Conversion ot double to int D and I 323.142 323

Conversion of double to byte D and b 323.142 67

3) A program to implement the type promotion rules


class promote { Public static void main(String args[]) { Byte b=42; Char c =a; Short s=1024; Int =i15000; Float f=5.67f; Double d=0.1234; Double result=(f*b)+(i/c)-(d*s); System.out.println(f*b)+++(i/c)+-+(d*s)); System.out.println(result=+result); } }

OUT PUT:-

4) A program to implement 2d matrix

Class twoda { Public static void main(String args[]) { Int twod[][]=new int[4][]; Twod[0]=new int [1]; Twod[1]=new int [2]; Twod[2]=new int [3]; Twod[3]=new int [4]; Int I,j,k=0; For(i=0;i<4;i++) For(j=0;j<i+1;j++) System.out.println(twod[i][j]+); System.out.println(); } } }

OUT PUT:-

0 1 3 2 4 5 8 9

6 7

5) A program to implement 3d matrix

Class Threedmatrix { Public static void main(String args[]) { Int threed[][][]=new int [3][4][5]; Int I,j,k; For(i=0;i<3;i++) For(j=0;j<4;j++) For(k=0;k<s;k++) Threed [i][j][k]=i*j*k; For(i=0;i<3;i++) { For(j=0;j<4;j++) { For(k=0;k<5;k++) System.out.println(threed[i][j][k]+); System.out.println(); } } }

OUT PUT:-

00000 00000 00000 00000 01234 02468 0 3 6 9 12

00000 02468 0 4 8 12 16 0 6 12 18 24

6) A program to implement bitwise logical operator

Class bitlogic { Public static void main(String args[]) { String binary[]={0000,0001,0010,0011,0100,0101,0110, 1000,1001, 1010, 1011,1011,1100,1110,1111}; Int a=3; Int b=6; Int c=a/b; Int d=a&b; Int e=a^b; Int f(~a&b)/(a&~b); Int g= ~ a & oxog; System.out.println(a=+binary[a]); System.out.println(b=+binary[b]); System.out.println(a/b=++binary[c]); System.out.println((a&b=+binary[d]); System.out.println(a^b=+binary[e]); System.out.println(((~a&b)/(a&~b)=binary[f]); System.out.println(((~a&oxof)=+binary[g]);

10

} }

OUT PUT:-

7) A program to illustrate switch statement

11

Class switch { Public static void main()(String args[]) { Int month =4; String season; Sitch (month) { Case 1: Case2: season=winter; Break; Case 3: Case 4: Case 5: season=summer; Break; Case 9: Case 10: Case 11:season=autumn; Break; Default:season=bojns month; } System.out.println(season is april+season);
12

} }

OUT PUT:-

8) A program to illustrate break statement

13

Class break loop { Public static void main(string args[]) { For(int i=0;i<5;i++) System.out.println(pass+i+:); For(int j=0;j<100;j++) { If(j==10) Break; System.out.println(i+) } System.out.println(); } System.out.println(loops complete); } }

OUT PUT:-

14

9) A program to illustrate continue statement

Class continue
15

{ Public static void main(String args[]) { For(int i=0;i<10;i++) System.out.println(i+); If(i/2==0) Continue; System.out.println(); } }

OUT PUT:-

10. A program to illustrate recursion


Class factorial {

16

Int fact(int n) { Int result; If(n==1) Return 1; Resul=float(n-1)*n; Return result; } } Class recursion { Public static void main(String args[]) { Factorial f=new factorial(); System.out.println(factorial of 3 is + f.facr(3)); System.out.println(factorial of 4 is +f.fact940); System.out.println(factorial of 5 is+f.fact(5)); } }

OUT PUT:-

17

11) A program to illustrate the static keywords

Class usestatic {
18

Static int a=3; Static int b; Static void meth(int x) { System.out.println(x=+x); System.out.println(a=+a); System.out.println(b=+b); } Static { System.out.println(static block initialized); B=a*4; } Public static void main(String args[]); { Meth(42); } }

OUT PUT:-

19

12) A program to illustrate inner classes


Class outer { Int outer-x=100; Void test()

20

{ Inner inner=new inner(); Inner.display(); } Class inner { Void display() { System.out.println(display:outer-x=+outer-x); } } } Class innerdemo { Public static void main(String args[]) { Outer outer=new outer(); Outer.test(); } }

OUT PUT:-

21

13) A Program to demonstrate some string methods class StringDemo2 { public static void main(String args[]) { String str ob1=First string; String str ob2=Second string; String str ob3=st ob1;
22

System.out.println( lenth of str ob1: + str ob1.length()); Sytem.out.pintln(char of index 3 in str ob1:+ str ob1.charAt(3)); if(str ob1.equals(str ob2)) System.out.println(str ob1==str ob2); else System.out.println(str ob1!=str ob2); if(str ob1.euals(str ob3)) System.out.println(str ob1==str 0b3); else System.out.println(str ob1!=str ob3); } }

OUTPUT:Length of str ob1 : 12

CharAt index 3 in str ob1: s Str ob1 !=str ob2 Str ob1==str ob3

23

14) A Program to demonstrate command line argument passing Class Commandline { Public static void main(String args[]) { For(int i=0;i<args.length;i++) System.out.println(args [ +i ]i+args[i]); } }
24

OUTPUT:Args[0] Args[0] Args[0] Args[0] Args[0] Args[0] : this : is : a : test : 100 : -1

15) A Program to illustrate method overriding Class A { Int I,j; A(int a,int b) { J=b; } Display I and j Void show(
25

{ System.out.println(I andj: +I+ +j); } } Class B extends A { Int k; B(int a, intb,int c) { Super(a,b); K=c; } //display k-this overrides show() in A Void show() { System.out.println(k:+k); } } Class overrides { Public static void main(String args[]) { B sub ob= new B(1,2,3); Sub ob.show(); // this calls show() In B }
26

OUTPUT:K=3

16) A Program to illustrate dynamic method dispatch Class A { Void callme() { System.out.println(Inside As callme method); } } Class B extends A { //override callme( {
27

System.out.println(Inside Bs callme method); } } Class c extends A { void callme() //override call me() { System.out.println(Inside cs callme method); } } class Dispath { Public static void main(String args[]) { A a=new A(); B b=new B(); C c=new C(); A r; r=a; r.callme(); r=b; r.callme(); r=c; r.callme(); } //object of type A //object of type B //object of type C //obtain a reference of type A

28

OUTPUT:Inside As callme method Inside Bs callme method Inside Cs callme method

17) A Program to illustrate the usage of abstract class

Abstract class Figure { Double dim1; Double dim2; Figure(double a,double b) { dim1=a; Dim2=b; } abstract double area(); }
29

//area is now an abstract method

Class Rectangle extends Figure { Rectangle(double a, double b) { Super(a,b); } Duble area() // override area for example rectangle

{ system.out.println(inside area for rectangtle); Return dim1*dim2; } } Class Triangle extends figure { Triangle(double a,double b) { Super(a,b); } Double area() { System.out.println(inside Area of Triangle); Return dim1*dim2/2; } } Class Abstract Areas
30

//override area for right triangle

{ pubic static void main(String args[]) { //Figure f=new figure(10,10); Rectangle r=new Rectangle(9,5); Triangle t=new Triangle(10,8); Figure Figure f; Figre f=r; System.out.println(Area is +Figure f.area()); Figure f=t; System.out.println(Area is + Figure f.area()); } } //illegal now

OUTPUT:Inside area of rectangle 45 Inside area for triangle 40

31

18) A Program to illustrate the final keyword

Class A { Final void meth() { System.out.println (This is a final method); } } Class B extends A { Void meth () { System.out.print(illegal); }
32

//ERROR : cant override

} OUTPUT:-

19 A Program to illustrate the access protection in packages Package p 1; Public class protection { int n=1; Private int n-pri=2; Protected in n-pro=3; Public int n-pub=4; Public protection(); { System.out.println(base constructor); System.out.println( n= +n); System.out.println( n-pr:= +n-pro); System.out.println(n-pro= +n-pro);

33

System.out.println(n-pub=+n-pub); } } Class Derived extends protection package1 { Derived() { System.out.println(derived constructor); System.out.println(n= +n); //system.out.println(n-pri= +n-pri) System.out.println(n-pro=+n-pro); System.out.println(n-pub=+n-pub); } } // this is file some package.java; Package p; Class some package { Same p package() { Protection p=new protection(); System.out.println(same package constructor); //class only //this is file derived: java:

34

System.out.println(n=+p.n); //system.out.println(n-pri= +p.n-pri) System.out.println(n-pro=+p.n-pro); System.out.println(n-pub=+p.n-pub); } } Package p2; Class protection 2 extends p1.protection { Protection 2() { System.out.println(derived other package constructor); //class or package only //system.out.println(n= +n); //class only //system.out.println(n-pri= +n-pri) System.out.println(n-pro=+n-pro); System.out.println(n-pub=+n-pub); } } // this is other file package.java Pacage p2; Class other package //class only //class only

35

{ Other package() { P1.protection p=new p1.protection(); System.out.println(other package constructor); //class or package only //system.out.println(n= +p.n); //class only //system.out.println(n-pri= +p.n-pri) System.out.println(n-pro=+p.n-pro); System.out.println(n-pub=+p.n-pub); } } Demo package p1; Package p1; //instantiate the various classes in p1 Public class Demo { Public static void main(String args[]) { Protection ob1=new protection(); Derived ob2=new Derived(); Sample package ob=new sample package();
36

//class only

} } //Demo package p2; Package p2; //instantiate the various classes in p2 Public class Demo { Public static void main(String args[]) { Protection 2 ob1=new protection 2(); Other package ob2=new other package(); } }

OUTPUT:-

37

20) A Program to create fixed stack using interface Class Fixed stack implements Int stack { Private int stck[] Private int tos; //allocate and initialize stack Fixed stack(int size) { Stck=new int[size]; Tos=-1; } //push an item on to the stack Public void push (int item) { If(tos==stck.length-1 System.ot.println(stack is full);
38

Else Stck[++tos]=item; } //pop an item the stack Public int pop() { If(tos,0) { System.out.println(stack undeflow); Return 0; Else return stck[tos--]; } } Class IfTest { Public static vod main(String args[]) { Fixed stack my stack1=new Fixed stack95); Fixed stack mystack2=new Fixed stack98); //push some numbers on to the stack For(int i=0;i<5;i++) Mystack1.push(i); For(int i=0;i<8;i++)
39

Mystack2.push(i); //pop these numbers off the stack System.out.println(stack in my stack1 ); For(int i=0;i<5;i++) System.out.println(my stack.pop()); System.out.println(Stack in my stack 2:); For(int i=0;i<8;i++) System.out.println(my stack2.pop()); } }

40

21) A Program to illustrate multi-threading Class New Thread mplements Runnable { String name; Thread t; New Thread(String Thread name) { Name=thread name; T=new Thread(this,name); System.ot.println(new Thread;+t); t.start(); } //this is the entry point for thread Public void run() { try { For(int i=5,i>0;i--) { //start the thread

41

System.out.println(name+:+i); Thread.sleep(1000); } }

Catch(interrupted exception e) { System.out.println(name + Interrupted ); } } System.out.println(name+Existing ); } } Class multithread Demo { new New Thread(one); new New Thread(Two); new New Thread(Three); try { Thread.slep(10,000); } Catch(interrupted exception e) { System.out.println(main thread interrupted );
42

//wait for other threads to end

} System.out.println(main thread existing); } } New thread : Thread [one.5.main] : Thread [Two.5.main] : Thread [Three.5.main] :5 :5 :5 :4 :4 :4 :3 :3 :3 :2 :2 :2 :1 :1 :1

OUTPUT: -

New thread New thread One Two Three One Two Three One Two Three One Two Three One Two Three One existing Two existing

43

Three existing Main existing 22) A Program to reading characters from the console until the uses type q //impact java.io.*; Class B R Read { Pubic static void main(String args[]) Throws IoException { Char c; Buffered Reader br=new Buffered Reader(new input=stream Reader(system.int)); System.out.println(Enter dharacters, q to quit); //read characters Do { C=(char) br.read(); System.out.println(c); } While(c!=q); } }

44

OUTPUT:Enter characters q to quit 123 abc q 1 2 3 a b c q

45

23) A Program to illustrate PrintWriter class //Demonstrate PrintWriter Import java.io.*; Public class print Writer Demo { Public static void main(String args[]) { Print writer pw=new print writer(System.out.true); Pw.pringln(This is a String); INT I<=7; PW.PRINTLN(I); DOUBLE D=4.5E-7; PW.PRINTLN(D); } } OUTPUT:This is a String -7 4.5E-7

46

24) A Program to input and display the consist of a text file [Name of file is given as commandlin] Class showFile { Public static void main(String args[]) { Throws IoException { Int I; File Input stream fin; Try { Fin=new file input search(args[0]); } Catch(File Not found Exception e) { System.out.println(Usage :show File File); Return: } Do { I=fin.read(); If(i!=-1) System.out.println((char)i);
47

} While(i!=-1); Fin.close(); } } OUTPUT:-

25) A Program to copy a text file into another Import java.io.*;


48

Class CopyFile { Public static void main(String args[]) { Throws IoException { Int I; FileInputstream fin; File OUtputstream fout; Try { Try { Fin=newfile input stream(args[0]); } Catch(File Not found exception e) { System.out.println(input file not found); Return; } Try { Fout=new file output stream(args[]);
49

} Catch(File Not found Exception e) { System.out.println(Error opening output file); Return; } } catch(Array Index out of Bounds Exception e) { System.out.pringln(usage;copy file from To); Retun; } Try { Do { I=fin.read(); If(i!=-1) Fout.write(i); } While(i!=-1); } Catch(Io Exception e)

50

{ System.out.println(ile Error): } Finlclose(); Fout.clolse(); }

OUTPUT:-

26) A Program to demonstrate methods in file class

Import.java.io.File; Class FileDemo


51

{ Static void p(string s) { System.out.println(s); } Pubic static void main(String args[]); { File f1=new file( 1 Java 1 copy RIGHT); P(filename:+f1.getname()); P(path:+f1.getpath()); P(Abspath:+get Absolute path(0); P(parent:+f1.getparrent()); P(f1.Exists()? EXosts: does not Exist); P(f1.canwite()? is writable: if not readable); P(f1.canread()? is readable: is not readable); P(f1.is+ f1..is directory ()? : not + a directory); P(f1.f=is file()? is normal file might be named pipe): P(File last modified : +f1.last modified()); P(File size: +f1.length() + Bytess); } } OUTPUT:File name : copy RIGHT

52

Path

: /java/copy RIGHT

Abs path : /java/copy RIGHT Parent Exists Is writable Is readable Is not a directory Is normall file Is absolute File last modified=8124,6520400 File size : 695 bytes : /java

27) A Program to illustrate Directories Import. Java.io.File; Class Direct list { Public static void main(String args[])
53

{ String dirname=/java; File f1=new file(dir name); If(f1.is Directory()) { System.out.pringln(Directory of +dirname); String s[]=f1.list(); For(int i=0i<s.length;i++) { File f=new file(dirname+/+s[i]); If(f.isDirectory()) { System.out.println(s[i]+ is a directory); } Else { System.out.println(s[i]+is a file); } } } Else { System.out.println(direname + is not a dirname):
54

} } } OUTPUT:Directory of 1 Java Bin is directory Lib is directory Demo is directory Copy RIGHT is a file READ ME is a file Index.html is a file Include is a directory Sc is a directory

28) A Program to implement the program File input Stream class Import.java.io.*; Class File input stream Demo { Public static void main(String ars[]) {

55

Int size; Input stream t=new File Input steam(File input stream Demo.java); System.out.println(Total available Bytes: +size=f.available()); Int n=size/40; System.out.println(Fist +n+bytes of the file one read() at a time); For(int i+);i<n;i++) { system.out.println(char)Fread()); } System.out.println(/n still available:f.available()): System.out.println(Reading the next +n +with one read b[]); Byte b[]=new byte[n]; If(f.read(b)!=n { System.out.println(couldnt read +n+bytes); } System.out.println(new string(b,o,n)); System.out.println(/nstill available: +size=f.available())); System.out.pringln(shipping half of remaining bytes with ship()); f.ship(size/2); System.out.println(still available:+available(0); System.out.println(Reading+n/2+in to the end of array);

56

If(f.read(b,n/2,n/2)!=n/2) { System.out.println(couldnt read+n/2+bytes); } System.out.println(new String(b,o,b.length)}; System.out.println(/ns tillavailable:+f.available()); f.close(); } }

OUTPUT:-

29) A Program to implement simple Banner Applet Import Java.awt.*; Import Java.applet.*; Public class simple Banner Extends Applet implements Runnable { String msg=A simple moving Banner;
57

Thread t=null; Int state; Boolean stapflay; Public void int() { Set back ground(color.cyan)); Set Back ground (color.red): } Public void stack() { T=new Thread(this); Stopflag=false; t.start(; } Public void run() { Char ch; For( ; ;) { Try { Repaint(); Thread.sleep(250);
58

Ch=msg.char At(0); Msg=msg.sub string(1,msg.length(1)): Msg+=ch; or msg=msg+ch; If(stop flag) Breadk; } Catch (Interrupted Exception e) { } } } Public void stop() { Stop flag=true; T=null; } public void paint(Graphics g) { g.draw string(msg,50,30); } }

OUTPUT:-

59

30) A PROGRAME TO PASS PARA METERES TO APPLETS.


import java.awt.*; import java.applet.*; public class para demo extends applet{ String font Name ; int font size; float leading;

60

boolen active; public void start(){ String param; front name=get parameter (font Name); if (Font Name == Not found; Font Name = Not found ; Param = get parameter (Font size); try{ if(param!=NULL) font size = integer.parse int (param); else font size = 0; } catch ( number format exception e) { font size = -1; } param = get parameter (leading); try{ if (param != NULL) Leading = float . value of (param).float value(); else leading=0;
61

} catch (Number format exception e){ Leading=-1; } Param=get parameter (account enabled); If (param!=NULL) Active= Boolean.value of(param). Boolean balue(); } Public void point (graphics g) { g.drawstring (Font Name:+font name,0,10); g.drawstring(Font Size: + Font size,0,26); g.drawstring(leading: + leading,0 ,42); g.drawstring(account active: + Active,0,58); } }

OUT PUT:-

62

31) A Program to illustrate the concepts class with constructors method and overloading

Class Box { Double width,height, depth; Box (double w,double h, double d) { Width=w; Height=h;
63

Depth=d; } Box() Width=-1; Height=-1; Depth=-1; } Box(double len) { Width=height = depth=len; } Double volume() { Return width*height*depth; } } Class overload cons { Public static void main(string org s[]) { Box mybox1=new box(10,20,15);
64

Box mybox2=newbox(); Box mycube=new box(7); Double volume; Vol=mybox1.volume(); System.out.println(volume of my box1 is + vol); Vol=mybox2.volume(); System.out.prinln(volume of myboxa2 is + vol); Vol=mycube.volume(); System.out.println(volume of mycube is + vol); } }

OUT PUT:-

65

32) A Program to illustrate the exception handling keyword try and catch, throw, throws, finally.

i) class excep
{ Public static void main(string arg s[]) { Int d,a; Try{ d=0; a=42/d; system.out.println(this will not be printed); }

66

Catch (Arithmetic exception e) { System.out.println(Division by zero); } System.out.println(After catch statement); } }


OUT PUT:-

67

(ii) //Using Throw


Class throw Demo { Static void demopro c() { Try{ Throw new NULL pointer exception(demo);} Catch(Null pointer exception e) { System.out.println(caught inside demopro c); Throw e;

68

} } Public static void main (string arg s[]) { Try { Demo pro c (); } Catch (Null pointer exception e) { System.out.println(Re cought: + e); } }

OUT PUT:-

69

(iii) //using throws


Class throws demo { Static void throw one() throws Illegal access exception { System.out.println(Inside throw one); Throw new Illegal Access Exception(demo); } Public static void main (string arg s[]) { Try{ Throw one(); }
70

Catch(Illegal Access Exception e) { System.out.println(caught + e); } } }


OUT PUT:-

71

(iv) //Using Finally


Class finally demo { Static void pro A() { Try{ System.out.println(Inside proc A); Throw new Runtime exception(demo); } Finally{ System.out.println(pro As Finally); } } Static void proc B() {
72

Try{ System.out.println(Inside proc B); Return; } Finally{ System.out.println(proc Bs finally); } } Static void proc(){ Try{ System.out.println(proc Cs is inside); } Finally{ System.out.println(proc is finally); } } Public static void main(String arg s[]) { Try{
73

Proc A(); } Catch (Exception e) { System.out.println(exception caught); } Proc B(); Proc C(); } }
OUT PUT:-

74

33) A Programme to illustrate the concept of multilevel Inheritance using super


Class Box { 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;

75

Depth=-1; } Box(double len) { Width=height=depth=len; } Double volume(){ Return width *height*depth; } } Class Box weight extends Box { Double weight; Box weight(Box weight ob){ Super(ob); Weight=ob.weight; } Box weight(double w, double h, double d,double m) { Super(w,h,d); Weight(){ Super(); Weight=-1;
76

} Box weight(double len,double m) { Super (len); Weight=m; } } Class shipment extends box weight { Double cost; Shipment(shipment ob) { Super9ob); Cost=ob.cost; } Shipment(double w, double h, double d, double m, double()) Super(w,h,d,m); Cost=c; } {

Class demoshipment {
77

Public static void main(string args[]) { Shipment shipment1=new shipment(10,20,15,10,3.41); Shipment shipment2= new shipment(2,3,4,0.76,1.28); Double vol; Vol shipment1.volume(); System.out.println(volume of shipment is + vol); System.out.println(height of shipment is + shipment1.weight); System.out.println(shipping cost: + shipments1.cost); System.out.println(); Vol=shipment2.volume(); System.out.println(volume of shipping2 is + vol); System.out.println(weight of shipping2 is + shipment2.weight); System.out.println(shipment cost: +shipment2.cost); } }

OUT PUT:-

78

34) Write a Program to implement Applet


Import java.awt.*; Import java.applet.*;

79

/* <applet code=sample width=300 height=50> <\applet> */ Public class sample extends applet { String msg; Public void int() { Set background(color.cryan); Set foreground(color.red); Msg=inside in it().; } Public void start() { Msg+=inside start ().; } Public void paint(graphics g){ Msg+=inside point(); g.draw string(msg.10,30); } }

OUT PUT:-

80

35) Create an applet to draw a line, rectangle, ellipse and circle a) Drawing line

81

import java.awt.*; import java. applet.*; /* <applet code=lines width=300 height=200></applet>*/ public class lines Extends Applet { public void paint(graphics g) { g.drawline (0, 0,100,100); g.drawline (0, 100, 100, 0); g.drawline (40, 25,250,180); g.drawline (75, 90,400,400); g.drawline (20, 150, 400, 40); g.drawline (5, 290,80,19); } }

b) Rectangles

import java.awt.*; import java. applet.*; /* <applet code=rectangle width=300 height=200></applet> */


82

public void Rectangle Extends Applet {public void paint (Graphic g){ g.draw Rect(10,10,60,50); g.fill Rect(100,10,60,50); g.draw Round Rect(190,10,60,50,15,15); g.fill Round Rect(70,90,140,100,30,40); } }

C) Ellipses and circles.


import java.awt.*; import java. applet.*; /* <applet code=Ellipse width=300 height=200></applet>*/ public class ellipses Extends Applets{ public void paint (Graphic g) { g.draw oral(10,10,50,50); g.fill over(100,10,75,50); g.draw over(190,10,75,30); g.fill over(70,90,140,100); } }

83

OUT PUT:-

84

36) Handling mouse events.


import java.awt.*; import java.awt.event.*; import java.applet.*; /* <applet code=mouse events width=300 height=200></applet> */ public class mouse events extends Applet implements mouse listener mouse motion listener { string msg= ; int mouse x=0;mouse y=0; public void int() { add mouse listener(this); add mouse maintain listener(this); } public void mouse clicked (mouse event me) { mouse x=0;

85

mouse y=10; msg =mouse clicked; repaint (); } public void mouse entered(mouse enter event me) { mouse x=0; mouse y=10; msg =mouse extend; mepaint (); }public void mouse exited(mouse event me){ mouse x=0; mouse y=10; msg =mouse exited; repaint (); } public void mouse pressed(mouse event me){ mouse x=me.getx(); mouse y=me.gety(); msg =down; repaint(); } public void mouse released(mouse event me){
86

mouse x=me.getx(); mouse y=me.gety(); msg=up; repaint (); } public void mouse dragged (mouse event me){ mouse x=me.getx(); mouse y=me.gety(); msg=*; show status (dragging mouse at +mouse x+ +mouse y+); repaint (); } public void mouse(mouse event me){ show status (moving mouse at+me.getx()+ +me.gety()); } public void paint (Graphic g){ g.draw string(msg.mouse x.mouse y); } }

87

OUT PUT:-

88

37) Handling keyboard events.


Import java.awt.*; Import java.awt.event.*; Import java.applet.*; /* <applet code=sample key width=300 height=200></applet> */ Public class simple key extends Applet Implements key listener { String msg= ; Int x=10,=20; Public void int(); Add key listener(this); Request focus(); } Public void key pressed (key event k){ Show status(key down); {

89

Public void key released (key event k){ Show status (key up); } Public void key typed(key event ke){ Msg+=ke.get key char(); Repaint (); } Public void paint(Graphic g) { g.draw string(msg.x,y); } }

OUT PUT:-

90

38) Adapter classes.


Import java.awt.*; Import java.awt.event.*; Import java. applet.*; /* <applet code=Adapter Demo width=300 height=200></applet> */ Public class Adapter Demo Extends Applets{ Public void int (){ Add mouse listener (new mymouse Adapter (this)); Add mouse motion listener (new my mouse motion Adapter(this)); }} Class mymouse Adapter Extends mouse adapter{ Adapter.Demo adapter demo; Public my mouse adapter(Adapter Demo adapter Demo) { This.adapter Demo =adapter Demo; } Public void mouse clicked(mouse event me){

91

Adapter Demo.show status(mouse clicked); } } Class my mouse motion Adapter extends mouse motion Adapter{ Adapter Demo adapter Demo; Public my motion Adapter (Adapter Demo){ } Public void mouse Dragged(mouse event me){ Adapter Demo show status(mouse dragged); } }

OUT PUT:-

92

39) Create a frame window in an Applet.


import java.awt.*; import java.awt.event.*; import java. applet.*; /* <applet code=Applet frame width=300 height=200></applet> */ class sample frame extends frame { sample frame(String title){ super(title); my window Adapter adapter new my window Adapter(this) add window listener(adapter); } public void paint (Graphic g){ g.draw string(this is an frame window 10,40); } } class my window Adapter extends window Adapter { sample frame .set visible(False);

93

} } public class applet frame extends Applet { frame f; public void init() { f=new sample frame(a frame window); f.setsize(250,250); f.set visible(true); } public void start() { f.set visible(true); } public void stop() { f.set visible(false); } public void paint(Graphic g) { g.draw string (this is in applet window 10,20); } }

94

OUT PUT:-

95

96

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy