Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1 0verview For this assignment, you are asked to implement a class that represen

ID: 3857524 • Letter: 1

Question

1 0verview For this assignment, you are asked to implement a class that represents polygon and a class that represents a point. Each polygon consists of a list of points. 2 Requirements The Point class should contains theh following constructor and methods. 1. Point (int x, int y) that constructs a point from x and y coordinates and stores the coordinates in the fields x and y respectively. 2. Point (int) xy) that constructor a point from an integer array of length 3. public String toString) that returns the string representation of a 4. public double distance (Point that) that computes the Euclidean dis- The Polygon class should contains the following constructor and methods. 1. Polygon (PointlI points) that constructs a polygon from an array of 2. Polygon (int [[ data) that coustructs a polygon from a double array 3. public String toString) that returns the string representation of a i. private double distance (Point point) that computes the minium 2. point. tance between this and that point. points and store the array in a field points. data where each element of data is used to construct a point. polygon. For example, t(o,o), (1, 0), (o, 1)1 is a triangle. distance from each poiit of this polygon to the argument point.

Explanation / Answer

//Dear student,

// name the file as driver.java

//compile it

class Point{

int x;

int y;

Point(int x, int y){

this.x = x;

this.y = y;

}

Point(int[] xy){

this.x = xy[0];

this.y = xy[1];

}

public String toString(){

String str = "(" + x + " , " + y + ")";

return str;

}

public double distance(Point that){

double distance1;

distance1 = Math.pow((this.x - that.x),2) + Math.pow((this.y - that.y),2);

distance1 = Math.sqrt(distance1);

return distance1;

}

}

class Polygon{

Point[] points;

Polygon(Point[] points){

points = new Point[points.length];

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

this.points[i] = points[i];

}

}

Polygon(int[][] data){

points = new Point[data.length];

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

Point temp = new Point(data[i]);

points[i] = temp;

}

}

public String toString(){

StringBuffer str1 = new StringBuffer("[");

for(int i1 = 0; i1<this.points.length; i1++){

str1.append(points[i1].toString());

str1.append(",");

}

str1.append("]");

String str = str1.toString();

return str;

}

private double distance(Point point){

double a;

a = this.points[0].distance(point);

for(int p = 1; p<this.points.length; p++){

if(this.points[p].distance(point) < a){

a = this.points[p].distance(point);

}

}

return a;

}

public double distance(Polygon that){

double a1;

a1 = this.distance(that.points[0]);

for(int q = 1; q<that.points.length; q++){

if(this.distance(that.points[q])<a1){

a1 = this.distance(that.points[q]);

}

}

return a1;

}

public static double distance(Polygon p1, Polygon p2){

return p1.distance(p2);

}

public static void printLargestDistance(Polygon[] polygon){

double a;

int position1;

int position2;

a = Polygon.distance(polygon[0],polygon[1]);

position1 = 0;

position2 = 1;

for(int n = 0; n<polygon.length; n++){

for(int o = n; o<(polygon.length); o++){

if(Polygon.distance(polygon[n],polygon[o])>a){

a = Polygon.distance(polygon[n],polygon[o]);

position1 = n;

position2 = o;

}

}

}

for(int a1_1=0; a1_1<polygon.length; a1_1++){

if(a1_1 == position1 || a1_1 == position2){

System.out.print("*");

}

System.out.println(polygon[a1_1].toString());

}

}

}

public class driver{

public static void main(String[] args) {

int[][][] data ={

{{0,0},{0,1},{1,1}},

{{2,2},{2,3},{3,3},{3,2}},

{{-1,-1},{0,-1},{-1,0}},

{{-1,-2},{-2,-2},{-2,-1}},

{{1,1},{-1,2},{3,2}}

};

Polygon[] polygon = new Polygon[data.length];

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

Polygon temp = new Polygon(data[i]);

polygon[i] = temp;

}

Polygon.printLargestDistance(polygon);

}

}