Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

Depar

tmentofComput
erSci
enceandEngi
neer
ing

CS8392-
Obj
ectOr
ient
edPr
ogr
ammi
ng

Uni
t-
wiseExampl
ePr
ogr
am’
s

UNI
T-1

1)Si
mpl
ejav
apr
ogr
am

i
mpor
tjav
a.i
o.*
;

cl
assFi
rst
Demo

publ
i
cinta=10;

publ
i
cst
ati
cvoi
dmai
n(St
ri
ngar
gs[
])

Sy
stem.
out
.pr
int
ln(
“Wel
come”
);

2)Jav
aPr
ogr
am f
orEncapsul
ati
on,
Abst
ract
ion,
Singl
eInher
it
ance:

abstractclassVehi cle//
AbstractClass
{  
  
 
   
abstractintget Speed()

  
//Abstr
actMet hod
}
classAbst EncapnDemoext endsVehi cle //
Singl
e
Inheri
tance
{
pri
vatei ntssn;
publi
ci ntgetEmpSSN( )
{
returnssn;
}
publi
cv oidset EmpSSN( i
ntnewVal ue)
{
ssn=newVal ue;
}
i
ntget Speed( )//
Abstractmet hodImplement
ati
on
 
   {
 
  
  
  
  ret
urn60;
 
   } 
}
publi
cclassEncapsTest
{
publ
icstat
icvoidmai n(
Str
ingargs[])
{
AbstEncapnDemoab=newAbst EncapnDemo(
);
ab.setEmpSSN( 100);
System.out.
pri
ntln(
ab.getEmpSSN( ))
;
System.out.
pri
ntln(
ab.getSpeed()
);
}
}

3)Jav
aPr
ogr
am wi
thConst
ruct
or’
s(or
)Const
ruct
orOv
erl
oadi
ng

Ty
pesofConst
ruct
ors:

i
) Def
aul
tConst
ruct
or

i
i
) Par
amet
eri
zedConst
ruct
ors

i
i
i) CopyConst
ruct
ors

cl
assDemo
{
intval
ue1;
intval
ue2;
Demo() //Def
aul
tConst
ruct
or

{
v
alue1=10;
v
alue2=20;
}
Demo( i
nta,i
ntb) / /Parameter
izedConst
ruct
or
{
value1=a;
value2=b;
}
Demo( Demo s) / /CopyConstruct
or

 
  value1 
= s.
val
ue1; 
 
 
  
 v alue2 
=s.val
ue2; 
 

publi
cv oi
ddispl
ay(
)
{

Sy
stem.
out
.pr
int
ln(
“Val
ue1”
+val
ue1)
;
Sy
stem.
out
.pr
int
ln(
“Val
ue2”
+val
ue2)
;
}
publi
cstat
icvoi
dmai
n(Stri
ngar gs[
])
{
Demod1=newDemo( );
d1.
displ
ay(
);
Demod2=newDemo( 100,200);
d2.
displ
ay(
);
Demod3=newDemo( d2);

}
}

4)Met
hodOv
erl
oadi
ng(
or)Funct
ionOv
erl
oadi
ng

Di
ff
erentway
stoov
erl
oadt
hemet
hod

1.Bychangi
ngnumberofar
gument
s
2.Bychangi
ngthedat
atype

class Adder

 
static 
int 
add( i
nt 
a,int 
b)
{
r
eturn a+b;

 
stati
c fl
oat add( f
loat 
a,fl
oat  
b) / /
Changi ngdat
aty
pe
{
r
eturn a+b;

 
static 
int 
add( i
nt 
a,int 
b,i
nt c)/
/ Changi
ngnumberofar gument
s
{
r
eturn a+b+c;

 
}  
cl
ass  TestOv er
loading1

 
publ i
c stati
c void main(Str
ing[] 
args)

 
Sy stem.out.pri
ntln(Adder.add(11,11))
;  
System. out.
pr i
ntl
n(Adder .
add( 20.5,
11.0))

 
Sy stem.out.pri
ntln(Adder.add(11,11,
11) )

 
}

5)St
ati
cMember
s,met
hodsandBl
ocks
Jav
aSt
aticBl
ockSy nt
ax:
classTest
{
stat
ic{
/
/Codegoesher
e
}
}

