aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt with the answer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "doubt with the answer" Watch "doubt with the answer" New topic
Author

doubt with the answer

andy lau
Ranch Hand

Joined: Apr 08, 2002
Posts: 51
The code is as follows(from the mock exam JOP):

Which statement is true?
A.The andy class is fully encapsulated.
B.The diameter of a given andy 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 answer should contain D. But i am not sure whether the C is right. can you explain the answer C ?
Thanks a lot!
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
I'm not sure exactly what you mean by "encapsulation." Do you mean that the class is "thread-safe?" Encapsulation is generally used to mean something else, but it looks like you're really asking is the class is thread-safe.
In addition, I don't know which lines 6 and 7 are as you've provided no numbering. I'm guessing you're referring to these two lines:

If that's the case, then answer D is still wrong. The fact that the member variables radius and diameter are public basically negate any sort of thread protection.
By making those public, any thread can change them whenever they want, without adhering to the basic rules of the class; that is, the diameter is twice the length of the radius.
That's what I'd say, anyway. I hope that answers your question.
Corey


SCJP Tipline, etc.
andy lau
Ranch Hand

Joined: Apr 08, 2002
Posts: 51
what does the ''thread-safe'' mean? is there any link to this topic?
i dont understand why you say D is still wrong ? can you explain more ?
thanks a lot!
Jim Bedenbaugh
Ranch Hand

Joined: Nov 09, 2001
Posts: 171
Originally posted by andy lau:
The code is as follows(from the mock exam JOP):

Which statement is true?
A.The andy class is fully encapsulated.
B.The diameter of a given andy 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 answer should contain D. But i am not sure whether the C is right. can you explain the answer C ?
Thanks a lot!


By process of elimination:
A is wrong because the class is not fully encapsulated. radius and diameter are public - why bother having a set method?
B looks right, but if you think about the precision of a double, you might actually get a value that is truncated and not precisely double the radius. So, this can't be right.
C Hmmm. . .if this were in a thread and you suppose you were interested in making sure that the radius and diameter were set before some other thread jumped in and set the values. . .but you have to make a lot of assumptions here and I don't see it's contribution to encapsulation. I must have skipped that part of the chapter covering encapsulation. . .
D Well, this can't be right because everytime you set the radius, you recalculate the diameter, assuming you call the class method setRadius() and don't directly change the member variable, which is not good OOP.
Well, the only one that looks like it could be close to right is C. I don't think I like this question, though. IMHO, the question could use some work.
Any of you bartenders, etc. got any thoughts?


Regards,
Jim
SCJP, SCJD, SCWCD, SCEA Part I
Jim Bedenbaugh
Ranch Hand

Joined: Nov 09, 2001
Posts: 171
Originally posted by Corey McGlone:
I'm not sure exactly what you mean by "encapsulation."
Corey

I'd say this question has some real problems.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by andy lau:
what does the ''thread-safe'' mean

A class is considered to be thread-safe if multiple threads can utilize the same instance of that class (or the class itself) without running into data integrity issues.
Let's look at an example:

The above code is not thread-safe.
Imagine a thread invokes the method addNumber. It executes the first line of code, which adds the number to the array but, before it can execute the line to update the counter, the thread is time-sliced out to allow another thread to execute.
Now, suppose that the new thread that is executing invokes the addNumber method. The number it sends is added to the array, but it overwrites the value that the other thread had put there because the counter wasn't incremented. Following that, the counter is incremented.
Some time later, the initial thread will get to finish its call to addNumber. All that's left to do is to increment the counter and exit the method.
So, after all that has been done, what do we have left? One of the values that should have been added to the array was overwritten and the counter says that there is one more item in the array than there really is. This is a data integrity issue.
Now, let's revise the code slightly:

By adding the keyword synchronized to the method addNumber, we've made the class thread-safe. Now, the condition that occured in the previous example can't possibly occur. When one thread enters the synchronized method, no others are allowed to enter it until it is complete.
I hope that helps. If you're still confused about what thread-safe means or about your original question, please ask.
Corey
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Jim Bedenbaugh:

I'd say this question has some real problems.

I agree. :roll: It's pretty vague.
Lance Titchkosky
Ranch Hand

Joined: Apr 30, 2002
Posts: 36
I think answer D is the only correct answer (note the original code is missing a bracket, I'm assuming that was just a typo).
If you run this code with this main you will see that D is correct, since the radius and diameter are public, they can be set outside of using the setter methods:

This outputs the following:

Answer C is wrong since to insure encapsulation you should set radius and diameter to private and have getter/setters for them.
lance
[ May 07, 2002: Message edited by: Lance Titchkosky ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: doubt with the answer