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

Write a java program. Write a class called Point that has two int fields x and y

ID: 3686957 • Letter: W

Question

Write a java program.

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)

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 x- coordinates and if the x-coordinates are same then use y-coordinates to define the order.

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

############### Point.java ##############

public class Point implements Comparable<Point>{
  
   private double x;
   private double y;
  
   public Point() {
       x=0;
       y=0;
   }
  
   public Point(double x, double y) {
       this.x = x;
       this.y = y;
   }

   public double getX() {
       return x;
   }

   public double getY() {
       return y;
   }

   public void setX(double x) {
       this.x = x;
   }

   public void setY(double y) {
       this.y = y;
   }

   @Override
   public int compareTo(Point o) {
       if(x < o.x)
           return -1;
       else if(x > o.x)
           return 1;
       else{// x = o.x
           if(y < o.y)
               return -1;
           else if(y > o.y)
               return 1;
           else
               return 0;
       }
   }
  
   @Override
   public String toString() {
       return "("+x+","+y+")";
   }
  


}


################# Circle.java ###################

public class Circle implements Comparable<Circle>{

   private double radius;
   private Point center;
  
   public Circle() {
       radius=0;
       center = new Point(0,0);
   }
   public Circle(double radius, Point point) {
       this.radius = radius;
       this.center = point;
   }
  
   public Circle(double radius, double x, double y){
       this.radius = radius;
       this.center = new Point(x,y);
   }
   public double getRadius() {
       return radius;
   }
   public Point getCenter() {
       return center;
   }
   public void setRadius(double radius) {
       this.radius = radius;
   }
   public void setCenter(Point center) {
       this.center = center;
   }
  
   public double getArea(){
       return 3.14*radius*radius;
   }
   @Override
   public int compareTo(Circle o) {
       if(this.getArea() < o.getArea())
               return -1;
       else if(this.getArea() > o.getArea())
           return 1;
       else{
           return this.center.compareTo(o.center);
       }
   }
   @Override
   public String toString() {
       return "Center: "+center.toString()+", Area: "+getArea();
   }
}

#################### TestCircle.java #########################

import java.util.Arrays;

public class TestCircle {
  
   public static void main(String[] args) {
      
       Circle circles[] = {
               new Circle(13, new Point(3,-2)),
               new Circle(10, -4,2),
               new Circle(7, 0, 0),
               new Circle(9, 4,4),
               new Circle(11, -5, 3),
               new Circle(10, -3.5, 2.5)
       };
      
       for(Circle c: circles)
           System.out.println(c.toString());
      
       Arrays.sort(circles);
      
       System.out.println(" After Sorting: ");
       for(Circle c: circles)
           System.out.println(c.toString());
      
   }

}

/*

Sample run:

Center: (3.0,-2.0), Area: 530.66
Center: (-4.0,2.0), Area: 314.0
Center: (0.0,0.0), Area: 153.86
Center: (4.0,4.0), Area: 254.34
Center: (-5.0,3.0), Area: 379.94
Center: (-3.5,2.5), Area: 314.0

After Sorting:
Center: (0.0,0.0), Area: 153.86
Center: (4.0,4.0), Area: 254.34
Center: (-4.0,2.0), Area: 314.0
Center: (-3.5,2.5), Area: 314.0
Center: (-5.0,3.0), Area: 379.94
Center: (3.0,-2.0), Area: 530.66

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote