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
Step: 1
public class Polygon public Point points Constructs a new Polygon object with a deep copy of the pro...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started