CS163/CS164 Programming Assignment 9
Classes, constructors and more
Due Monday, Oct. 30th at 6:00pm
Late Tuesday, Oct. 31st at 8:00am
In this assignment you will finish the implementation of two classes in a
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.
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:
The main method is provided and will do an initial test of your Point implementation. Add more tests to your main method.
- 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.
When you are happy with Point, move on to Cloud. Clouds have an ArrayList<Point> 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:
Here is the result of running Cloud, when debug is OFF.
The main method, again, does an initial test of the Cloud implementation.
- 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.
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: