About Question enthuware.ocajp.i.v7.2.876 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
scranen
Posts: 12
Joined: Thu May 07, 2015 8:55 am
Contact:

About Question enthuware.ocajp.i.v7.2.876 :

Post by scranen »

This question is badly formulated; I guess the intention is clear, but the suggested refactoring not only "makes it better in terms of encapsulation", it also enforces an invariant that was not previously there. Making "side" and "area" private, and also making "setSide()" private would enforce another invariant (namely, side stays equal to the initial value and area stays equal to 0). While this may not make much sense in terms of application logic (assuming we know what the Square class is intended to do) or even in terms of code style, you could argue that in terms of encapsulation it does as good a job as the more logical refactoring that is marked as the correct answer to this question.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.876 :

Post by admin »

Well, you said yourself that the given answer is more logical :)
If you like our products and services, please help us by posting your review here.

heleneshaikh
Posts: 24
Joined: Wed Sep 02, 2015 3:43 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.876 :

Post by heleneshaikh »

I'm sorry, I do not understand why 'area' must be removed. Could you please elaborate? I indicated that both instance variables must be declared private and that the getArea() method must be made public.

Thank you in advance

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.876 :

Post by admin »

As the explanation says, "There is no need to keep the area field because that would amount to duplicating the data. If you change side, the value of area will become obsolete."

For example, let's say the side is 10 and area is 100. Now, if you change side to 5, who will change area to 25? area will still be 100. Which means, for a square of side 5, area is 100. This is wrong.

That is why, there is no need to keep area field at all because it is a computed value.
If you like our products and services, please help us by posting your review here.

heleneshaikh
Posts: 24
Joined: Wed Sep 02, 2015 3:43 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.876 :

Post by heleneshaikh »

Thank you very much for clarifying this. Have a nice day!

Thetri_enth
Posts: 6
Joined: Tue May 03, 2022 7:50 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.876 :

Post by Thetri_enth »

This question is unfair. The existing implementation of Square has no suggestion that 'area' will always be equal to side*side, we only see that in the testClass, but that does not imply that all uses of Square will always abide by that rule.

On the other hand, context tells us that if the Square class is meant to represent actual squares, the area will always be solely determined by the sides.

So, when taking the exam, you have to guess whether or not the test expects you to lean on context, and choose answers 4 and 5, or work solely with what is stated, and choose answers 3 and 6 (so that uses of Square can have a different area). How are we supposed to know which of these is correct?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.876 :

Post by admin »

Yes, you may require to make certain assumptions and infer things from the context in the exam as well based on common sense. Exam questions are created by humans and a certain amount of ambiguity may be present. So, it would be best to go with the most reasonable interpretation. Even in this question, it may be possible to conjure up a convoluted situation where area of a square is not the same as side*side and argue about the fairness of the question, but I don't think that is the right approach. One can safely assuming what the area of a square would be, specially when it is quite clear from the context.
If you like our products and services, please help us by posting your review here.

Thetri_enth
Posts: 6
Joined: Tue May 03, 2022 7:50 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.876 :

Post by Thetri_enth »

I would have to disagree.
First of, I wouldn't say that "I would need to conjure up a convoluted situation where the area of a square is not the same as side*side", as non-euclidean geometry is a well established subject by now. Although, I would agree that it seems unlikely that this exam would go into non-euclidean geometry.

Secondly, the 'context' also shows us that the code was explicitly written in such a way that the area field of our Object can be changed independently of the side field. Given the amount of trick questions the exam has, I do not think it is far fetched to think the exam tries to trick you into making an assumption that you are not allowed to, as they haven't explicitly stated that it is an okay assumption.

My point is that I do not see why the encapsulation offered by options 3 and 6 is incorrect, especially as the question asks for what changes I would make. The question is subjective; the encapsulation you choose depends on what features you want the code to have. There is no reason why the context clues for 'area of square is side*side' are better then the context clues for 'area should be changeable independently of side'. How am I supposed to know if the exam is trying to trick me, if both options offer correct encapsulation?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.876 :

Post by admin »

>Although, I would agree that it seems unlikely that this exam would go into non-euclidean geometry.
Well, we are talking about this exam, aren't we?

But again, I see your point. Area of a square is an 8th grade concept and it is reasonable assume that that is what is meant here. If it were about non-euclidean geometry, the question would have said so or given some extra information. So, we believe this question is fine and reflects the way exam asks you questions. If you feel it is unfair or incorrect, that is fine too. You may give an additional point to yourself in the mock exam. In the real exam, you have to use your best judgment, we can only provide you our opinion. But it is up to you, of course.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 46 guests