Question: Please help me with my code for Point.java 1. public void crop(Point p1, Point p2) { (from cloud.java) 2.public double[] extremes() { 3. public double

Please help me with my code for Point.java

1. public void crop(Point p1, Point p2) { (from cloud.java)

2.public double[] extremes() {

3. public double minDist() {

Thank you!

Description

This program deals with Points, that are elements of a Cloud (a set of Points). Implement these classes in a step-wise (one method at the time) fashion: first you implement Point, and test it using the provided main method of Point, then implement Cloud, and test it using the provided main method of Cloud. It is a good idea to create more test cases in your mains. Put your name in a comment at the top of your Point and Cloud classes.

Point

A Point in a two dimensional plane has double x and double y coordinates. Because double comparisons cannot be based on exact equality, the Point class has a

 public static final double EPSILON = 1e-5; 

The static final modifier indicates that the value epsilon is constant and shared by all Point objects. To access it, use Point.EPSILON. Compare this to e.g. the Math.PI constant in the class Math. The provided toString method produces a String (x,y), eg "(0.0,0.0)".

Stepwise implement the following methods:

Point(x,y) (Constructor): sets the values of the provided instance variables x and y.

Point() (Constructor): creates an origin (0.0,0.0) by calling the Point(x,y) constructor using this.

getX and getY: return the value of their respective instance variables.

equals: checks for equality of two Points. Because x and y are doubles, Equals compares x and p.x by checking whether the absolute difference of the two is less than the provided epsilon value, and does the same for y and p.y.

euclidDistance: computes the Euclidian distance between this and another point: the square root of the sum of (x - p.x) squared and (y - p.y) squared.

The main method is provided and will do an initial test of your Point implementation. Add more tests to your main method.

Cloud

When you are happy with Point, move on to Cloud. Clouds have an ArrayList points, instance variable that will hold the points of the Cloud. The Cloud (Constructor) and toString method are provided, so we all print in the same format. Stepwise implement the following methods:

isEmpty: checks if the cloud is empty.

size: the number of elements in the cloud.

hasPoint: uses the equals method of Point to check whether a given Point occurs in the Cloud.

addPoint: if the Point is not in the cloud, adds the Point to the Cloud ***to the end*** of points, again so that we all print the same results because we keep the same order.

extremes: returns null if the Cloud is empty, and an array of x and y coordinate doubles: leftmost, rightmost, top, bottom ***in that order*** otherwise. For example, if the cloud contains [ (0.0,0.0), (1.0,1.0), (2.0,0.0), (1.0,-1.0) ], the result array is [0.0, 2.0, 1.0, -1.0].

centerP: creates a center Point (average x and average y of the Cloud).

minDist: returns 0.0 if the Cloud is empty or has one element, and the minimal distance between two Points, otherwise.

crop: has parameters that are two Points in the Cloud. One of these two points is a bottom corner, and the other a top corner, of a rectangle. The top corner is diagonally across from the bottom corner. Crop will remove all points outside this rectangle from the Cloud much like you would crop an image. The crop method must deal with two input points on a horizontal or vertical line segment, in which case all points not on the line segment are removed, and it must deal with two equal Points p1 and p2, in which case all Points but p1 (if is exists in the cloud) are removed from the Cloud.

For example, if the two input Points are (0.0,0,0) and (1.0,1.0), all Points outside the square delimited by (0.0,0.0), (0.0,1.0), (1.0,1.0), and (0.0,1.0) are removed, but if the two input Points are (0.0,0,0) and (0.0,1.0), all Points outside the line segment delimited by (0.0,0.0), and (0.0,1.0) are removed.

public class Point {

/* Insert your name here */

private double x; private double y; public static final double EPSILON = 1e-5; public static boolean debug = false; //TODO Implement Point.Point(double x, double y) /** instantiate a point * "this.x" refers to the instance variable of the object * x refers to the parameter * same for this.y and y */ public Point(double x, double y) { // System.out.println("Point(x,y) not implemented yet"); this.x = x; this.y = y; } //TODO Implement Point.Point() /** * Point() creates the origin by appropriately calling * the general Point constructor */ public Point() { // System.out.println("Point() not implemented yet"); this.x = 0; this.y = 0; } //TODO Implement Point.getX /** * @return x */ public double getX() { // System.out.println("getX not implemented yet"); return this.x; } //TODO Implement Point.getY /** * @return y */ public double getY() { // System.out.println("getY not implemented yet"); return this.y; } // Given Point.toString /** * convert to String */ public String toString() { return "(" + x + "," + y + ")"; } //TODO Implement Point.equals /** * * @param p other point * @return test for equality using epsilon, because we * are dealing with doubles,so roundoff can occur */ public boolean equals(Point p) { // System.out.println("equals not implemented yet"); if (p.getX() == this.getX() && p.getY() == this.getY()) return true; else return false; } // Given equals(Object o) /** * We need this equals method for ArrayList, because * the generic ArrayList is really an ArrayList of Object. * In the case of equals, the signature is * public boolean equals(Object o) * and the method below overrides the Object equals method * and the calls the class's equals(Point) method * * @see java.lang.Object#equals(java.lang.Object) */

public boolean equals(Object o) { if (o instanceof Point) { Point p = (Point) o ; return equals(p); } return false; } //TODO Implement Point.euclidDist /** * * @param p * @return Euclidean distance of this point to point p */ public double euclidDist(Point p) { // System.out.println("euclidDist not implemented yet"); double X = this.getY() - p.getY(); double Y = this.getX() - p.getX(); double result = Math.sqrt(X * X + Y * Y); return result; } /** * @param args: no args */ public static void main(String[] args) { // test all methods if (debug) System.out.println("debug ON"); else System.out.println("debug OFF"); System.out.println("EPSILON: " + Point.EPSILON); Point origin = new Point(); Point p1 = new Point(0.0, 4.0); Point p2 = new Point(3.0000001, 3.9999999); Point p3 = new Point(3.0, 4.0); Point p4 = new Point(0.0, 5.0); Point p5 = new Point(12.0, 0.0); System.out.println("origin: " + origin); System.out.println("p1: " + p1); System.out.println("p2: " + p2); System.out.println("p3: " + p3); System.out.println("p4: " + p4); System.out.println("p5: " + p5); if (p2.equals(p3)) System.out.println(p2 + " equals " + p3); else System.out.println(p2 + " does not equal " + p3); System.out.println("Euclidean distance between " + origin + " and " + p1 + ": " + origin.euclidDist(p1)); System.out.println("Euclidean distance between " + p1 + " and " + p3 + ": " + p1.euclidDist(p3)); System.out.println("Euclidean distance between " + p3 + " and " + origin + ": " + p3.euclidDist(origin)); System.out.println("Euclidean distance between " + p4 + " and " + p5 + ": " + p4.euclidDist(p5)); } }

OUTPUT should be:

debug OFF EPSILON: 1.0E-5 origin: (0.0,0.0) p1: (0.0,4.0) p2: (3.0000001,3.9999999) p3: (3.0,4.0) p4: (0.0,5.0) p5: (12.0,0.0) (3.0000001,3.9999999) equal (3.0,4.0) Euclidean distance between (0.0,0.0) and (0.0,4.0): 4.0 Euclidean distance between (0.0,4.0) and (3.0,4.0): 3.0 Euclidean distance between (3.0,4.0) and (0.0,0.0): 5.0 Euclidean distance between (0.0,5.0) and (12.0,0.0): 13.0

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock 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 Databases Questions!