In: Physics
java
Define the Circle2D class that contains: • Two double data fields named x and y that specify the center of the circle with get methods. A data field radius with a get method. • A data field radius with a get method. • A no-arg constructor that creates a default circle with (0, 0) for (x, y) and 1 for radius. • A constructor that creates a circle with the specified x, y, and radius. • A method getArea() that returns the area of the circle. • A method getPerimeter() that returns the perimeter of the circle. • A method contains(double x, double y) that returns true if the specified point (x, y) is inside this circle (a) • A method contains(Circle2D circle) that returns true if the specified circle is inside this circle(b) • A method overlaps(Circle2D circle) that returns true if the specified circle overlaps with this circle(c)
import java.lang.Math; public class Circle2D { // Data fields for center of the circle. private double x; private double y; // Data field for the radius of the circle. private double radius; // A no-argument constructor that creates a circle with (0,0) for (x,y) and 1 for radius. Circle2D() { x = 0; y = 0; radius = 1; } // A constructor that creates a circle with the specified x, y, and radius. Circle2D(double x, double y, double radius) { this.x = x; this.y = y; this.radius = radius; } // A get method for x. public double getX() { return x; } // A get method for y. public double getY() { return y; } // A get method for radius. public double getRadius() { return radius; } // A method that returns the area of the circle. public double getArea() { double Area = Math.PI *radius*radius; // Area of circle pi*r^2. return Area; } // A method that returns the perimeter of the circle. public double getPerimeter() { double Perimeter = 2*Math.PI *radius; // Perimeter of circle 2*pi*r. return Perimeter; } /* A method that returns true if the specified point (x, y) is inside this circle. * Logic : If the distance of given point(x,y) from center is less then radius of * the circle, then the point will lie inside the circle. */ public boolean Contains(double x, double y) { double SqrDist = Math.pow((this.x - x),2) + Math.pow((this.y - y),2); double Dist = Math.sqrt(SqrDist); if (Dist < radius ) { return true; } else { return false; } } /* A method that returns true if the specified circle is inside this circle. * Logic : If the sum of the distance between the centers of the circles and * smaller radius is lesser than the bigger radius, then smaller is inside the bigger circle. */ public boolean Contains(Circle2D circle) { double SqrCentersDist = Math.pow((this.x - circle.getX()),2) + Math.pow((this.y - circle.getY()),2); double CentersDist = Math.sqrt(SqrCentersDist); double SmallRadius = circle.getRadius(); if ( (CentersDist + SmallRadius) < radius) { return true; } else { return false; } } /* A method that returns true if the specified circle overlaps with this circle. * Logic : If sum of the distance between the centers of the circles is less than * sum of smaller radius and big radius and greater than difference of big radius and small radius. * then these two circle will overlap. */ public boolean Overlaps(Circle2D circle) { double SqrCentersDist = Math.pow((this.x - circle.getX()),2) + Math.pow((this.y - circle.getY()),2); double CentersDist = Math.sqrt(SqrCentersDist); double SmallRadius = circle.getRadius(); if ((CentersDist<(radius + SmallRadius)) && (CentersDist>(radius - SmallRadius))) { return true; } else { return false; } } }
=======================================================================================
If you want to test this class:
public class Test { public static void main(String[] args) { // TODO Auto-generated method stub Circle2D circle1 = new Circle2D(5,5,10); Circle2D circle2 = new Circle2D(5,5,2); System.out.println("Center = (" + circle1.getX() + ", " + circle1.getY() + ")" ); System.out.println("Radius = " + circle1.getRadius()); System.out.println("area = " + circle1.getArea()); System.out.println("parameter = " + circle1.getPerimeter()); System.out.println("Is circle1 Contains (1.5, 0.5) = " + circle1.Contains(1.5, 0.5)); System.out.println("Is circle1 Contains circle2 = " + circle1.Contains(circle2)); } }