Source of Circle.java


  1: package interfaces;

  3: // Circle.java
  4: //  a class that implements Measurable
  5: // See also:
  6: //  Measurable.java
  7: //  Rectangle.java

  9: public class Circle implements Measurable {

 11:     private double radius;

 13:     public Circle (double r) {
 14:         radius = r;
 15:     }

 17:     public double getRadius () {
 18:         return radius;
 19:     }

 21:     public double getCircumference () {
 22:         return 2 * Math.PI * radius;
 23:     }

 25:     public double getDiameter () {
 26:         return 2 * radius;
 27:     }

 29:     @Override
 30:     public double getArea () {
 31:         return Math.PI * Math.pow (radius, 2);
 32:     }

 34:     @Override
 35:     public double getPerimeter () {
 36:         return getCircumference ();
 37:     }

 39:     // toString method (inherited from Object)
 40:     @Override
 41:     public String toString () {
 42:         return "Circle (r = " + radius + ")";
 43:     }

 45:     // equals method (inherited from Object)
 46:     @Override
 47:     public boolean equals (Object obj) {
 48:         // if the object is a circle
 49:         if (obj instanceof Circle) {
 50:             // make a Circle variable to refer to it
 51:             Circle c = (Circle) obj;
 52:             // and return whether it has the same radius
 53:             return c.radius == this.radius;
 54:         }

 56:         // not a circle?  not equals!
 57:         return false;
 58:     }
 59: }