aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Encapsulation (was useless: Am i right!) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Encapsulation (was useless: Am i right!)" Watch "Encapsulation (was useless: Am i right!)" New topic
Author

Encapsulation (was useless: Am i right!)

Xiao Song
Greenhorn

Joined: Aug 29, 2006
Posts: 17

Which statement is true?
A. The Mycircle class is fully encapsulated.
B. The diameter of a given MyCircle is guaranteed to be twice its radius.
C. Lines 6 and 7 should be in a synchronized block to ensure encapsulation.
D. The radius of a MyCircle object can be set without affecting its diameter.


I think the right answer is D, am I right?
thanks advance!
[ November 24, 2006: Message edited by: Barry Gaunt ]

scjping.......
Abdi Duale
Greenhorn

Joined: Dec 13, 2005
Posts: 7
I am convinced that the answer is B. A is false because the class is not encapsulated as the members and the class are left public. C putting those two lines in a block won't encapsulate, I don't see how it would. D, once the radius is set, the diameter is obvious from the formula.

Best of luck!
Gian Franco
blacksmith
Ranch Hand

Joined: Dec 16, 2003
Posts: 977
I think the answer is D as well.

(A) False, MyCircle is not fully encapsulated, because it's instance variables are public and not private

(B) False, The diameter is not guaranteed to alway be twice it's radius, because the diameter can be altered by altering it's public variable

(C) False, Synchronization doesn not play a role in encapsulation in general, and encapsulation will still be broken by the above

(D) True, since the instance variable radius is public it can be accessed and altered without affecting the diameter.

Kind Regards,

Gian


"Eppur si muove!"
Abdul Rehman
Ranch Hand

Joined: Nov 07, 2006
Posts: 65
A. The MyCircle class is fully encapsulated.

Totally wrong. The class members are public and therefore, not encapsulated.

B. The diameter of a given MyCircle is guaranteed to be twice its radius.

Again wrong. The members are public and therefore, open to abrupt change. Moreover, no constructor means that the radius == diameter for a newly constructed object. Thus there is no guarantee at all.

C. Lines 6 and 7 should be in a synchronized block to ensure encapsulation.

The method setRadius() should be synchronized if it will be accessed by multiple threads simultaneously, to prevent a "data race". This is illustrated in the following two figures, taken from the JLS.




For explanation & details, see Section 17.4.5 in the JLS.

But still, this option isn't correct, because, this doesn't seem to be related with encapsulation.

Looking at this from another perspective, if the method setRadius() isn't synchronized & is accessed by multiple threads at the same time, it will result in disaster. The radius & diameter variables may get inconsistent/incorrect values due to this. Will this break/affect the encapsulation then? Anyone on this...

D. The radius of a MyCircle object can be set without affecting its diameter.

Yes, indeed. Both instance variables are public and therefore, can be changed in any manner whatsoever.

A & B are incorrect & D is correct, undoubtedly. I am unsure about option C.

Best regards,
Abdul Rehman.
[ November 24, 2006: Message edited by: Abdul Rehman ]

SCJP 5.0 (100%)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Encapsulation (was useless: Am i right!)