About Question enthuware.ocpjp.ii.v11.2.3400 :
Moderators: Site Manager, fjwalraven
-
- Posts: 10
- Joined: Tue Jul 09, 2019 8:33 am
- Contact:
About Question enthuware.ocpjp.ii.v11.2.3400 :
Hi
As far as I can see one of the correct answers is the setter, but there isn't even a setter in the code to begin with so why it would be within the answers is beyond me.
Should I expect questions like this in the real exam, or is it simply a mistake in the code?
As far as I can see one of the correct answers is the setter, but there isn't even a setter in the code to begin with so why it would be within the answers is beyond me.
Should I expect questions like this in the real exam, or is it simply a mistake in the code?
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.ii.v11.2.3400 :
It is a mistake in the code. It should have setVertices.
Did you get our email that we sent out to you yesterday?
Did you get our email that we sent out to you yesterday?
If you like our products and services, please help us by posting your review here.
-
- Posts: 10
- Joined: Tue Jul 09, 2019 8:33 am
- Contact:
Re: About Question enthuware.ocpjp.ii.v11.2.3400 :
Thanks for the answer. And no, I didn't receive an email.
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.ii.v11.2.3400 :
I sent you a private message just now.
If you like our products and services, please help us by posting your review here.
-
- Posts: 14
- Joined: Thu Nov 21, 2019 5:48 am
- Contact:
Re: About Question enthuware.ocpjp.ii.v11.2.3400 :
1. Your Shape class will not compile. setVertices should set Point[] and not Points[]. The same applies to getVertices. It should return Point[].
2. I think it is not enough to clone input arrays (and collections) to prevent the calling code from changing the elements of the array. Here is my code:
These lines mofiy the internals of shape:
To solve the problem you need to make a deep copy of the array in the setter method. Something like this:
2. I think it is not enough to clone input arrays (and collections) to prevent the calling code from changing the elements of the array. Here is my code:
Code: Select all
public class Shape {
Point[] vertices;
public Shape(Point[] verts){ this.vertices = verts; }
public Shape() {}
public Point[] getVertices(){ return vertices; }
public void setVertices(Point[] vertices){
this.vertices = vertices.clone();
}
public static void main(String[] args) {
Point[] vertices ={ new Point(1,1), new Point(2,2)};
Shape shape = new Shape();
shape.setVertices(vertices);
// Modifies internals of shape
vertices[0].setX(99);
vertices[0].setY(99);
// Output: Point{x=99, y=99}
System.out.println(shape.getVertices()[0]);
}
}
class Point {
private Integer x = 0;
private Integer y = 0;
// Copy constructor
public Point(Point point) {
this(point.getX(), point.getY());
}
public Point(Integer x, Integer y) {
this.x = x;
this.y = y;
}
public Integer getX() {
return x;
}
public void setX(Integer x) {
this.x = x;
}
public Integer getY() {
return y;
}
public void setY(Integer y) {
this.y = y;
}
@Override
public String toString() {
return "Point{" +
"x=" + x +
", y=" + y +
'}';
}
}
Code: Select all
vertices[0].setX(99);
vertices[0].setY(99);
Code: Select all
public void setVertices(Point[] vertices) {
List<Point> list = Arrays.stream(vertices)
.map(Point::new)
.collect(Collectors.toList());
this.vertices = new Point[list.size()];
this.vertices = list.toArray(this.vertices);
}
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.ii.v11.2.3400 :
1. Yes, it should be Point[] not Points[]. Fixed. Thank you for your feedback!
2. As far as the Shape class is concerned, cloning the array is enough to make it immutable because the code for Point is not given and none of the options suggest that Point is mutable.
Whether a deep copy is required or not depends on business requirement and whether Point is immutable or not. From the perspective of the exam, the option is correct. Had the problem statement given any information about Point and if the question had any option such as "If Point is mutable, a deep copy/clone is required", then that would have been a correct option.
HTH,
Paul.
2. As far as the Shape class is concerned, cloning the array is enough to make it immutable because the code for Point is not given and none of the options suggest that Point is mutable.
Whether a deep copy is required or not depends on business requirement and whether Point is immutable or not. From the perspective of the exam, the option is correct. Had the problem statement given any information about Point and if the question had any option such as "If Point is mutable, a deep copy/clone is required", then that would have been a correct option.
HTH,
Paul.
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 8 guests