About Question enthuware.ocajp.i.v8.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
lenach87
Posts: 2
Joined: Tue Aug 23, 2016 8:01 am
Contact:

About Question enthuware.ocajp.i.v8.2.876

Post 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

Paulus
Posts: 5
Joined: Fri Jul 15, 2016 8:55 am
Contact:

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

Post 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.

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

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

Post 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.
If you like our products and services, please help us by posting your review here.

lenach87
Posts: 2
Joined: Tue Aug 23, 2016 8:01 am
Contact:

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

Post by lenach87 »

Thank you for the answers!

st.lisker
Posts: 22
Joined: Sat Jun 30, 2018 6:11 am
Contact:

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

Post by st.lisker »

Hello. Is "Refactoring" means that all options will be applied together? Not independently of each other as usual?

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

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

Post 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.
If you like our products and services, please help us by posting your review here.

st.lisker
Posts: 22
Joined: Sat Jun 30, 2018 6:11 am
Contact:

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

Post 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?

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

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

Post 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.
If you like our products and services, please help us by posting your review here.

st.lisker
Posts: 22
Joined: Sat Jun 30, 2018 6:11 am
Contact:

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

Post 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.

ignore.your
Posts: 3
Joined: Thu Dec 23, 2021 5:26 pm
Contact:

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

Post 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?

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

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

Post 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.
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 30 guests