You are on page 1of 17

Question Paper

Object Oriented Programming and Java (MC221) : April 2!


"ection A : #asic Concepts ($ Mar%s)
This section consists of questions with serial number 1 - 30.
Answer all questions.
Each question carries one mark.
Maximum time for answering Section A is 30 Minutes.
1& hat is the !rintout of the following co"e#
"ouble x $ %.%&
int ' $ (int)x&
S'stem.out.!rintln(*x is * + x + * an" ' is * + ')&
(a) x is %.0 an" ' is ,.0
(b) x is ,.0 an" ' is ,.0
(c) x is , an" ' is ,
(") x is %.% an" ' is %.0
(e) x is %.% an" ' is ,.%.

2& hich of the following is the correct ex!ression of character -#
(a) -
(b) .-/
(c) 01000-2
(") 0-2
(e) 01-2.

$& 3ow man' b'tes a 4a5a character uses for storing#
(a) 6ne b'te
(b) Two b'tes
(c) Three b'tes
(") 7our b'tes
(e) 7i5e b'tes.

'& hich of the following assignment statement(s) is8are correct#
9. char c $ :":&
99. char c $ 100&
999. char c $ *"*&
9;. char c $ *100*&
(a) 6nl' (9) abo5e
(b) 6nl' (99) abo5e
(c) <oth (9) an" (99) abo5e
(") <oth (99) an" (999) abo5e
(e) <oth (999) an" (9;) abo5e.

(& hat "oes the ex!ression (int)(=,.0>%>1=% ? 100) 8 100 e5aluate to#
(a) =,.0>
(b) =,
(c) =,.0>%>1=%
(") =,.03
(e) =,.03%>=1%.

)& hich of the follows 4@A comman" is correct to run a 4a5a a!!lication in <'teBo"e.class#
(a) Ca5a <'teBo"e
(b) Ca5a <'teBo"e.class
(c) Ca5ac <'teBo"e.Ca5a
(") Ca5ac <'teBo"e
(e) 4A;AB <'teBo"e.

!& Su!!ose 'ou "efine a 4a5a class as followsD
!ublic class Test E
F
9n or"er to com!ile this !rogramG the source co"e shoul" be store" in a file name"
(a) Test. class
(b) Test."oc
(c) Test.txt
(") Test. Ca5a
(e) An' name with extension .Ca5a.

*& hich of the following statement is not true#
(a) A "efault no-arg constructor is !ro5i"e" automaticall' if no constructors are ex!licitl' "eclare"
in the class
(b) At least one constructor must alwa's be "efine" ex!licitl'
(c) Bonstructors "o not ha5e a return t'!eG not e5en 5oi"
(") Bonstructors must ha5e the same name as the class itself
(e) Bonstructors are in5oke" using the new o!erator when an obCect is create".

+& hat is ,rong in the following co"e#
class Tem!Blass E
int i&
!ublic 5oi" Tem!Blass(int C) E
int i $ C&
F
F
!ublic class B E
!ublic static 5oi" main(StringHI args) E
Tem!Blass tem! $ new Tem!Blass(>)&
F
F
(a) The !rogram has a com!ilation error because Tem!Blass "oes not ha5e a "efault constructor
(b) The !rogram has a com!ilation error because Tem!Blass "oes not ha5e a constructor with an int
argument
(c) The !rogram com!iles fineG but it "oes not run because class B is not !ublic
(") The !rogram com!iles an" runs fine
(e) All of the abo5e.

1& To "eclare a constant MAJKLEMNT3 as a member of the classG 'ou write
(a) 7inal static MAJKLEMNT3 $ OO.OP&
(b) 7inal static float MAJKLEMNT3 $ OO.OP&
(c) Static "ouble MAJKLEMNT3 $ OO.OP&
(") 7inal "ouble MAJKLEMNT3 $ OO.OP&
(e) 7inal static "ouble MAJKLEMNT3 $ OO.OP&.

11& hat is the !rintout of the secon" !rintln statement in the main metho"#
!ublic class 7oo E
int i&
static int s&
!ublic static 5oi" main(StringHI args) E
7oo f1 $ new 7oo()&
S'stem.out.!rintln(*f1.i is * + f1.i + * f1.s is * + f1.s)&
7oo f> $ new 7oo()&
S'stem.out.!rintln(*f>.i is * + f>.i + * f>.s is * + f>.s)&
7oo f3 $ new 7oo()&
S'stem.out.!rintln(*f3.i is * + f3.i + * f3.s is * + f3.s)&
F
!ublic 7oo() E
i++&
s++&
F
F
(a) f>.i is 1 f>.s is 1
(b) f>.i is 1 f>.s is >
(c) f>.i is > f>.s is >
(") f>.i is > f>.s is 1
(e) f>.i is 0 f>.s is 1.

12& hat is the !rintout of the thir" !rintln statement in the main metho"#
!ublic class 7oo E
int i&
static int s&
!ublic static 5oi" main(StringHI args) E
7oo f1 $ new 7oo()&
S'stem.out.!rintln(*f1.i is * + f1.i + * f1.s is * + f1.s)&
7oo f> $ new 7oo()&
S'stem.out.!rintln(*f>.i is * + f>.i + * f>.s is * + f>.s)&
7oo f3 $ new 7oo()&
S'stem.out.!rintln(*f3.i is * + f3.i + * f3.s is * + f3.s)&
F
!ublic 7oo() E
i++&
s++&
F
F
(a) f3.i is 1 f3.s is 1
(b) f3.i is 1 f3.s is >
(c) f3.i is 1 f3.s is 3
(") f3.i is 3 f3.s is 1
(e) f3.i is 3 f3.s is 3.

1$& hat co"e ma' be fille" in the blank without causing s'ntax or runtime errors#
!ublic class Test E
Ca5a.util.@ate "ate&
!ublic static 5oi" main(StringHI args) E
Test test $ new Test()&
S'stem.out.!rintln(KKKKKKKKKKKKKKKKK)&
F
F
(a) Test."ate
(b) @ate
(c) Test."ate.toString()
(") @ate.toString()
(e) @ate.test.

1'& Anal'Qe the following co"e an" choose the correct answerD
!ublic class 7oo E
!ri5ate int x&
!ublic static 5oi" main(StringHI args) E
7oo foo $ new 7oo()&
S'stem.out.!rintln(foo.x)&
F
F
(a) Since x is !ri5ateG it cannot be accesse" from an obCect foo
(b) Since x is "efine" in the class 7ooG it can be accesse" b' an' metho" insi"e the class without
using an obCect. Rou can write the co"e to access x without creating an obCect such as foo in this
co"e
(c) Since x is an instance 5ariableG it cannot be "irectl' use" insi"e a main metho". 3owe5erG it can
be accesse" through an obCect such as foo in this co"e
(") Rou cannot create a self-reference" obCect& that isG foo is create" insi"e the class 7oo
(e) Since x is !ublic it cannot be accesse" from an obCect foo.

1(& hat is the !rintout for the secon" statement in the main metho"#
!ublic class 7oo E
static int i $ 0&
static int C $ 0&
!ublic static 5oi" main(StringHI args) E
int i $ >&
int k $ 3&
E
int C $ 3&
S'stem.out.!rintln(*i + C is * + i + C)&
F
k $ i + C&
S'stem.out.!rintln(*k is * + k)&
S'stem.out.!rintln(*C is * + C)&
F
F
(a) k is 0
(b) k is 1
(c) k is >
(") k is 3
(e) k is -1.

1)& hat woul" be the result while attem!ting to com!ile an" run the following co"e#
!ublic class Test E
!ublic static 5oi" main(StringHI args) E
"oubleHI x $ new "oubleHIE1G >G 3F&
S'stem.out.!rintln(*;alue is * + xH1I)&
F
F
(a) The !rogram has a s'ntax error because the s'ntax new "oubleHIE1G >G 3F is wrong an" it shoul"
be re!lace" b' E1G >G 3F.
(b) The !rogram has a s'ntax error because the s'ntax new "oubleHIE1G >G 3F is wrong an" it shoul"
be re!lace" b' new "oubleH3IE1G >G 3F&
(c) The !rogram has a s'ntax error because the s'ntax new "oubleHIE1G >G 3F is wrong an" it shoul"
be re!lace" b' new "oubleHIE1.0G >.0G 3.0F&
(") The !rogram com!iles an" runs fine an" the out!ut *;alue is 1.0* is !rinte"
(e) The !rogram com!iles an" runs fine an" the out!ut *;alue is >.0* is !rinte".

1!& hat is the out!ut of the following co"e#
!ublic class Test E
!ublic static 5oi" main(StringHI args) E
intHI x $ E1G >G 3G -G %F&
increase(x)&
intHI ' $ E1G >G 3G -G %F&
increase('H0I)&
S'stem.out.!rintln(xH0I + * * + 'H0I)&
F
!ublic static 5oi" increase(intHI x) E
for (int i $ 0& i S x.length& i++)
xHiI++&
F
!ublic static 5oi" increase(int ') E
'++&
F
F
(a) 0 0
(b) 1 1
(c) > >
(") > 1
(e) 1 >.

1*& Assume intHI scores $ E1G >0G 30G -0G %0FG what 5alue "oes Ca5a.util.Arra's.binar'Search(scoresG 3)
return#
(a) 0
(b) -1
(c) 1
(") >
(e) ->.

1+& hen 'ou run an a!!letG which of the following is in5oke" first#
(a) The init metho"
(b) The start metho"
(c) The sto! metho"
(") The "estro' metho"
(e) The a!!let:s "efault constructor.

2& Anal'Qe the following co"e.
1. im!ort Ca5a.util.?&
>. !ublic class Test E
3. !ublic static 5oi" main(StringHI args) E
-. Balen"arHI calen"ars $ new Balen"arH10I&
%. calen"arsH0I $ new Balen"ar()&
,. calen"arsH1I $ new NregorianBalen"ar()&
=. F
P. F
(a) The !rogram has a s'ntax error on Line - because Ca5a.util.Balen"ar is an abstract class
(b) The !rogram has a s'ntax error on Line % because Ca5a.util.Balen"ar is an abstract class.
(c) The !rogram has a s'ntax error on Line , because Balen"arH1I is not of a NregorianBalen"ar
t'!e
(") <oth (a) an" (b) abo5e
(e) <oth (b) an" (c) abo5e.

21& E5er' e5ent obCect has the KKKKKKKK metho".
(a) getSource()
(b) getActionBomman"()
(c) getTimeStam!()
(") gethen()
(e) getAe'Bhar().

22& Anal'Qe the following co"eD
!ublic class Test E
!ublic static 5oi" main(StringHI args) E
String s $ new String(*elcome to 4a5a*)&
6bCect o $ s&
String " $ (String)o&
F
F
(a) hen assigning s to o in 6bCect o $ sG a new obCect is create"
(b) hen casting o to s in String " $ (String)oG a new obCect is create"
(c) hen casting o to s in String " $ (String)oG the contents of o is change"
(") sG oG an" " reference the same String obCect
(e) sGoGan" " "oesnot reference the same String obCect.

2$& Anal'Qe the following co"eD
!ublic class Test E
int x&
E x++& F
F
(a) The !rogram cannot be com!ile"G because the statement x++ must be !lace" insi"e a metho" or
a constructor
(b) Rou cannot construct an instance of TestG because it "oes not ha5e a constructor
(c) hen 'ou construct an instance of TestG the 5alue of x becomes 0&
(") hen 'ou construct an instance of TestG the 5alue of x becomes 1&
(e) hen 'ou contruct an instance of Test the 5alue of x become -1.

2'& The co"e causes 4a5a to throw
int number $ 9nteger.MAJK;ALTE + 1&
(a) UuntimeExce!tion
(b) Exce!tion
(c) Error
(") Throwable.
(e) Mo exce!tions.

2(& After Anal'Qing the following co"eG which statement is a!!ro!riate#
class Test E
!ublic static 5oi" main(StringHI args)
throws M'Exce!tion E
S'stem.out.!rintln(*elcome to 4a5a*)&
F
F
class M'Exce!tion exten"s Error E
F
(a) Rou shoul" not "eclare a class that exten"s ErrorG because Error raises a fatal error that
terminates the !rogram
(b) Rou cannot "eclare an exce!tion in the main metho"
(c) Rou "eclare" an exce!tion in the main metho"G but 'ou "i" not throw it
(") The !rogram has a com!ilation error
(e) Rou can "eclare a class that exten"s error.

2)& hat is the out!ut for '#
int ' $ 0&
for (int i $ 0& iS10& ++i) E
' +$ i&
F
S'stem.out.!rintln(')&
(a) 10
(b) 11
(c) 1>
(") 13
(e) -%.

2!& hat will be the 5alue of i for the following loo!#
int ' $ 0&
for (int i $ 0& iS10& ++i) E
' +$ i&
F
(a) O
(b) 10
(c) 11
(") 1>
(e) Tn"efine".

2*& hich of the following !ro5i"es the naming ser5ices for the ser5er to register the obCect an" for the
client to locate the obCect#
(a) Ser5er obCect interface
(b) Ser5er im!lementation
(c) UM9 Uegistr'
(") Ser5er stub
(e) Ser5er Skeleton.

2+& hich of the following statement is not "efine" in the 6bCect class#
(a) slee!(long millisecon"s)
(b) wait()
(c) notif'()
(") notif'All()
(e) toString().

$& hich of the following metho" is a static in Ca5a.lang.Threa"#
(a) run()
(b) slee!(long)
(c) start()
(") Coin()
(e) setVriorit'(int).

-./ O0 "-C12O. A
"ection # : Problems (( Mar%s)
This section consists of questions with serial number 1 W %.
Answer all questions.
Marks are in"icate" against each question.
@etaile" workings shoul" form !art of 'our answer.
@o not s!en" more than 110 - 1>0 minutes on Section <.
1& Bonsi"er the following class "efinitionD
class A E
class < E
!ublic class B E
!ublic class @ E
!ri5ate String "String $ *"ee!*&
F
!ri5ate @ m'@ $ new @ ( )&
F
!ri5ate B m'B $ new B ( )&
F
!ri5ate < m'< $ new <( )&
!ublic A ( ) E
m'<.m'B.m'@."String $ *oooh*&
F
F
ithout changing an' co"e for the inner classes <G B or @ "o the following. A"" a new
metho" to class A that sets the 5alue of fiel" "String in the obCect m'@. Mext a"" a getter
metho" to class A that returns the 5alue of "String in the obCect m'@.
(1 mar%s)

2& This 4a5a !rogram com!iles without an' errorsD
class A E
!ri5ate String f&
class < E
!ri5ate String f1 $ f&
F
F
hereas if we change it as shown below
class A E
!ri5ate String f&
F
class < E
!ri5ate String f1 $ f&
F
e get the errorD
cannot fin" s'mbol
s'mbol D 5ariable f
Ex!lain briefl' on the abo5e.
(1 mar%s)

$& rite the 4a5a co"e for the a!!lication below. 9t shoul" start counting in the text fiel" when
the .start/ button is !resse"G shoul" !ause when the .!ause/ button is !resse"G resume
counting when the .!ause/ button is !resse" again an" sto! counting when the .sto!/ button
is !resse".

Bounting A!!lication
(1 mar%s)
'& A 4a5a a!!lication has been written that uses a number of threa"s. 3owe5er it turns out that
the threa"s will ha5e a resource contention. The' will therefore require a lock class to be
written to allow them to access the resource safel' one at a time. rite a suitable lock class
base" on the following !seu"o-TML class "iagramD
Uecall that the s'nchroniQe" metho"s must use the wait an" notif' all metho"s to successfull'
!ro5i"e a threa" s'nchroniQe" lock. That onl' one threa" obCect can access the lock at an'
time. Uecall also that the wait metho" throws 9nterru!te"Exce!tionG an" must be containe" in
a tr'-catch statement. Tse the !ri5ate boolean 5alue to control access to the lock. So that the
lock is free when a5ailable $$ trueG an" not otherwise.
(1 mar%s)

(& The following class M'Blass has an un!rotecte" fiel". 9n the class UogueBlass it is !ossible
to tr' to assign a 5alue to "bl using an' string. That can lea" to a runtime errorG raising the
Mumber7ormatExce!tion exce!tion. Bhange class M'Blass so that "bl becomes !ri5ate an"
can onl' be accesse" b' a set an" get metho". Make sure that the set metho" correctl' catches
exce!tions.
!ublic class M'Blass E
@ouble "bl&
F
!ublic class UogueBlass E
M'Blass mc $ new M'Blass()&
!ublic 5oi" makeKtrouble (String x) E
mc."bl $ new @ouble(x)&
F
F
(1 mar%s)

-./ O0 "-C12O. #
"ection C : Applied 13eor4 (2 Mar%s)
This section consists of questions with serial number , - =.
Answer all questions.
Marks are in"icate" against each question.
@o not s!en" more than >% -30 minutes on section B.
)&
Bom!are an" contrast applets to applications. hen woul" one choose to use A!!lets o5er
a!!lications# hat are the limitations of a!!lets# @escribe the lifec'cle of a!!lets an"
a!!lications. Ban an a!!lication be an a!!let#
(1 mar%s)

!& a. @iscuss the "ifferent access s!ecifiers a5ailable in the 4a5a !rogramming language.
b. hat are the "ifferent la'out mangers# Ex!lain on an' t,o briefl'.
($ 5 ! 6 1 mar%s)

-./ O0 "-C12O. C
-./ O0 Q7-"12O. PAP-8
"uggested Ans,ers
Object Oriented Programming and Java (MC221) : April 2!
"ection A : #asic Concepts
1& Answer D (")
UeasonD The 5alue is x is not change" after the casting.

2& Answer D (")
UeasonD Rou ha5e to write :-:.

$& Answer D (b)
UeasonD 4a5a characters use Tnico"e enco"ing.

'& Answer D (c)
UeasonD Bhoice (<) is also correctG because an int 5alue can be im!licitl' cast into a char 5ariable.
The Tnico"e of the character is the int 5alue. 9n this caseG the character is "

(& Answer D (b)
UeasonD Rour answer @ is incorrect
9n or"er to obtain =,.0>G 'ou ha5e "i5i"e 100.0.

)& Answer D (a)
UeasonD A is the right choice.

!& Answer D (")
UeasonD Test.Ca5a is the name of the source co"e of the file.

*& Answer D (b)
UeasonD There is no certain rule that at least one constructor must alwa's be "efine" ex!licitl'.

+& Answer D (b)
UeasonD The !rogram woul" be fine if the 5oi" ke'wor" is remo5e" from !ublic 5oi"
Tem!Blass(int C).

1& Answer D (e)
UeasonD E is the right wa' to re!resent.

11& Answer D (b)
UeasonD i is an instance 5ariable an" s is staticG share" b' all obCects of the 7oo class.

12& Answer D (c)
UeasonD i is an instance 5ariable an" s is staticG share" b' all obCects of the 7oo class.

1$& Answer D (a)
UeasonD b an" " cause s'ntax errors because "ate is an instance 5ariable an" cannot be accesse"
from static context. c is wrong because test."ate is nullG causing MullVointerExce!tion.

1'& Answer D (c)
UeasonD (A) is incorrectG since x can be accesse" b' an obCect of 7oo insi"e the 7oo class. (<) is
incorrect because x is non-staticG it cannot be accesse" in the main metho" without creating
an obCect. (@) is incorrectG since it is !ermissible to create an instance of the class within the
class.(E) is incorrect as x is "eclare" as !ri5ate but not !ublic.The best choice is (B).

1(& Answer D (c)
UeasonD hen com!uting k $ i + C& i $ > an" C $ 0.

1)& Answer D (e)
UeasonD new "oubleHIE1G >G 3F is correct. This is the s'ntax. 9n this questionG "oubleHI x $ new
"oubleHIE1G >G 3F is equi5alent to "oubleHI x $ E1G >G 3F&

1!& Answer D (")
UeasonD 9n5oking increase(x) !asses the reference of the arra' to the metho". 9n5oking
increase('H0I) !asses the 5alue 1 to the metho". The 5alue 'H0I outsi"e the metho" is not
change".

1*& Answer D (e)
UeasonD The binar'Search metho" returns the in"ex of the search ke' if it is containe" in the list.
6therwiseG it returns #insertion !oint - 1. The insertion !oint is the !oint at which the ke'
woul" be inserte" into the list. 9n this case the insertion !oint is 1. Mote that the arra' in"ex
starts from 0.

1+& Answer D (e)
UeasonD hen the a!!let is loa"e" to the eb browserG the eb browser creates an instance of the
a!!let b' in5oking the a!!lets "efault constructor.

2& Answer D (b)
UeasonD (A) is incorrect since it is 6A to use abstract class as "ata t'!e for arra's. new Balen"arH10I
"oes not create Balen"ar obCects. 9t Cust creates an arra' with 10 elementsG each of which
can reference to a Balen"ar obCect. (<) is correct since 'ou cannot create an obCect from an
abstract class. (B) is incorrect since it is fine to create a NregorianBalen"ar obCect an"
assign its reference to a 5ariable of its su!erclass t'!e.

21& Answer D (a)
UeasonD E5er' e5ent obCect is a subclass of E5ent6bCectG which contains the getSource() metho".

22& Answer D (")
UeasonD Basting obCect reference 5ariable "oes not affect the contents of the obCect.

2$& Answer D (")
UeasonD x++ is in an initialiQation block. 9t is in5oke" when an' constructor of the class is in5oke".

2'& Answer D (e)
UeasonD At !resentG 4a5a "oes not throw integer o5erflow exce!tions. The future 5ersion of 4a5a
ma' fix this !roblem to throw an o5er flow exce!tion.

2(& Answer D (a)
UeasonD hen an exce!tion of Error t'!e occursG 'our !rogram woul" terminate. ThereforeG 'ou
shoul" not "eclare an exce!tion that exten"s Error.

2)& Answer D (e)
UeasonD Rour answer @ is incorrect
' shoul" be 0 + 1 + > + 3 + - + % + , + = + P + O $ -%

2!& Answer D (e)
UeasonD The sco!e of i is insi"e the loo!. After the loo!G i is not "efine".

2*& Answer D (c)
UeasonD The UM9 registr' !ro5i"es the naming ser5ices for the ser5er to register the obCect an" for
the client to locate the obCect.

2+& Answer D (a)
UeasonD Uemaining other statements are "efine" in the obCect class.

$& Answer D (b)
UeasonD < is the a!t choice.

"ection # : Problems
1& "olution:
(% marks for correct "eclarationsG s'ntax an" so on)
!ublic String get@string ( ) E
return m'<.m'B.m'@."String& (% marks)
F
!ublic 5oi" set@string (String st) E
m'<.m'B.m'@."String $ st& (% marks)
F

2& The first 5ersion "eclares < as an inner class to A. (> marks) Therefore < can access all
fiel"s an" metho"s that belong to A "irectl'G an" hence fiel" f is within the logical sco!e of

class <. hereas in the secon" 5ersion < is external to A an" can not access its fiel"s
"irectl'.
$& Bounting A!!lication
im!ort Ca5a.a!!let.?&
im!ort Ca5a.awt.?&
im!ort Ca5a.awt.e5ent.?&
!ublic class BounterA!!lication exten"s 7rame im!lements ActionListener
E
!ri5ate <utton startG sto!G !ause&
!ri5ate Text7iel" count7iel"1&
!ri5ate Bounter theBounter1&
!ublic BounterA!!lication()
E
su!er(*Bounter A!!lication*)&
this.setLa'out(new 7lowLa'out())&
count7iel"1 $ new Text7iel"(10)&
this.a""(count7iel"1)&
start $ new <utton(*Start*)&
start.a""ActionListener(this)&
this.a""(start)&
sto! $ new <utton(*Sto!*)&
sto!.a""ActionListener(this)&
this.a""(sto!)&
!ause $ new <utton(*Vause*)&
!ause.a""ActionListener(this)&
this.a""(!ause)&
this.theBounter1 $ new Bounter(count7iel"1G 100G 0)&
this.setSiQe(300G100)&
this.set;isible(true)&
F
!ublic 5oi" actionVerforme"(ActionE5ent e)
E
if (e.getSource().equals(start))
E
this.theBounter1.start()&
F
else if (e.getSource().equals(sto!))
E
this.theBounter1.sto!Bounter()&
F
else if (e.getSource().equals(!ause))
E
this.theBounter1.toggleBounter()&
F
F
!ublic static 5oi" main(String argsHI)
E
new BounterA!!lication()&
F
F
class Bounter exten"s Threa"
E
!ri5ate int countG "ela'&

!ri5ate boolean running $ trueG !ause" $ false&
!ri5ate Text7iel" the7iel"&
!ublic Bounter(Text7iel" tG int "ela'G int start;alue)
E
this.the7iel" $ t&
this.count $ start;alue&
this."ela' $ "ela'&
F
!ublic 5oi" run()
E
while (running)
E
the7iel".setText(*BountD *+this.count++)&
tr'
E
Threa".currentThrea"().slee!(this."ela')&
s'nchroniQe"(this) E
while (!ause")
wait()&
F
F
catch (9nterru!te"Exce!tion e)
E
the7iel".setText(*9nterru!te"X*)&
this.sto!Bounter()&
F
F
F
!ublic int getBount() E return count& F
!ublic 5oi" sto!Bounter() E this.running $ false& F
!ublic s'nchroniQe" 5oi" toggleBounter()
E
this.!ause" $ Xthis.!ause"&
if (Xthis.!ause") notif'()&
F
F
'& !ublic class Lock
E
!ri5ate boolean a5ailable $ true&
!ublic s'nchroniQe" 5oi" grab()
Ewhile (a5ailable $$ false)
E
tr' E88 wait for Vro"ucer to !ut 5aluewait()&
F catch (9nterru!te"Exce!tion e)
E
F
F
a5ailable $ false&88 notif' Vro"ucer that 5alue has been retrie5e"notif'All()&
F
!ublic s'nchroniQe" 5oi" release() E
while (a5ailable $$ true)
E
tr' E88 wait for Bonsumer to get 5aluewait()&

F catch (9nterru!te"Exce!tion e)
E
F
F
a5ailable $ true&88 notif' Bonsumer that 5alue has been set
notif'All()&
F
F
Uecall that the s'nchroniQe" metho"s must use the wait an" notif' all metho"s to
successfull' !ro5i"e a threa" s'nchroniQe" lock. That onl' one threa" obCect can access the
lock at an' time. Uecall also that the wait metho" throws 9nterru!te"Exce!tionG an" must be
containe" in a tr'-catch statement. Tse the !ri5ate boolean 5alue to control access to the
lock. So that the lock is free when a5ailable $$ trueG an" not otherwise.
(& !ublic class M'Blass E
!ri5ate @ouble "bl&
!ublic setK"bl (String st) E
tr' E
"bl $ new @ouble(st)&
F catch (Mumber7ormatExce!tion ex!1) E
S'stem.out.!rintln(*666VSD * + e1.toString() + *1n*)&
F
F
!ublic getK"bl () Ereturn "bl&F
F
> marks making "bl !ri5ate
3 marks "eclaring set an" get correctl'
% marks for tr' catch correct
for han"ling correct exce!tion
for get metho"

"ection C: Applied 13eor4
)& An a!!let is a s!ecific t'!e of 4a5a a!!lication that is "esigne" to be !ortable an"
"ownloa"able o5er the 9nternet. 7or this reason an a!!let ma' onl' contain a subset of the
total functionalit' of an a!!lication. 7or exam!leG an a!!let cannot ha5e access to local files
on a machine& otherwise it is theoreticall' !ossible for a hostile a!!let to u!loa" (to some
"estination) the contents of a local machine. This t'!e of hostile a!!let also inclu"es a!!lets
that ma' tr' to use all BVT a5ailableG or contact remote sitesG without 'our !ermission.
These too are not allowe". A!!lications on the other han" are assume" to be truste"G as the'
resi"e on the local har" "isk. These a!!lications ha5e full access to the local har"-"ri5e an"
to all network a""resses.
An a!!let woul" be chosen when an a!!lication must be "istribute" to a number of clients
who trustG or "on2t trust the content. The' allow a local 5ersion to resi"e on the ser5erG so
onl' one co!' existsG allowing com!lete 5ersion control.
An a!!lication on the other han" "oes not contain the inherent restrictions an" so must be
com!letel' truste" b' the client. An in"e!en"ent 5ersion is "istribute" to each client so there
is not the same "egree of 5ersion control as that associate" with a!!lets. There is no
o5erhea" in "ownloa"ing the a!!lication before running it. Rou also "o not ha5e to be
connecte" to the 9nternet to run an a!!lication.
The or"er of execution of an a!!let is main() an" then whate5er functionalit' that is
in5ol5e" in main() 9n an a!!let the or"er is init() -Y start() -Y !aint() an" sto!() on the
"estruction. These
calls ma' ha!!en as'nchronousl'.
An a!!lication ma' be an a!!let an" an a!!lication. This is create" b' exten"ing the a!!let
class (Ca5a.awt.A!!let) an" then a""ing a main() metho" to the A!!let class b' calling the
init()-Ystart()-Y!aint() metho"s in the or"er that the a!!let5iewer woul".

!& a. 9n 4a5a we ha5e !ublicG !ri5ate an" !rotecte" access s!ecifiersG but we also ha5e
another accesss!ecifier *!ackage*. This is the "efault access s!ecifier an" means that
all states an" metho"s are accessible to all classes within the same !ackage. There is no
!ackage access s!ecifier ke'wor"G it is sim!l' the "efault if !ublicG !ri5ate or !rotecte"
are not use". Access s!ecifiers in 4a5a areD
!ublic - accessible e5er'whereG an interface metho" (same as B++)
!ri5ate - accessible onl' in the class where it was "efine" (same as B++)
!rotecte" - accessible in the class where it is "efine"G the "eri5e" classes an" in the
same !ackage (almost the same as B++ with the exce!tion of the !ackage)
*!ackage* - "efault (there is no !ackage s!ecifier ke'wor") an" means that it is
accessible b' an' class in the same !ackage. Rou can equate this to the B++ frien"l'
con"ition b' sa'ing that all of the classes in the same !ackage ("irector') are frien"l'.
b. The 4a5a !latform su!!lies fi5e commonl' use" la'out managersD
1. <or"erLa'out
>. <oxLa'out
3. 7lowLa'out
-. Nri"<agLa'out
%. Nri"La'out.
,. Bar"La'out
These la'out managers are "esigne" for "is!la'ing multi!le com!onents at once.
#order9a4out
<or"erLa'out is the "efault la'out manager for e5er' content !ane. A content !ane is
the main container in all framesG a!!letsG an" "ialogs. A <or"erLa'out has fi5e areas
a5ailable to hol" com!onentsD northG southG eastG westG an" center. All extra s!ace is
!lace" in the center area.
#o:9a4out
The <oxLa'out class !uts com!onents in a single row or column. 9t res!ects the
com!onents: requeste" maximum siQesG an" also lets 'ou align com!onents.
0lo,9a4out
7lowLa'out is the "efault la'out manager for e5er' 4Vanel. 9t sim!l' la's out
com!onents from left to rightG starting new rows if necessar'. 3ere:s an exam!le of an
a!!let that uses a 7lowLa'outD
;rid#ag9a4out
Nri"<agLa'out is the most so!histicate"G flexible la'out manager the 4a5a !latform
!ro5i"es. 9t aligns com!onents b' !lacing them within a gri" of cellsG allowing some
com!onents to s!an more than one cell. The rows in the gri" aren:t necessaril' all the
same height& similarl'G gri" columns can ha5e "ifferent wi"ths. 3ere:s an a!!let that
uses a Nri"<agLa'out to manage fi5e buttonsD
;rid9a4out
Nri"La'out sim!l' makes a bunch of com!onents equal in siQe an" "is!la's them in
the requeste" number of rows an" columns. 3ere:s an a!!let that uses a Nri"La'out to
control the "is!la' of fi5e buttonsD
Card9a4out
The Bar"La'out class lets 'ou im!lement an area that contains "ifferent com!onents at
"ifferent times. Tabbe" !anes are interme"iate Swing containers that !ro5i"e similar
functionalit'G but with a !re-"efine" NT9. A Bar"La'out is often controlle" b' a
combo boxG with the state of the combo box "etermining which !anel (grou! of
com!onents) the Bar"La'out "is!la's. 3ere:s an a!!let that uses a combo box an"
Bar"La'out in this wa'D

You might also like