Source of Circle.java


  2: /**
  3:  * A class that implements the Measurable interface. This class represents a
  4:  * circle.
  5:  *
  6:  * @author Mark Young(A00000000)
  7:  */
  8: public class Circle implements Measurable {

 10:     private double radius;

 12:     /**
 13:      * Create a circle with the given radius.
 14:      *
 15:      * @param reqRad the requested radius
 16:      */
 17:     public Circle(double reqRad) {
 18:         requirePositive("radius", reqRad);
 19:         radius = reqRad;
 20:     }

 22:     /**
 23:      * Get this circle's radius.
 24:      *
 25:      * @return this circle's radius
 26:      */
 27:     public double getRadius() {
 28:         return radius;
 29:     }

 31:     /**
 32:      * Get this circle's circumference.
 33:      *
 34:      * @return this circle's circumference
 35:      */
 36:     public double getCircumference() {
 37:         return 2 * Math.PI * radius;
 38:     }

 40:     /**
 41:      * Get this circle's diameter.
 42:      *
 43:      * @return this circle's diameter
 44:      */
 45:     public double getDiameter() {
 46:         return 2 * radius;
 47:     }

 49:     @Override
 50:     public double getArea() {
 51:         return Math.PI * Math.pow(radius, 2);
 52:     }

 54:     @Override
 55:     public double getPerimeter() {
 56:         return getCircumference();
 57:     }

 59:     @Override
 60:     public String toString() {
 61:         return "Circle(r = " + radius + ")";
 62:     }

 64:     @Override
 65:     public boolean equals(Object obj) {
 66:         return (obj instanceof Circle that) && that.radius == this.radius;
 67:     }

 69:     // ---------- private methods ---------- //
 70:     /**
 71:      * Require that the given dimension be positive.
 72:      *
 73:      * @param dim the name of the dimension
 74:      * @param value the value requested for that dimension
 75:      * @throws IllegalArgumentException if {@code value} is not positive
 76:      */
 77:     private static void requirePositive(String dim, double value) {
 78:         if (value <= 0.0) {
 79:             throw new IllegalArgumentException(dim + ": " + value);
 80:         }
 81:     }

 83: }