About Question enthuware.ocajp.i.v7.2.844 :
Moderator: admin
-
- Site Admin
- Posts: 10391
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.844 :
You are getting an exception because you are trying to modify the list while you are iterating through it. Remove the for loop:
ArrayList<Integer> result = asd.getScores();
result.add(100);
ArrayList<Integer> result = asd.getScores();
result.add(100);
-
- Posts: 35
- Joined: Sat Nov 25, 2017 4:13 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.844 :
Hi there,I want to ask how is Make computeAverage() public not right?Methods should normally be public in order to be accessible anywhere,right? Or In this case,It doesn't even matter since there is only one Class?
-
- Site Admin
- Posts: 10391
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.844 :
Methods need to be public only if you want them to be accessed from non-subclass classes outside the package. A default access method can be accessed from any class within the same package. So it is not always necessary for a method to be public.
-
- Posts: 5
- Joined: Tue Feb 06, 2018 2:41 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.844 :
I wouldn't completely agree with the correct answer 4.
There's a big :What if I want to change this student's scores and recompute the average? The integrity of the Student object is fine as long as I can't directly access the list reference and possibly null it causing computeAverage() to fail completely.
Also, what's the point of this whole code if scores isn't set or filled anywhere? The 4th answer is really stretching it.
If 4 were irrefutably the most correct answer the Student object would be one big bunker with no doors. I don't see the use in that.
There's a big :What if I want to change this student's scores and recompute the average? The integrity of the Student object is fine as long as I can't directly access the list reference and possibly null it causing computeAverage() to fail completely.
Also, what's the point of this whole code if scores isn't set or filled anywhere? The 4th answer is really stretching it.
If 4 were irrefutably the most correct answer the Student object would be one big bunker with no doors. I don't see the use in that.
-
- Site Admin
- Posts: 10391
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.844 :
Your argument is correct , but the question only asks you to improve the encapsulation aspect of the class, not the "usefulness" of the class. One could certainly make the class useful by applying other changes that are not relevant to this question but while letting the class be well encapsulated.
-
- Posts: 10
- Joined: Fri Mar 06, 2020 5:18 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.844 :
But what I still don't understand is why we should not 'Make computeAverage() public', since the goal of encapsulation is 1. Declare the variables of a class as private and 2. Provide public setter and getter methods to modify and view the variables values. And is computeAverage() not a 'setter'?
-
- Site Admin
- Posts: 10391
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.844 :
No, computeAverage is neither a getter nor a setter! It does not start with a get(or is) or set. "computeAverage" is not a property of this class. "average" is and there is a getter for that already.
-
- Posts: 13
- Joined: Thu May 16, 2024 5:30 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.844 :
Why is "Make getScores() protected." not a valid answer? It would do as much or more than the last option in terms of preventing other classes from modifying scores.
-
- Site Admin
- Posts: 10391
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.844 :
The question asks you to improve encapsulation. Making getScores protected makes no difference. scores is a property of the class and it is correctly exposed using a getScores method. The issue is with the value returned by this method. Returning the internal data member allows other classes to modify its contents. This issue will not be fixed by making getScores protected (option 3)
The last option returns a copy of the scores, which makes sure that other classes cannot modify the scores of Student object. This is much better than option 3.
The last option returns a copy of the scores, which makes sure that other classes cannot modify the scores of Student object. This is much better than option 3.
-
- Posts: 13
- Joined: Thu May 16, 2024 5:30 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.844 :
Thank you for the response.admin wrote: ↑Thu May 16, 2024 10:21 pmThe question asks you to improve encapsulation. Making getScores protected makes no difference. scores is a property of the class and it is correctly exposed using a getScores method. The issue is with the value returned by this method. Returning the internal data member allows other classes to modify its contents. This issue will not be fixed by making getScores protected (option 3)
The last option returns a copy of the scores, which makes sure that other classes cannot modify the scores of Student object. This is much better than option 3.
(Edit: never mind - I just learned that protected allows non-child classes to access a field/method, as long as they're in the same package.)
That's not my argument that the other option should be correct - that's just my interpretation when I read the question. My question is how I can identify that returning a copy is the answer that I'm expected to provide in this case?
-
- Site Admin
- Posts: 10391
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.844 :
> My question is how I can identify that returning a copy is the answer that I'm expected to provide in this case?
There are two ways to arrive at this answer:
1. You have to select two options. Do you see any option that is better than this?
2. Encapsulation is about preventing direct access to data members because they are considered "implementation detail" of the class. In this case, allows others to mess with the elements of scores List directly will cause the average property to be inconsistent with the business logic. Therefore, returning a copy is desirable.
There are two ways to arrive at this answer:
1. You have to select two options. Do you see any option that is better than this?
2. Encapsulation is about preventing direct access to data members because they are considered "implementation detail" of the class. In this case, allows others to mess with the elements of scores List directly will cause the average property to be inconsistent with the business logic. Therefore, returning a copy is desirable.
Who is online
Users browsing this forum: No registered users and 4 guests