Page 1 of 1
About Question enthuware.ocpjp.ii.v11.2.3400 :
Posted: Thu Aug 08, 2019 5:51 am
by BinSlayer
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?
Re: About Question enthuware.ocpjp.ii.v11.2.3400 :
Posted: Thu Aug 08, 2019 8:04 am
by admin
It is a mistake in the code. It should have setVertices.
Did you get our email that we sent out to you yesterday?
Re: About Question enthuware.ocpjp.ii.v11.2.3400 :
Posted: Thu Aug 08, 2019 8:30 am
by BinSlayer
Thanks for the answer. And no, I didn't receive an email.
Re: About Question enthuware.ocpjp.ii.v11.2.3400 :
Posted: Thu Aug 08, 2019 9:07 am
by admin
I sent you a private message just now.
Re: About Question enthuware.ocpjp.ii.v11.2.3400 :
Posted: Tue Jan 14, 2020 4:48 am
by kabanvau
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:
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 +
'}';
}
}
These lines mofiy the internals of shape:
Code: Select all
vertices[0].setX(99);
vertices[0].setY(99);
To solve the problem you need to make a deep copy of the array in the setter method. Something like this:
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);
}
Re: About Question enthuware.ocpjp.ii.v11.2.3400 :
Posted: Tue Jan 14, 2020 6:39 am
by admin
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.