About Question enthuware.ocajp.i.v7.2.844 :
Moderator: admin
-
- Site Admin
- Posts: 10053
- 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);
If you like our products and services, please help us by posting your review here.
-
- 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: 10053
- 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.
If you like our products and services, please help us by posting your review here.
-
- 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: 10053
- 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.
If you like our products and services, please help us by posting your review here.
-
- 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: 10053
- 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.
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 45 guests