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

home / study / questions and answers / engineering / computer science / define a

ID: 3664151 • Letter: H

Question

home / study / questions and answers / engineering / computer science / define a class named circle. a circle object stores ...

Question

write a java class named Circle. A Circle object stores a radius and the (x,y) coordinates of its center point. Each Circle object should have 3 private fields , x-coordinate, y-coordinate and radius. Each circle object should have the following public methods:

Circle(x,y,radius)

Constructs a new circle with a center specified by the given Point and with the given integer radius.

getX()

returns the center's x coordinate

getY()

returns the center's y coordinate

getRadius()

returns the circle's radius

getArea()

Returns the area occupied by the circle, using the formula (pie)*r^2

getCircumference ()

Returns the circle's circumference (distance around the circle) using the formula 2(pie)(r)

toString()

Returns a string representation of the circle,such as"Circle[center = (75,20),radius=30]"

contains(x,y)

Returns true if the (x,y) coordinates lie within the circle else returns false. (Hint:calculate the distance between the center and the (x,y) and compare it with the radius)

Then write a client class named CircleClient. It should create an object of class Circle and initialize it to (10.5) and Radius to 7. Print the object Print out its circumference and area. If point(5,7) lies within the clircle print " (5,7) lies within the circle"else print"(5,7) doesnot lie within the circle".

Explanation / Answer

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

   double x, y, radius;

   /**
   * @param x
   * @param y
   * @param radius
   */
   public Circle(double x, double y, double radius) {
       super();
       this.x = x;
       this.y = y;
       this.radius = radius;
   }

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

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

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

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

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

   /**
   * @param radius
   * the radius to set
   */

   public void setRadius(double radius) {
       this.radius = radius;
   }

   /**
   * Returns true if the (x,y) coordinates lie within the circle else returns
   * false. (Hint:calculate the distance between the center and the (x,y) and
   * compare it with the radius)
   *
   * @param x
   * @param y
   * @return
   */
   public boolean contains(double x, double y) {

       double dist = Math.sqrt((x - this.x) * (x - this.x) - (y - this.y)
               * (y - this.y));
       if (dist > this.radius) {
           return false;
       } else {
           return true;
       }

   }

   /**
   * return circumference
   *
   * @return
   */
   public double getCircumference() {

       double circumference = 2 * Math.PI * radius;
       return circumference;
   }

   /**
   * return circumference
   *
   * @return
   */
   public double getArea() {

       double area = Math.PI * (radius * radius);
       return area;
   }

}

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

   /**
   * @param args
   */
   public static void main(String[] args) {
       Circle circle = new Circle(10, 5, 7);
       System.out.println("Circumference :" + circle.getCircumference());
       System.out.println("Area:" + circle.getArea());
       if (circle.contains(5, 7)) {

           System.out.println("(5,7) lies within the circle");
       } else {

           System.out.println("(5,7) does not lies within the circle");
       }
   }
}

OUTPUT:

Circumference :43.982297150257104
Area:153.93804002589985
(5,7) lies within the circle