Source of Circle.java


  1: /**
  2:  * A class to represent a circle (as an example of implementing an interface,
  3:  * in this case Measurable).
  4:  * See also:
  5:  *  Measurable.java
  6:  *  Rectangle.java
  7:  *
  8:  * @author Mark Young (A00000000)
  9:  */
 10: public class Circle implements Measurable {

 12:     private double radius;
 13:     
 14:     /**
 15:      * Create a circle with the given radius.
 16:      *
 17:      * @param reqRadius     the radius of the new circle
 18:      */
 19:     public Circle(double reqRadius) {
 20:         requirePositive(reqRadius);
 21:         radius = reqRadius;
 22:     }
 23:     
 24:     /**
 25:      * This circle's radius.
 26:      *
 27:      * @return the radius of this circle
 28:      */
 29:     public double getRadius() { 
 30:         return radius; 
 31:     }

 33:     /**
 34:      * This circle's width.
 35:      *
 36:      * @return the width of this circle.
 37:      */
 38:     public double getWidth() {
 39:         return getDiameter();
 40:     }
 41:     
 42:     /**
 43:      * This circle's height.
 44:      *
 45:      * @return the height of this circle.
 46:      */
 47:     public double getHeight() {
 48:         return getDiameter();
 49:     }
 50:     
 51:     /**
 52:      * This circle's circumference.
 53:      *
 54:      * @return the circumference of this circle
 55:      */
 56:     public double getCircumference() { 
 57:         return 2 * Math.PI * radius; 
 58:     }
 59:     
 60:     /**
 61:      * This circle's diameter.
 62:      *
 63:      * @return the diameter of this circle
 64:      */
 65:     public double getDiameter()        { 
 66:         return 2 * radius; 
 67:     }
 68:     
 69:     /**
 70:      * This circle's area.
 71:      *
 72:      * @return the area of this circle
 73:      */
 74:     @Override
 75:     public double getArea() { 
 76:         return Math.PI * Math.pow(radius, 2); 
 77:     }
 78:     
 79:     /**
 80:      * This circle's perimeter.
 81:      *
 82:      * @return the perimeter of this circle
 83:      */
 84:     @Override
 85:     public double getPerimeter () { 
 86:         return getCircumference(); 
 87:     }

 89:     /**
 90:      * Convert thsi circle to a String.
 91:      *
 92:      * @return a String representing this circle
 93:      */
 94:     @Override
 95:     public String toString() {
 96:         return "Circle (r = " + radius + ")";
 97:     }

 99:     /**
100:      * Throw an exception if the given value is not positive.
101:      *
102:      * @param value the value to check
103:      * @throws IllegalArgumentException if value ≤ 0.0.
104:      */
105:     private static void requirePositive(double value) {
106:         if (value <= 0.0) {
107:             throw new IllegalArgumentException("Radius: " + value);
108:         }
109:     }

111: }