About Question enthuware.ocpjp.ii.v11.2.3400 :

All the posts and topics that contain only an error report will be moved here after the error is corrected. This is to ensure that when users view a question in ETS Viewer, the "Discuss" button will not indicate the presence of a discussion that adds no value to the question.

Moderators: Site Manager, fjwalraven

Post Reply
BinSlayer
Posts: 10
Joined: Tue Jul 09, 2019 8:33 am
Contact:

About Question enthuware.ocpjp.ii.v11.2.3400 :

Post 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?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.ii.v11.2.3400 :

Post 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?
If you like our products and services, please help us by posting your review here.

BinSlayer
Posts: 10
Joined: Tue Jul 09, 2019 8:33 am
Contact:

Re: About Question enthuware.ocpjp.ii.v11.2.3400 :

Post by BinSlayer »

Thanks for the answer. And no, I didn't receive an email.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.ii.v11.2.3400 :

Post by admin »

I sent you a private message just now.
If you like our products and services, please help us by posting your review here.

kabanvau
Posts: 14
Joined: Thu Nov 21, 2019 5:48 am
Contact:

Re: About Question enthuware.ocpjp.ii.v11.2.3400 :

Post 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);
}

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.ii.v11.2.3400 :

Post 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.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 16 guests