You are on page 1of 4

Ki

ngKhal
idUni
ver
si
ty
Col
legeofSci
enceandAr
tsi
nTanomah

Comput
erSc
ienc
eDe
par
tme
nt

LabEv
aluat
ion#1
AY1444-1445–Ter m 3–
t
h Obt
ainedMar
k:
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
Thr
eadext
endsThr
ead{

pr
ivat
eintcount
;

pr
ivat
eSt
ri
ngt
hreadName;

publ
i
cmy
Thr
ead(
intcount
er,
Str
ingname)
{

t
his.
count=count
er;

t
his.
thr
eadName=name;
Al
lthr
eadsst
art
ed.
}
T1st
art
ed.

publ
i
cvoi
drun(
){ T1hascount
er5T1ended.
Sy
stem.
out
.pr
int
ln(
thi
s.t
hreadName+"st
art
ed.
")
;
Al
lthr
eadsended
Sy
stem.
out
.pr
int
ln(
thi
s.t
hreadName+"hascount
er"
+count
);

Sy
stem.
out
.pr
int
ln(
thi
s.t
hreadName+"ended.
")
;

publ
i
ccl
assTest
{

223CCS3–Adv
ancedObj
ect
-Or
ient
edPr
ogr
ammi
ng LabEv
al#1–AY1444/ m 3–May8th2023
1445–Ter Page:1/
1
publ
i
cst
ati
cvoi
dmai
n(St
ri
ng[
]ar
gs)
{

my
Thr
eadt
1=newmy
Thr
ead(
5,"
T1"
);

my
Thr
eadt
2=newmy
Thr
ead(
10,
"T2"
);

t
1.st
art
();

Sy
stem.
out
.pr
int
ln(
"Al
lthr
eadsst
art
ed.
")
;

t
1.j
oin(
);

Sy
stem.
out
.pr
int
ln(
"Al
lthr
eadsended"
);

Quest
ion2(
3pt
s)
Wri
teaThread-
basedclassnamedmy Thr
ead.Thist
hreadwi
ll
pri
ntonthescreen“ Hell
o”whenitst
art
s,
t
henitwai
tsfor20seconds,andf
inal
l
yitpri
nts“Thr
eadDone”onthescr
een.(tr
y-catchbl
ockisopti
onal
).

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

223CCS3–Adv
ancedObj
ect
-Or
ient
edPr
ogr
ammi
ng LabEv
al#1–AY1444/ m 3–May8th2023
1445–Ter Page:2/
2
Quest
ion3(
3pt
s)
Wr
it
eacl
assnamedmy
Testwi
thamai
nmet
hod.Themai
nmet
hod
1.cr
eat
esf
ourt
hreadsf
rom my
Thr
eadcl
ass,
2.st
art
sthef
ourt
hreads,
3.andt
henwai
tfort
hef
ourt
hreadst
ofi
nish,
4.f
inal
l
y,pr
int
s“Al
lisdone”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"
);
}

}
}

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
‫ﻴﻖ‬
‫ﻓ‬‫ﻮ‬‫ﺘ‬
‫ﻟ‬‫ﺎ‬
‫ﺑ‬

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

You might also like