In: Computer Science
1. Design a Java CartesianPoint class for a Cartesian Point which implements both cloneable and comparable interfaces
The class should have the following private member variables:
• the x value of a point: int
• the y value of a point: int
and the class should have the following public member functions:
• default constructor which initializes the point to the origin of the Cartesian coordinate system
• explicit constructor which initializes the point to a pair of given value as (u, v) • setX() function, which sets the x value to a new value v
• setY()function, which sets the y value to a new value u
• whereX() function, which returns the x value
• whereY() function, which returns the y value
• Overrided compareTo()method: use the distance to the coordinate system’s origin for comparison
• Overrided clone() method.
Please write your complete CartesianPoint class and its testing class below
1a (35 pts) Write your complete Java code for the above Java class in CartesianPoint.java file. 1b (35 pts) In a new file named testPoint.java, write your test Java class and its main method which will do the following:
✓ create two Cartesian objects(pointOne, pointTwo), both of them with the explicit constructors;
✓ then compare the two points and output the farther away one’s x and y coordinates (test of comparable interface);
✓ next create a third CartesianPoint object pointThree which is a clone of the farther away one between the above two points, and then reset its coordinates to new x and y values and output them.
//Java code
public class CartesianPoint implements Comparable<CartesianPoint> { private int x; private int y; /** * default constructor which initializes * the point to the origin of the Cartesian coordinate system */ public CartesianPoint() { x=0; y=0; } /** * explicit constructor which initializes the point to a * pair of given value as (u, v) • * @param u * @param v */ public CartesianPoint(int u, int v) { this.x = u; this.y = v; } //getters and setters /** * whereX() function, which returns the x value * @return */ public int whereX() { return x; } /** * setX() function, which sets the x value to a new value v * @param u */ public void setX(int u) { this.x = u; } /** * whereY() function, which returns the y value * @return */ public int whereY() { return y; } /** * setY()function, which sets the y value to a new value u * @param v */ public void setY(int v) { this.y = v; } //helper Function public int distance (CartesianPoint other) { int dx = x - other.x; int dy = y - other.y; return (int) Math.sqrt(dx*dx + dy*dy); } @Override public int compareTo(CartesianPoint o) { if(this.distance(o)<1) return 1; else if(this.distance(o)>1) return -1; else return 0; } @Override protected CartesianPoint clone() throws CloneNotSupportedException { CartesianPoint clone1 = new CartesianPoint(); clone1.x = this.x; clone1.y = this.y; return clone1; } @Override public String toString() { return "Point ( "+x+", "+y+" )"; } }
//=======================================
public class testPoint { public static void main(String[] args) throws CloneNotSupportedException { CartesianPoint pointOne = new CartesianPoint(50,60); CartesianPoint pointTwo = new CartesianPoint(70,90); System.out.println(pointOne); System.out.println(pointTwo); //Compare int value = pointOne.compareTo(pointTwo); CartesianPoint pointThree = new CartesianPoint(); if(value>=0) { System.out.println("Farther Coordinate: " + pointOne); pointThree = pointOne.clone(); System.out.println("Clone: "+pointThree); } else { System.out.println("Farther Coordinate: " + pointTwo); pointThree = pointTwo.clone(); System.out.println("Clone: "+pointThree); } } }
//Output
//If you need any help regarding this solution .......... please leave a comment ... thanks