Question

In: Computer Science

1. Design a Java CartesianPoint class for a Cartesian Point which implements both cloneable and comparable...

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.

Solutions

Expert Solution

//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


Related Solutions

In java design and code a class named comparableTriangle that extends Triangle and implements Comparable. Implement...
In java design and code a class named comparableTriangle that extends Triangle and implements Comparable. Implement the compareTo method to compare the triangles on the basis of similarity. Draw the UML diagram for your classes. Write a Driver class (class which instantiates the comparableTriangle objects) to determine if two instances of ComparableTriangle objects are similar (sample output below). It should prompt the user to enter the 3 sides of each triangle and then display whether or not the are similar...
4) Define an abstract class Name Java class that implements interface Comparable   
4) Define an abstract class Name Java class that implements interface Comparable   
In Java: Job class The Job implements the Comparable interface A Job object is instantiated with...
In Java: Job class The Job implements the Comparable interface A Job object is instantiated with three int variables, indicating the arrivalTime, the timeForTheJob, and the priority. When the Job is created it is given the next sequential ID starting from 1. (You should use a static variable to keep track of where you are in ID assignment.) There are also int variables for startTime, waitTime (in queue) and endTime for the Job. The following methods are required: getID, set...
In Java: Job class The Job implements the Comparable interface A Job object is instantiated with...
In Java: Job class The Job implements the Comparable interface A Job object is instantiated with three int variables, indicating the arrivalTime, the timeForTheJob, and the priority. When the Job is created it is given the next sequential ID starting from 1. (You should use a static variable to keep track of where you are in ID assignment.) There are also int variables for startTime, waitTime (in queue) and endTime for the Job. The following methods are required: getID, set...
in JAVA PLEASE SHOW OUTPUT! PriorityQueueUserDefinedObjectExample (20) Create an Employee class which implements Comparable<Employee> The constructor...
in JAVA PLEASE SHOW OUTPUT! PriorityQueueUserDefinedObjectExample (20) Create an Employee class which implements Comparable<Employee> The constructor consists of an employee’s first name and an employee’s salary, both of which are instance variables. Create accessor and mutator methods for both of these variables Write an equals method that returns true if the salaries are equal with one cent and the names are exactly equal Write a compareTo method that returns 1 if the salary of this employee is greater than the...
**Java Programming Question** A class that implements a data type, “Point,” which has the following constructor:...
**Java Programming Question** A class that implements a data type, “Point,” which has the following constructor: Point(double x, double y, double z) and, the following API: double distanceto(Point q) it returns the Euclidean distance between this and q. The Euclidean distance between (x1, y1, z1) and (x2, y2, z2) is defined as sqrt( (x1-x2)^2 + (y1-y2)^2) + (z1-z2)^2). String toString() – it returns the string representation of the point. An example would be (2.3,4.5,3.0). Write a main method in the...
(a) The Cartesian coordinates of a point are (1, 1).
(a) The Cartesian coordinates of a point are (1, 1). (i) Find polar coordinates (r, θ) of the point, where r > 0 and 0≤θ< 2π. (ii) Find polar coordinates (r, θ) of the point, where r < 0 and 0 ≤ θ < 2π. (b) The Cartesian coordinates of a point are \((2 \sqrt{3},-2)\).(i)  Find polar coordinates (r, θ) of the point, where r >0 and 0≤θ<2π. (ii) Find polar coordinates (r, θ) of the point, where r<0 and 0 ≤θ< 2π.
Write a program to have a Car class which is comparable. Make the Car class comparable...
Write a program to have a Car class which is comparable. Make the Car class comparable and use speed to compare cars. Write a method in Main class that finds the minimum of 4 things (generically). Create 4 cars and pass the cars to the minimum method and find the smallest car. Have a toString method for the Car class. The main program should be able to find out how many cars are created so far by calling a static...
3.2. Unfortunately, you cannot modify the Rectangle class so that it implements the Comparable interface. The...
3.2. Unfortunately, you cannot modify the Rectangle class so that it implements the Comparable interface. The Rectangle class is part of the standard library, and you cannot modify library classes. Fortunately, there is a second sort method that you can use to sort a list of objects of any class, even if the class doesn't implement the Comparable interface. Comparator<T> comp = . . .; // for example, Comparator<Rectangle> comp = new RectangleComparator(); Collections.sort(list, comp); Comparator is an interface. Therefore,...
IN JAVA: Write a parent class, Device, which implements ageneric computer device.Create two child...
IN JAVA: Write a parent class, Device, which implements a generic computer device.Create two child classes, Disk and Printer, which specialize a Device with added functionality.Device Task:We are only interested in three things for the time being: the name of the device, an ID number identifying the device, and a flag indicating whether or not it is enabled. Thus, three fields are necessary, a String for the name, a int for the ID, and a boolean for the enabled status.Any...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT