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

public class Square { public Point( float x, float y ); public float getX(); pub

ID: 3540964 • Letter: P

Question

public class Square {

   public Point( float x, float y );

   public float getX();

   public float getY();

   public float distanceTo( Point p );  // returns the distance

                                        // to Point p

   private float x;    // x coordinate

   private float y;    // y coordinate

   }

Using the above Point class, post the code for a class that represents some polygon (right triangle, equilateral triangle, isosceles triangle, general triangle, square, rhombus, parallelogram, rectangle, trapezoid, general quadrilateral, pentagon, etc.)   Your class should contain private data fields of type Point

Explanation / Answer

import java.util.*;

class Point
{
public Point( float x, float y )
{
this.x = x;
this.y = y;
}
public float getX()
{
return x;
}
public float getY()
{
return y;
}
public float distanceTo( Point p ) // returns the distance // to Point p
{
return (float) (Math.sqrt((p.x-x)*(p.x-x) + (p.y-y)*(p.y-y)));
}
private float x; // x coordinate
private float y; // y coordinate
}

class general_triangle
{
private Point P1;
private Point P2;
private Point P3;
public general_triangle(Point x,Point y,Point z)
{
P1=x;
P2=y;
P3=z;
}
public float perimeter()
{
double dist_a = P1.distanceTo(P2);
double dist_b = P2.distanceTo(P3);
double dist_c = P3.distanceTo(P1);
return (float)(dist_a+dist_b+dist_c);
}
}

class rectangle
{
private Point P1;
private Point P2;
private Point P3;
private Point P4;
public rectangle(Point a,Point b,Point c,Point d)
{
P1 = a;
P2 = b;
P3 = c;
P4 = d;
}
public float perimeter()
{
double dist_a = P1.distanceTo(P2);
double dist_b = P2.distanceTo(P3);
return (float)(2.0*(dist_a+dist_b));
}
}

public class polygon_tester
{
public static void main(String[] args)
{
rectangle R1 = new rectangle(new Point(0,0),new Point(0,5), new Point(5,5), new Point(5,0));
System.out.println("Rectangle R1 perimeter given by :" + R1.perimeter());
general_triangle GT1 = new general_triangle(new Point(0,0),new Point(0,5), new Point(5,0));
System.out.println("General_triangle GT1 perimeter given by :" + GT1.perimeter());
}
}