Page 1 of 1

About Question enthuware.ocajp.i.v8.2.876

Posted: Tue Aug 23, 2016 8:12 am
by lenach87
For such sort of questions, just want to understand, why the TestClass is given? So it has (bold typed)
public class TestClass {
public static void main(String[] args) throws Exception {
Square sq = new Square(10.0);
sq.area = sq.getSide()*sq.getSide();
System.out.println(sq.getArea());
}
}
And the right choice is "Make the side field private and remove the area field." from Square class. But if we remove area field from Square class, TestClass will not compile, or am I missing something?

Almost the same in question enthuware.ocajp.i.v8.2.877

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

Posted: Wed Aug 24, 2016 3:20 am
by Paulus
You can remove the area field because this is a calculated value. You don't need to store an extra variable that is always the result of side * side. The getArea() method can simply return side * side. There is no need to keep a variable for the area, by removing it you improve the encapsulation.

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

Posted: Wed Aug 24, 2016 5:20 am
by admin
Refactoring may involve recompilation and changes to other classes. When you refactor a class, you are trying to reduce coupling so naturally there will be some code that will break. So you need not worry about any class other than the one you are being asked to refactor.

HTH,
Paul.

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

Posted: Thu Aug 25, 2016 2:50 am
by lenach87
Thank you for the answers!

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

Posted: Sat Jul 14, 2018 12:53 pm
by st.lisker
Hello. Is "Refactoring" means that all options will be applied together? Not independently of each other as usual?

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

Posted: Sat Jul 14, 2018 8:54 pm
by admin
No, refactoring has nothing to do with number of options or their dependence. This question asks you to select only 1 option you don't worry about whether the options are independent or not.

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

Posted: Sun Jul 15, 2018 3:43 am
by st.lisker
Thank you of Sunday response. It's 2 options to choose, no one. And if look them independent the option "Make the side field private and remove the area field. " have no sense. That's why I did not choose it. How can I know, like in this case, I must consider the options together?

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

Posted: Sun Jul 15, 2018 4:16 am
by admin
Yes, you are right. You need to select two options. Generally, if it is important, the problem statement tells you if you need to consider the options independent of each other.

However, there are situations where it is obvious that the options are to be considered independent of each other. For example, if the problem statement is - which of the following statements correctly declare an integer variable named num?
1. int num;
2. int num = 2;
3. integer num = 0;
4. Int num;

Here, it is obvious that option 1 and option 2 cannot appear at the same time.

So in conclusion, most of the time, it will be clear as to what you need to consider. In case it is not clear, go with the most reasonable interpretation.

HTH,
Paul.

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

Posted: Sun Jul 15, 2018 6:41 am
by st.lisker
Thank you, Paul. I would not choose this option, where it is said to remove the area field because it will make all the rest of the code to be uncompiled ... But this is my opinion. ;) Nice day! Stephanie.

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

Posted: Sun Dec 26, 2021 11:48 pm
by ignore.your
You are assigned the task of refactoring the Square class to make it better in terms of encapsulation. What changes will you make to this class?
A square (google translation: "a four sided figure") which can have any form: trapeze, rectangle, quadrad. But according to the code (lineĀ²) it is at least a rectangle (could be a quadrad as well).

Now you're asked to refactor it to "make it better in terms of encapsulation".

This means that the side and area fields should be private and a setter on area has to be added. Everything else would go beyond the requirements and is therefore an invalid refactoring.

This means that #3 and #6 are the correct answers.

Right?

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

Posted: Tue Dec 28, 2021 12:21 am
by admin
Not sure where you saw this definition but I think everyone who has gone past grade school understands that Square is a quadrilateral with 4 equal sides and each angle is 90 degrees. Here is what wikipedia say: https://en.wikipedia.org/wiki/Square

The code given in the question also indicates the same because there is only one field named side. So, there is no place to store values for the lengths or angles of other sides. So, it is clear what a Square means here.

Yes, as the explanation notes, "There can be multiple ways to accomplish this. The exam asks you questions on the similar pattern." So, depending on how you interpret the requirements, you can decide which options you think should be correct in the exam.