You are on page 1of 3

Ki

ngKhali
dUni
versi
t
y
Col
legeofScienceandArtsi
nTanomah
Compute
rSci
enceDe pa
rtme
nt

LabEv
aluat
ion#1
AY1444-1445–Ter m 3– Obt
ainedMar
k:
t
h
May8 ,2023–Dur ati
on60mi nut
es.
223CCS3–Adv ancesObject
-Ori
entedPr
ogr
ammi
ng …………….
./5

Quest
ion1(
2pt
s)
Fort
hef
oll
owi
ngJav
acode,
prov
idet
hegener
atedout
put
:(ormanypossi
f bleout
put
s,chooseone)

cl
assmy ThreadextendsThr
ead{
pr
ivat
eintcount;
pr
ivat
eStri
ngt hr
eadName;

publ
icmy Thr
ead(
intcount
er,
Str
ingname)
{
t
his.
count=counter
;
t
his.
thr
eadName=name;
}

publi
cv oi
drun(
){
System.out
.pr
int
ln(
thi
s.t
hreadName+"star
ted.
");
System.out
.pr
int
ln(
thi
s.t
hreadName+"hascounter"
+count
); Al
lthr
eadsst
art
ed.
System.out
.pr
int
ln(
thi
s.t
hreadName+"ended."
);
} T1st
art
ed.
}
T1hascount
er5T1ended.

Al
lthr
eadsended

publi
ccl assTest{
publicst ati
cvoidmain(Stri
ng[]ar
gs){
my Threadt 1=newmy Thread(5,"T1")
;
my Threadt 2=newmy Thread(10,"T2")
;
t1.
start();
System. out.
pri
ntl
n("
Allthreadsstart
ed."
);
t1.
join();
System. out.
pri
ntl
n("
Allthreadsended");
}
}

Quest
ion2(
3pt
s)
Wr
it
eaThr
ead-
basedcl
assnamedmy
Thr
ead.Thi
sthr
eadwi
l
lpr
intont
hescr
een“
Hel
lo”wheni
tst
art
s,t
heni
t
223CCS3–Adv
ancedObj
ect
-Or
ient
edPr
ogr
ammi
ng LabEv
al#1–AY1444/ m 3–May8th2023
1445–Ter Page:1/
1
wai
tsf
or20seconds,
andf
inal
l
yitpr
int
s“Thr
eadDone”ont
hescr
een.(
try
-cat
chbl
ocki
sopt
ional
).

publicclassmy ThreadextendsThread{
@Ov erri
de
publi
cv oidrun(){
System.out .
print
ln("
Hello"
);
tr
y{
Thread.sleep(20000);//20seconds
}catch(InterruptedExcepti
one){
e.
printStackTrace();
}
System.out .
print
ln("
ThreadDone");
}
}

Quest
ion3( 3pt s)
Wri
teaclassnamedmy Testwithamai nmet hod.Themai
nmet
hod
1. createsfourt hr
eadsf rom my Threadclass,
2. start
st hefourt hreads,
3. andthenwai tfort hefourt
hreadst ofi
nish,
4. fi
nall
y ,
pr i
nts“Allisdone”ont hescr ee

publi
cclassmy Test {
publi
cstati
cv oidmain(St
ri
ng[
]args){
myThread[]threads=newmy Thread[
4];

/
/Cr eatef
ourthreadsfrom my Thr
eadcl
ass
f
or(inti=0;i<threads.
length;i
++){
t
hreads[
i]=newmy Thread()
;
}

/
/St ar
tthef ourthreads
f
or(my Threadt hr
ead:threads){
t
hread.start
();
}

/
/Wai tforthefourthreadstofini
sh
f
or( my Threadthread:threads){
tr
y{
thread.j
oin()
;
}catch( I
nterr
uptedExceptione){
e.pri
ntStackTrace()
;
}
}

/
/Pr
int"
Alli
sdone"ont
hescr
een

Fi
nal
ly{
Syst
em.
out
.pr
int
ln(
"Al
lisdone"
);
}

}
}

223CCS3–Adv
ancedObj
ect
-Or
ient
edPr
ogr
ammi
ng LabEv
al#1–AY1444/ m 3–May8th2023
1445–Ter Page:2/
2
Endoft
heexam sheet
‫ﻴﻖ‬‫ﻓ‬
‫ﻮ‬‫ﺘ‬‫ﻟ‬
‫ﺎ‬‫ﺑ‬

223CCS3–Adv
ancedObj
ect
-Or
ient
edPr
ogr
ammi
ng LabEv
al#1–AY1444/ m 3–May8th2023
1445–Ter Page:3/
3

You might also like