Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

write program in (java) eclipse 1. Write a class called Point that has two int f

ID: 3687048 • Letter: W

Question

write program in (java) eclipse

1. Write a class called Point that has two int fields x and y coordinates. The class should have the necessary accessors, mutators and constructors. (You may choose to write the compareTo method as well)


2. Write another class called Circle that has 2 fields; a double field called radius and a Point object that stores coordinates of the central point of the circle. The class should have the necessary accessors, mutators and constructors. Use the compareTo method to define a natural order for the Circle class. The objects should be ordered based on the area of the circle. If the circles have equal area then they should be ordered according to the xcoordinates and if the x-coordinates are same then use y-coordinates to define the order.

3. Also write a client class that creates an array of circles and use the Arrays.sort() method to see if the compareTo method you wrote works as intended.

Explanation / Answer

/**
* @author Srinivas Palli
*
*/
public class Point {
   int x;
   int y;

   public Point() {
       // TODO Auto-generated constructor stub
   }

   public Point(int x, int y) {
       this.x = x;
       this.y = y;
   }

   /**
   * @return the x
   */
   public int getX() {
       return x;
   }

   /**
   * @param x
   * the x to set
   */
   public void setX(int x) {
       this.x = x;
   }

   /**
   * @return the y
   */
   public int getY() {
       return y;
   }

   /**
   * @param y
   * the y to set
   */
   public void setY(int y) {
       this.y = y;
   }

   /*
   * (non-Javadoc)
   *
   * @see java.lang.Object#toString()
   */
   @Override
   public String toString() {
       return "(" + x + ", " + y + ")";
   }

}

/**
* @author Srinivas Palli
*
*/
public class Circle implements Comparable<Circle> {
   double radius;
   Point p;

   public Circle() {
       // TODO Auto-generated constructor stub
   }

   public Circle(double radius, Point p) {
       super();
       this.radius = radius;
       this.p = p;
   }

   /**
   * @return the radius
   */
   public double getRadius() {
       return radius;
   }

   /**
   * @param radius
   * the radius to set
   */
   public void setRadius(double radius) {
       this.radius = radius;
   }

   /**
   * @return the p
   */
   public Point getP() {
       return p;
   }

   /**
   * @param p
   * the p to set
   */
   public void setP(Point p) {
       this.p = p;
   }

   @Override
   public int compareTo(Circle c) {
       // TODO Auto-generated method stub

       if (this.getRadius() > c.getRadius())
           return 1;
       else if (this.getRadius() == c.getRadius()) {
           if (this.getP().getX() > c.getP().getX())
               return 1;
           else if (this.getP().getX() == c.getP().getX()) {
               if (this.getP().getY() > c.getP().getY()) {
                   return 1;

               } else
                   return -1;

           } else
               return -1;
       } else
           return -1;
   }

   /*
   * (non-Javadoc)
   *
   * @see java.lang.Object#toString()
   */
   @Override
   public String toString() {
       return "Circle [radius=" + radius + ", p=" + p + "]";
   }

}

import java.util.Arrays;
import java.util.Random;

/**
* @author Srinivas Palli
*
*/
public class CircleClient {

   public static void main(String[] args) {

       Circle[] circles = new Circle[10]; // Create an array of 10 circles
       Random random = new Random();
       for (int i = 0; i < circles.length; i++) { // Populate the array with 10
                                                   // random circles
           circles[i] = new Circle(Math.random() * 10, new Point(
                   random.nextInt(5), random.nextInt(5)));
       }

       System.out.println("Unsorted"); // Print array of circles before sorting
       for (Circle circle : circles) {
           System.out.println(circle + " ");
       }
       Arrays.sort(circles); // Can be sorted because we implemented Comparable
                               // interface
       System.out.println("Sorted"); // Print sorted array
       for (Circle circle : circles) {
           System.out.println(circle + " ");
       }

   }
}

OUTPUT:

Unsorted
Circle [radius=6.041110736618106, p=(2, 4)]
Circle [radius=8.037688442806015, p=(3, 3)]
Circle [radius=8.122255639965532, p=(1, 4)]
Circle [radius=4.202177617747209, p=(2, 3)]
Circle [radius=9.27631782826717, p=(2, 1)]
Circle [radius=4.861442836199342, p=(3, 1)]
Circle [radius=5.27207254281175, p=(0, 0)]
Circle [radius=0.855592156739815, p=(3, 3)]
Circle [radius=9.765768515329345, p=(4, 3)]
Circle [radius=2.7832707566528345, p=(4, 1)]
Sorted
Circle [radius=0.855592156739815, p=(3, 3)]
Circle [radius=2.7832707566528345, p=(4, 1)]
Circle [radius=4.202177617747209, p=(2, 3)]
Circle [radius=4.861442836199342, p=(3, 1)]
Circle [radius=5.27207254281175, p=(0, 0)]
Circle [radius=6.041110736618106, p=(2, 4)]
Circle [radius=8.037688442806015, p=(3, 3)]
Circle [radius=8.122255639965532, p=(1, 4)]
Circle [radius=9.27631782826717, p=(2, 1)]
Circle [radius=9.765768515329345, p=(4, 3)]