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

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 » Mon May 18, 2015 4:17 am

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: 8702
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin » Mon May 18, 2015 5:26 am

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 » Mon Oct 12, 2015 10:31 am

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: 8702
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin » Mon Oct 12, 2015 12:11 pm

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 » Tue Oct 13, 2015 3:07 am

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

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests