Java Record
Java Record
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;
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); } }
OUT PUT:-
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
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 02468 0 4 8 12 16 0 6 12 18 24
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:-
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:-
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
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:-
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
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
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); } }
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
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
Abstract class Figure { Double dim1; Double dim2; Figure(double a,double b) { dim1=a; Dim2=b; } abstract double area(); }
29
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
{ 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
31
Class A { Final void meth() { System.out.println (This is a final method); } } Class B extends A { Void meth () { System.out.print(illegal); }
32
} 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
} 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
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
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
OUTPUT:-
{ 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
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
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
67
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
71
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
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
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
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); } }
83
OUT PUT:-
84
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
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
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
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