Exampl
e:
publi
cclassDemo
{
st
aticinta;
st
aticintb;
st
atic
{
a=10;
b=20;
}
publi
cst ati
cvoidmain(
Str
ingar
gs[
])
{
System.out.
pri
ntl
n("
Val
ueofa="+a) ;
System.out.
pri
ntl
n("
Val
ueofb="+b) ;
}
}
UNI
T-I
I

1.Si
ngl
eInher
it
ancewi
thsuper
()key
wor
d.

cl
ass Animal

 
Str
ing 
color="whit
e";
  
Animal()
{
Syst
em. out.
pri
ntl
n(“Animalcl
ass”
);
}
voi
d pri
ntColor(
)

 
Syst
em. out.
pri
ntl
n(color)
;

 
}

cl
ass 
Dog 
ext
ends 
Ani
mal /
/Si
ngl
eInher
it
ance
{
  
Str
ing 
col
or="bl
ack";
 
Dog()
{
super(
); //I
nvokesbasecl
assconst
ruct
ors
System.out.
pri
ntl
n(“Dogclass”
);

voi
d pr
int
Color(
)

 
super.
print
Color
(); //I
nvokesbasecl
assmethod
System.out.
pri
ntl
n(super
.color
);/ /I
nvokesbasecl
assvar
iabl
es

 
}
  

2.Abst
ractcl
assesandAbst
ractmet
hods
abstr
actclassVehi cle / /
Abst r
actClass

  
 
 
  
 abstr
actintget Speed( )

  
//AbstractMethod
}
cl
assAbst EncapnDemoext endsVehi cl
e //Singl
eInher
it
ance
{
priv
atei ntssn;
publicintget EmpSSN( )
{
retur
nssn;
}
publicvoi dsetEmpSSN( i
ntnewVal ue)
{
ssn=newVal ue;
}
intgetSpeed( ) / /
Abst r
actmet hodImpl
ement ati
on
 
   {
 
  
  
  
  retur
n60;
 
   } 
}
publi
cclassEncapsTest
{
publi
cstati
cv oi dmai n(
Stri
ngar gs[]
)
{
AbstEncapnDemoab=newAbst EncapnDemo( )
;
ab.setEmpSSN( 100);
System.out.pri
ntln(
ab.getEmpSSN());
System.out.pri
ntln(
ab.get
Speed()
);
}
}

3.f
inal
met
hodandf
inal
class
f
inal
class Animal //
final
class
{
  
Stri
ng col
or="
whit
e";
  
Animal()
{
Syst
em.out.
pri
ntl
n(“
Animalclass”
);
}
f
inal
void 
pri
ntCol
or(
) /
/fi
nal
met
hod
{
  
Syst
em.out.
pri
ntl
n(col
or)
;
}
  
}

cl
assDogext
endsAni
mal /
/Thr
owsEr
ror
.Becausef
inal
classcannoti
nher
it

{
}

4.Exampl
eforint
erf
ace,ext
endi
ngi
nter
face,
impl
ement
ingi
nter
face
andmult
ipl
einher
it
ance.
i
nter
facevehicle1 //Inter
face
{
i
ntspeed=90;
publ
i
cv oiddi stance();
}
i
nter
facevehicle2 extends v ehicl
e1 //ExtendingI nt
erf
ace
{
i
ntli
mi t=100;
}
i
nter
facevehicle3 //Inter
face
{
i
ntdistance=50;
publ
i
cv oidspeed( );
}
cl
assvehi
clei mplement svehicle2, vehi
cle3/ /implementingi nt
erf
ace&
//multi
pleinher
itance
{
publ
i
cv oiddi stance()
{
intdi stance=speed* 100;
Sy st
em. out.
pri
ntln("distancetravel
ledis"+distance);
}
publ
i
cv oidspeed( )
{
intspeed=di stance/ 100;
}

5.Jav
apr
ogr
am t
osor
tint
egerusi
ngAr
ray
Listcl
ass&Ar
ray
Listmet
hods

i
mportj
ava.uti
l
.*;
cl
assArr
ayList
Demo
{
 
  
  publ
icstaticvoi
dmai n(Str
ingar
gs[
])
 
  
  {
ArrayLi
st<I
nteger>al=newArray
List
<Int
eger>(
);
al.
add(10);
al.
add(8);
al
.add(50);
al
.add(30);
System.out.
pri
ntl
n(al
.size(
));
Coll
ections.
sor
t(al
);
System.out.
pri
ntl
n(al
.remov e(
10)
);
System.out.
pri
ntl
n(al
);
System.out.
pri
ntl
n(al
.clear
());
System.out.
pri
ntl
n(al
);
}
}

UNIT-
II
I
1.Except
ionhandl
i
ngi
njav
ausi
ngt
ry,
catch,
final
l
y&Mul
ti
plecat
chst
atement
s

cl
assTryCatchExample1 
{
publ
ic st
ati
c v
oid mai
n(St
ri
ng[

args)
 

t
ry 
 

{
  
i
ntz[]
={100}
;
z[
3]=200;
i
nt 
data=50/
0;  /
/may
 thr
ow 
except
ion 
  

 
catch(
Ari
thmeticExcept
ion 
e)  
  /
/handli
ng 
the 
except
ion 
 

 
Sy
stem.out.pr
int
ln(
e); 
 

catch(
Array
IndexOutOfBoundExceptionse)
{
Sy
stem.out.pr
int
ln(
e);

fi
nall
y /
/final
l
yblock
{
Sy
stem.out.pr
int
ln(
"rest 
of 
the 
code")

 

 
}
  

2.Userdef
inedExcept
ioncl
ass

cl
ass 
Inv
ali
dAgeExcept
ion 
ext
ends 
Except
ion /
/userdef
inedexcept
ioncl
ass

{
  

 
Inv
ali
dAgeExcept
ion(
Str
ing 
s){
  

 
 super
(s)

 
 

 

}
  

3.Nest
edt
ryst
atement
s

cl
assTr
yCatchExample1 
{
publ
i
c st
atic 
voi
d main(
Str
ing[

args)
 

t
ry 
 


 
try //Nestedtr
ystatement
s
{
intz[]={100};
z[3]=200;
int data=50/0; 

 catch(ArrayIndexOutOfBoundExcept
ionse)
{
System.out.pr
intl
n(e)
;

}
catch(Arithmet i
cExcepti
on e) 
 

 
Sy stem.out .
pri
ntl
n(e);
  

}

4.Copy
ingcont
ent
sfr
om onef
il
etoanot
herf
il
eusi
ngI
nput/Out
putst
ream

i
mpor tjava.io.*;
cl
assReadFi l
e
{
 
  
 publi
cst at i
cvoidmai n(Str
ing[
]args)thr
owsI
OExcept
ion
 
  
 {
 
  
  
  
 i
ntch;
 
  
  
  
 Fi
leReaderf r
=null;
Fi
leWr i
t erfw=null;
 
  
  
  
 tr
y
 
  
  
  
 {
 
  
  
  
  
  
 fr=newFi leReader("i
nput
.txt
");
fw=newFi l
eWr i
ter(
"output
.t
xt")
 
  
  
  
 }
 
  
  
  
 catch( Fil
eNot FoundExcepti
onf e)
 
  
  
  
 {
 
  
  
  
  
  
 System. out.pri
ntl
n("Fi
lenotfound")
;
 
  
  
  
 }
 
  
  
  
  
while((
ch=fr.
read(
))!
=-1)
 
  
  
  
  
  
 f
w.writ
e(ch))
;
 
  
  
  
  
fr
.cl
ose();
fw.
close();
 
  
 }
}

UNI
T-I
V

1.Exampl
epr
ogr
am f
or

a.Thr
ead

b.Mul
ri
Thr
ead

c. I
nter
-t
hreadcommuni
cat
ion

d. Sy
nchr
oni
zat
ionbl
ockandSy
nchr
oni
zat
ionmet
hod.

e.Consumerpr
oducerpr
obl
em usi
ngThr
ead

i
mpor
tjav
a.i
o.*
;

i
mpor
tjav
a.ut
il
.*
;

cl
assThr
eadExampl
eex
tendsThr
ead /
/Ext
endi
ngThr
eadcl
ass

publ
i
cst
ati
cvoi
dmai
n(St
ri
ngar
gs[
])t
hrowsI
nter
rupt
edExcept
ion

Pr
odConpc=newPr
odCon(
);

Thr
eadt
1=newThr
ead(
newRunnabl
e(){
voi
drun(
){pc.
prod(
);
}}
);

Thr
eadt
2=newThr
ead(
newRunnabl
e(){
voi
drun(
){pc.
con(
);
}}
);

t
1.st
art
();

t
2.st
art
(); /
/Mul
tiThr
ead

t
1.j
oin(
);
t
2.j
oin(
);

cl
assPr
odCon

Ar
ray
List
<Int
eger
>al
=newAr
ray
List
<Int
eger
>()
;

i
ntcapaci
ty=10;

v
oidpr
od(
)

i
ntv
al=0;

whi
l
e(t
rue)

sy
nchr
oni
zed(
thi
s) /
/Sy
nchr
oni
zedBl
ock

whi
l
e(al
.si
ze(
)==capaci
ty)

wai
t(
);

Sy
stem.
out
.pr
int
ln(
“Pr
oducerPr
oduct
”+v
al)
;

al
.add(
val
++)
;

not
if
y()
; /
/Int
erThr
eadCommuni
cat
ion

Thr
ead.
sleep(
100)
;

sy
nchr
oni
zedv
oidcon(
) /
/Sy
nchr
oni
zedMet
hod

whi
l
e(t
rue)

whi
l
e(al
.si
ze(
)==0)

wai
t(
);

i
ntv
ol=al
.r
emov
e()
;
Sy
stem.
out
.pr
int
ln(
“Consumerconsumed”
+vol
);

not
if
yAl
l
();

Thr
ead.
sleep(
100)
;

2.Gener
iccl
assandGener
icMet
hod

cl
assPair<T,U> //
Gener
iccl
ass
{
 
  
 Tfi
rst;
 
  
 Usecond;
 
 publi
cPair(Tfi
rst,Usecond) /
/Gener
icMet
hod
{
  
  
  
  
thi
s.fi
rst=fir
st;
  
  
  
  
thi
s.second=second;
}
 publi
cTget Fir
st(
)
{
  
  
  
  
retur
nf i
rst
;
}
 
 publi
cUget Second()
{
  
  
  
  
retur
nsecond;
 
  
  }
}

3.Way
sofcr
eat
ingThr
eadcl
ass

i
. extendi
ngThr eadcl ass
class 
Multi
 extends  Thr ead

 
publ
ic void run()

 
Sy stem. out.pri
ntl
n("thread i
s r
unni
ng.
..
")

 

 
publ
ic static void main(Str
ing args[]
)

 
Mul ti 
t1=new  Mul t
i(
);  
t1.start();
  

 

i
i.I
mpl ementi
ngr unnabl einterf
ace
class 
Multi
3 impl ement s r
unnable

 
public void r
un( )

 
System. out .
printl
n("
thread is 
running..
."
);
  

 
public stati
c void 
mai n(Str
ing args[]
)

 
Mul ti3 m1=new  Mul ti
3();
  
Thr ead t1 
=new  Thread( m1);  
t1.start()

 

 
}
 

UNI
T-V

1. Wor
kingwi
th2Dshapesusi
ngAWT

i
mpor
tjav
a.awt.
Color;
i
mpor
tjav
a.awt.
Graphics;
i
mpor
tjav
a.awt.
Graphics2D;
i
mpor
tjav
ax.swi
ng.JFrame;
i
mpor
tjav
ax.swi
ng.JPanel;

publ
i
cclassPanel extendsJPanel
{
publi
cv oidpai ntComponent (Gr aphicsg)
{
super.paintComponent (g) ;
Graphics2Dg2d=( Gr aphi cs2D)g;
g2d.setColor (newCol or(31, 21,1))
;
g2d.fi
ll
Rect (250, 195, 90, 60) ;
}
publi
cst aticvoi dmai n(Stri
ng[ ]args)
{
Panel rects=newPanel ();
JFramef r
ame=newJFr ame( "Rectangl
es")
;
fr
ame. setDef aultCloseOper ati
on(JFrame.EXIT_
ON_
CLOSE)
;
fr
ame. add( rects);
fr
ame. setSize( 360, 300);
fr
ame. setLocat i
onRel ati
v eTo( null
);
fr
ame. setVi sibl
e(true);
}
}

2. Swi
ngexampl
epr
ogr
am usi
ngal
lcomponent
s
i
mportj
avax.swing.*;
i
mportj
ava.awt.event.
*;
cl
assSwingDemoext endsJFrameimpl
ementsActi
onList
ener
{
SwingDemo( )
{
JTextFi
eldj
tf=newJText
Fiel
d(20)
;

JCheckBoxj
cb1=newJCheckBox(
"yes"
);
add(
jcb1);
JCheckBoxjcb2=newJCheckBox(
"no"
);
add(
jcb2);
JCheckBoxjcb3=newJCheckBox(
"maybe"
);
add(
jcb3);

JRadioBut
tonj
cb1=newJRadi
oBut
ton(
"C")
;
add(j
cb1);
JRadioBut
tonj
cb2=newJRadi
oBut
ton(
"CPP")
;
add(j
cb2);
JRadioBut
tonj
cb3=newJRadi
oBut
ton(
"JAVA")
;
add(j
cb3);

JBut
tonbt1=newJBut
ton(
"Yes"
);
add(
bt1)
;
JBut
tonbt2=newJBut
ton(
"No")
;
add(
bt2)
;

bt
1.addAct
ionLi
stener
(newSwi
ngDemo(
));

set
Default
CloseOperat
ion(
JFrame.
EXI
T_ON_
CLOSE)
;
set
Layout(
newFl owLayout
())
;
set
Size(
400,400);
}
publi
cvoidacti
onPerf
ormed(Acti
onEvente)
{
stat
usLabel.
set
Text("
OkButtonCli
cked."
);
}
publi
cstati
cvoidmain(St
ri
ng[]ar
gs)
{
newSwi ngDemo();
}
}

You might also like