Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Point.java Cloud.java Description This program deals with Points, that are elements of a Cloud (a set of Points). Implement these classes in a step-wise (one

Point.java

Cloud.java

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.

Here is the result of running Cloud, when debug is OFF.

The main method, again, does an initial test of the Cloud implementation.

Submitting Your Assignment

Put your name in the Point and Cloud classes. If you used a debug flag, make sure you turn it to false before submitting.

Submit one file P9.jar, containing your Cloud.java and Point.java. To create a jar file in Eclipse, select Export from the File menu, then under wizards select Java / JAR as the wizard type. Choose the project (if necessary), uncheck Export generated class and check Export Java source files. See below:

image text in transcribed

Point.Java

 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"); } //TODO Implement Point.Point() /** * Point() creates the origin by appropriately calling * the general Point constructor */ public Point(){ System.out.println("Point() not implemented yet"); } //TODO Implement Point.getX /** * @return x */ public double getX(){ System.out.println("getX not implemented yet"); return 0.0; } //TODO Implement Point.getY /** * @return y */ public double getY(){ System.out.println("getY not implemented yet"); return 0.0; } // 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"); 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){ 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"); return 0.0; } /** * @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)); } }

Cloud.Java

import java.awt.Component; import java.util.ArrayList; import java.util.Arrays; /* Insert your name here */ public class Cloud { 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"); return false; } //TODO Implement Cloud.size /** * @return number of points in the cloud */ public int size(){ System.out.println("size not implemented yet"); return -1; } //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"); 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"); } //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(){ System.out.println("centerP not implemented yet"); return null; } //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) { 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(); 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); } }
JAR Export JAR File Specification Define which resources should be exported into the JAR. Select the resources to export: ??.classpath ??.project urve > 1nheritCartesian InheritHiptobeSquare > InheritWords ? Recap stuff ? Export generated class files and resources Export all output folders for checked projects Export Java source files and resources ? Export refactorings for checked projects. Select refactorings." Select the export destination: JAR file: CUsers waker\Documents\Education CS161 F16 P2.jar Options: Browse... Compress the contents of the JAR file ? Add directory entries Overwrite existing files without warning Back Next > Finish Cancel

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

Database And Expert Systems Applications 31st International Conference Dexa 2020 Bratislava Slovakia September 14 17 2020 Proceedings Part 1 Lncs 12391

Authors: Sven Hartmann ,Josef Kung ,Gabriele Kotsis ,A Min Tjoa ,Ismail Khalil

1st Edition

303059002X, 978-3030590024

More Books

Students also viewed these Databases questions

Question

Understand the different approaches to job design. page 167

Answered: 1 week ago