Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Class name is Quadrilateral It has 4 private MyPoint object attributes, one for each corner of the quadrilateral (4 sided) shape. If you want to

Class name is Quadrilateral

It has 4 private MyPoint object attributes, one for each corner of the quadrilateral (4 sided) shape. If you want to you could use an array of MyPoints, it doesn't really matter because they are private.

Give it a no argument Constructor with default corner points (-1,1), (1,1), (1,-1), (-1,-1)

Include a Constructor that takes 4 MyPoint objects. Be aware that the Points may not be given is a usable order and the points will have to be adjusted as necessary.

Method named getArea() that returns the area.

Method named getPerimeter() that returns the total length of the perimeter.

Method named getCenter() that returns a MyPoint value for the center of the Quadrilateral object.

*hint on finding the intersection between two lines (make sure to include attribution to the source) https://tutorialspoint.dev/algorithm/geometric-algorithms/program-for-point-of-intersection-of-two-lines (Links to an external site.)

Method named getSides() that returns an array of doubles containing the length of each side in clockwise order, it does not matter which side is first..

Method named getCorners() that returns an array of MyPoints of the corner locations. the points will be in a order going clock wise (doesn't mater which corner is first). The order is not necessarily the same as was provided to the Constructor. make sure to deep copy the corner MyPoint objects before returning to make sure the Quadrilater Class is immutable and can not be changed using the returned MyPoint objects.

Method named contains(double x, double y) that takes a x and y doubles and returns if that location is inside the Quadrilateral.

Overloaded method named contains(MyPoint thing) that takes a MyPoint object and returns if that location is inside the Quadrilateral.

Overloaded method named contains(Quadrilateral thing) that takes another instance of a Quadrilateral object and returns if it is inside the calling Quadrilateral.

Method named overlaps(Quadrilateral thing) that returns true if the shapes overlap. Returns false if the other shape is completely inside or outside of the calling Quadrilateral instance.

Below should be the test to see if thee classes work

package geometrytest; import geometryclass.MyPoint; import geometryclass.Quadrilateral; public class QuadrilateralTest { public static void main(String[] args) { System.out.println(" Make a default Quadrilateral object using the no attribute constructor"); Quadrilateral quad1 = new Quadrilateral(); double area1 = quad1.getArea(); System.out.println("area[4.0]= " + area1); System.out.println("--- testing getPerimeter() ---------------------------------------"); double perimeter1 = quad1.getPerimeter(); System.out.println("getPerimeter[8.0]= " + perimeter1); System.out.println("--- testing getCenter() ---------------------------------------"); MyPoint cent1 = quad1.getCenter(); System.out.print("center[(-0.0,-0.0)]= "); printMyPointLn(cent1); System.out.println("--- testing getSides() ---------------------------------------"); double [] sides1 = quad1.getSides(); printSides(sides1); System.out.println("--- testing getCorners() ---------------------------------------"); MyPoint [] corners1 = quad1.getCorners(); printCorners(corners1); System.out.println("--- testing contains(points/coordinates) --------------------------"); boolean containCheck1 = quad1.contains(1.0, 1.0); System.out.println("contains point1[true]= " + containCheck1); MyPoint testPt1 = new MyPoint(1.0, 1.001); boolean containCheck2 = quad1.contains(testPt1); System.out.println("contains point2[false]= " + containCheck2); System.out.println("--- testing contains(Quadrilateral otherQuad) --------------------"); Quadrilateral quad3 = new Quadrilateral(new MyPoint(0.5,0.5), new MyPoint(0.5,0.5), new MyPoint(0.5,0.5), new MyPoint(0.5,0.5)); boolean containQuad3 = quad1.contains(quad3); System.out.println("contains quad3[true]= " + containQuad3); Quadrilateral quad4 = new Quadrilateral(new MyPoint(0.5,0.5), new MyPoint(3.5,0.5), new MyPoint(3.5,3.5), new MyPoint(0.5,3.5)); boolean containQuad4 = quad1.contains(quad4); System.out.println("contains quad4[false]= " + containQuad4); System.out.println("--- testing overlaps(Quadrilateral otherQuad) --------------------"); boolean ovrLap4 = quad1.overlaps(quad4); System.out.println("overlaps quad4[true]= " + ovrLap4); System.out.println("====== testing the constructor to catch illegal shapes and fix or default them ======"); Quadrilateral crossQuadsysmetric = new Quadrilateral(new MyPoint(1,1), new MyPoint(5,3), new MyPoint(5,1), new MyPoint(1,3)); printQuad(crossQuadsysmetric); Quadrilateral goodQuadsysmetric = new Quadrilateral(new MyPoint(1,1), new MyPoint(5,1), new MyPoint(5,3), new MyPoint(1,3)); printQuad(goodQuadsysmetric); Quadrilateral crossQuadskew = new Quadrilateral(new MyPoint(1,1), new MyPoint(6,4), new MyPoint(5,1), new MyPoint(1,3)); printQuad(crossQuadskew); Quadrilateral goodQuadskew = new Quadrilateral(new MyPoint(1,1), new MyPoint(5,1), new MyPoint(6,4), new MyPoint(1,3)); printQuad(goodQuadskew); System.out.println(); Quadrilateral concave1 = new Quadrilateral(new MyPoint(1,1), new MyPoint(3,5), new MyPoint(5,1), new MyPoint(3,3)); printQuad(concave1); } //----- Helper methods --------------------------------------- public static void printCorners(MyPoint [] cornersLst) { System.out.print("corners= "); for(int i = 0; i < 4; i++) { printMyPoint(cornersLst[i]); System.out.print(" "); } System.out.println(); } public static void printSides(double [] sideLst) { System.out.print("side lengths= "); for(int i = 0; i < 4; i++) { System.out.print(sideLst[i]); System.out.print(" "); } System.out.println(); } public static void printMyPointLn(MyPoint pnt) { System.out.println("("+ pnt.getX() + "," + pnt.getY() + ")"); } public static void printMyPoint(MyPoint pnt) { System.out.print("("+ pnt.getX() + "," + pnt.getY() + ")"); } public static void printQuad(Quadrilateral inQuad) { MyPoint[] points = inQuad.getCorners(); System.out.print("corners= "); for (int i = 0; i < 4; i++) { printMyPoint(points[i]); System.out.print(" "); } System.out.println(); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions