About Question enthuware.ocajp.i.v8.2.876
Moderator: admin
-
- Posts: 2
- Joined: Tue Aug 23, 2016 8:01 am
- Contact:
About Question enthuware.ocajp.i.v8.2.876
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
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
-
- Posts: 5
- Joined: Fri Jul 15, 2016 8:55 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.876
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.
-
- Site Admin
- Posts: 10045
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.876
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.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 2
- Joined: Tue Aug 23, 2016 8:01 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.876
Thank you for the answers!
-
- Posts: 22
- Joined: Sat Jun 30, 2018 6:11 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.876
Hello. Is "Refactoring" means that all options will be applied together? Not independently of each other as usual?
-
- Site Admin
- Posts: 10045
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.876
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.
-
- Posts: 22
- Joined: Sat Jun 30, 2018 6:11 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.876
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?
-
- Site Admin
- Posts: 10045
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.876
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.
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.
-
- Posts: 22
- Joined: Sat Jun 30, 2018 6:11 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.876
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.
-
- Posts: 3
- Joined: Thu Dec 23, 2021 5:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.876
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).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?
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?
-
- Site Admin
- Posts: 10045
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.876
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.
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.
Who is online
Users browsing this forum: No registered users and 59 guests