The document defines a Point class with methods to initialize points, get x and y coordinates, calculate distance between points, and check for equality. It also defines a Cloud class to represent a collection of points with methods to add/remove points, get size and extremes, and calculate the center point and minimum distance between points in the cloud. Both classes are tested with example points in a main method.
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
public class Point { Insert your name here private dou.pdf
1. 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
2. /**
* 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)
3. */
public boolean equals(Object obj) {
if (obj instanceof Point) {
Point p = (Point) obj;
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);
4. 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:
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) does not 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
import java.util.ArrayList;
public class Cloud {
5. private ArrayList points;
private boolean debug = false;
/**
* Given Constructor
*/
public Cloud() {
points = new ArrayList();
}
public void setDebug(Boolean debug) {
this.debug = debug;
}
// TODO Implement Cloud.isEmpty
/**
* return whether cloud is empty or not
*/
public boolean isEmpty() {
// System.out.println("isEmpty not implemented yet");
if (this.points.size() == 0)
return true;
else
return false;
}
// TODO Implement Cloud.size
/**
* return number of points in the cloud
*/
public int size() {
// System.out.println("size not implemented yet");
return this.points.size();
}
// TODO Implement Cloud.hasPoint
/**
*
* param p a Point return whether p in the cloud
*/
public boolean hasPoint(Point p) {
6. // System.out.println("hasPoint not implemented yet");
for (int i = 0; i < points.size(); i++) {
Point point = points.get(i);
if (p.equals(point))
return true;
}
return false;
}
// TODO Implement Cloud.addPoint
/**
*
* param p if p not in points, add p ***at the end*** of points (to keep
* same order)
*/
public void addPoint(Point p) {
// System.out.println("addPoint not implemented yet");
points.add(p);
}
// Given Cloud.toString
public String toString() {
return points.toString();
}
// TODO Implement Cloud.extremes
/**
*
* return an array of doubles: left, right, top, and bottom of points, null
* for empty cloud
*/
public double[] extremes() {
System.out.println("extremes not implemented yet");
return null;
}
// TODO Implement Cloud.centerP
/**
*
* return: if (cloud not empty) create and return the center point else
7. * null;
*/
public Point centerP() {
System.out.println("centerP not implemented yet");
Point center = new Point();
double sumX = 0, sumY = 0;
for (int i = 0; i < points.size(); i++) {
Point point = points.get(i);
sumX += point.getX();
sumY += point.getY();
}
return new Point(sumX / points.size(), sumY / points.size());
}
// TODO Implement Cloud.minDist
/**
*
* return minimal distance between 2 points in the cloud 0.0 for a cloud
* with less than 2 points
*/
public double minDist() {
System.out.println("minDist not implemented yet");
return 0.0;
}
// TODO Implement Cloud.crop
/**
*
* param p1 param p2
*
* all Points outside the rectangle, line or point spanned by p1 and p2 are
* removed
*
*/
public void crop(Point p1, Point p2) {
System.out.println("minDist not implemented yet");
}
/**
8. * param args:no args
*/
public static void main(String[] args) {
Cloud cloud = new Cloud();
cloud.setDebug(false);
System.out.println("cloud.debug OFF");
System.out.println("cloud: " + cloud);
if (!cloud.isEmpty())
System.out.println("Error: cloud should be empty!");
if (cloud.extremes() != null)
System.out.println("Error: extremes should be null!");
if (cloud.minDist() != 0.0)
System.out.println("Error: minDist should return 0.0!");
Point p31 = new Point(3.0, 1.0);
cloud.addPoint(p31);
Point p22 = new Point(2.0, 2.0);
cloud.addPoint(p22);
Point p42 = new Point(4.0, 2.0);
cloud.addPoint(p42);
Point p33 = new Point(3.0, 3.0);
cloud.addPoint(p33);
System.out.println("cloud 1: " + cloud);
System.out.println("center point in cloud: " + cloud.centerP());
System.out.println("cloud: " + cloud);
System.out.println("cloud 2: " + cloud);
Point p77 = new Point(7, 7);
if (cloud.hasPoint(p77))
System.out.println("Error: point " + p77
+ " should not be in cloud!");
else
System.out.println("OK: point " + p77 + " is not in cloud");
double[] extrs = cloud.extremes();
if (extrs != null) {
System.out.println("left: " + extrs[0]);
System.out.println("right: " + extrs[1]);
System.out.println("top: " + extrs[2]);
9. System.out.println("bottom: " + extrs[3]);
}
double minD = cloud.minDist();
System.out.printf("min dist in cloud: %5.3f ", minD);
System.out.println("Test cloud with 1 point");
Cloud cloud1 = new Cloud();
Point p = new Point();
cloud1.addPoint(p);
minD = cloud1.minDist();
System.out.printf("min dist in cloud1: %5.3f ", minD);
}
}
OUTPUT:
cloud.debug OFF
cloud: []
extremes not implemented yet
minDist not implemented yet
cloud 1: [(3.0,1.0), (2.0,2.0), (4.0,2.0), (3.0,3.0)]
centerP not implemented yet
center point in cloud: (3.0,2.0)
cloud: [(3.0,1.0), (2.0,2.0), (4.0,2.0), (3.0,3.0)]
cloud 2: [(3.0,1.0), (2.0,2.0), (4.0,2.0), (3.0,3.0)]
OK: point (7.0,7.0) is not in cloud
extremes not implemented yet
minDist not implemented yet
min dist in cloud: 0.000
Test cloud with 1 point
minDist not implemented yet
min dist in cloud1: 0.000
NOTE:
Not enough time to complete the the question
Solution
public class Point {
/* Insert your name here */
10. 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
*/
11. 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 obj) {
if (obj instanceof Point) {
12. Point p = (Point) obj;
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);
13. 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:
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) does not 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
import java.util.ArrayList;
public class Cloud {
private ArrayList points;
private boolean debug = false;
/**
14. * Given Constructor
*/
public Cloud() {
points = new ArrayList();
}
public void setDebug(Boolean debug) {
this.debug = debug;
}
// TODO Implement Cloud.isEmpty
/**
* return whether cloud is empty or not
*/
public boolean isEmpty() {
// System.out.println("isEmpty not implemented yet");
if (this.points.size() == 0)
return true;
else
return false;
}
// TODO Implement Cloud.size
/**
* return number of points in the cloud
*/
public int size() {
// System.out.println("size not implemented yet");
return this.points.size();
}
// TODO Implement Cloud.hasPoint
/**
*
* param p a Point return whether p in the cloud
*/
public boolean hasPoint(Point p) {
// System.out.println("hasPoint not implemented yet");
for (int i = 0; i < points.size(); i++) {
Point point = points.get(i);
15. if (p.equals(point))
return true;
}
return false;
}
// TODO Implement Cloud.addPoint
/**
*
* param p if p not in points, add p ***at the end*** of points (to keep
* same order)
*/
public void addPoint(Point p) {
// System.out.println("addPoint not implemented yet");
points.add(p);
}
// Given Cloud.toString
public String toString() {
return points.toString();
}
// TODO Implement Cloud.extremes
/**
*
* return an array of doubles: left, right, top, and bottom of points, null
* for empty cloud
*/
public double[] extremes() {
System.out.println("extremes not implemented yet");
return null;
}
// TODO Implement Cloud.centerP
/**
*
* return: if (cloud not empty) create and return the center point else
* null;
*/
public Point centerP() {
16. System.out.println("centerP not implemented yet");
Point center = new Point();
double sumX = 0, sumY = 0;
for (int i = 0; i < points.size(); i++) {
Point point = points.get(i);
sumX += point.getX();
sumY += point.getY();
}
return new Point(sumX / points.size(), sumY / points.size());
}
// TODO Implement Cloud.minDist
/**
*
* return minimal distance between 2 points in the cloud 0.0 for a cloud
* with less than 2 points
*/
public double minDist() {
System.out.println("minDist not implemented yet");
return 0.0;
}
// TODO Implement Cloud.crop
/**
*
* param p1 param p2
*
* all Points outside the rectangle, line or point spanned by p1 and p2 are
* removed
*
*/
public void crop(Point p1, Point p2) {
System.out.println("minDist not implemented yet");
}
/**
* param args:no args
*/
public static void main(String[] args) {
17. Cloud cloud = new Cloud();
cloud.setDebug(false);
System.out.println("cloud.debug OFF");
System.out.println("cloud: " + cloud);
if (!cloud.isEmpty())
System.out.println("Error: cloud should be empty!");
if (cloud.extremes() != null)
System.out.println("Error: extremes should be null!");
if (cloud.minDist() != 0.0)
System.out.println("Error: minDist should return 0.0!");
Point p31 = new Point(3.0, 1.0);
cloud.addPoint(p31);
Point p22 = new Point(2.0, 2.0);
cloud.addPoint(p22);
Point p42 = new Point(4.0, 2.0);
cloud.addPoint(p42);
Point p33 = new Point(3.0, 3.0);
cloud.addPoint(p33);
System.out.println("cloud 1: " + cloud);
System.out.println("center point in cloud: " + cloud.centerP());
System.out.println("cloud: " + cloud);
System.out.println("cloud 2: " + cloud);
Point p77 = new Point(7, 7);
if (cloud.hasPoint(p77))
System.out.println("Error: point " + p77
+ " should not be in cloud!");
else
System.out.println("OK: point " + p77 + " is not in cloud");
double[] extrs = cloud.extremes();
if (extrs != null) {
System.out.println("left: " + extrs[0]);
System.out.println("right: " + extrs[1]);
System.out.println("top: " + extrs[2]);
System.out.println("bottom: " + extrs[3]);
}
double minD = cloud.minDist();
18. System.out.printf("min dist in cloud: %5.3f ", minD);
System.out.println("Test cloud with 1 point");
Cloud cloud1 = new Cloud();
Point p = new Point();
cloud1.addPoint(p);
minD = cloud1.minDist();
System.out.printf("min dist in cloud1: %5.3f ", minD);
}
}
OUTPUT:
cloud.debug OFF
cloud: []
extremes not implemented yet
minDist not implemented yet
cloud 1: [(3.0,1.0), (2.0,2.0), (4.0,2.0), (3.0,3.0)]
centerP not implemented yet
center point in cloud: (3.0,2.0)
cloud: [(3.0,1.0), (2.0,2.0), (4.0,2.0), (3.0,3.0)]
cloud 2: [(3.0,1.0), (2.0,2.0), (4.0,2.0), (3.0,3.0)]
OK: point (7.0,7.0) is not in cloud
extremes not implemented yet
minDist not implemented yet
min dist in cloud: 0.000
Test cloud with 1 point
minDist not implemented yet
min dist in cloud1: 0.000
NOTE:
Not enough time to complete the the question