Question: I need assistance with this constructor(Polygon(Point[]_points)). I have made an attempt at the question. public class Point { public int x, y; /** * DO

I need assistance with this constructor(Polygon(Point[]_points)). I have made an attempt at the question.

public class Point {

public int x, y;

/**

* DO NOT MODIFY

* @param x

* @param y

*/

public Point(int x, int y) {

this.x = x;

this.y = y;

}

/**

* DO NOT MODIFY

*/

public String toString() {

return "("+x+","+y+")";

}

public class Polygon {

public Point[] points;

/**

*

* @param _points

*

* Populate the instance variable array points with items of _points.

*

* See the test data to determine null case scenario, etc.

*

* Make sure all objects (including arrays) copying is done using instance copy, not reference copy.

*/

public Polygon(Point[] _points) {

Polygon p1 = new Polygon(_points);

Polygon p2 = new Polygon(p1);

}

@Test @Order(2)

public void testPolygonConstructor2() {

Point[] src = null;

Polygon p = new Polygon(src);

assertNotNull(p);

assertNotNull(p.points);

assertEquals(0, p.points.length);

src = new Point[5];

for(int i=0; i < src.length; i++) {

src[i] = new Point(2*i, (i+5)/3);

}

p = new Polygon(src);

assertNotNull(p);

assertNotNull(p.points);

assertEquals(5, p.points.length);

assertNotEquals(src, p.points); //no reference copies

for(int i=0; i < src.length; i++) {

assertNotNull(p.points[i]);

assertNotEquals(src[i], p.points[i]); //no reference copies

assertEquals(src[i].toString(), p.points[i].toString());

}

currentMethodName = new Throwable().getStackTrace()[0].getMethodName();

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

public class Polygon public Point points Constructs a new Polygon object with a deep copy of the pro... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!