Source of ColourCircle.java


  1: import java.awt.Color;

  3: /** 
  4:  * ColourCircle.java
  5:  *  a class that implements Measurable and Colourable
  6:  * See also:
  7:  *  Measurable.java
  8:  *  Colourable.java
  9:  *
 10:  * @author Mark Young (A00000000)
 11:  */
 12: public class ColourCircle implements Measurable, Colourable {

 14:     private double radius;
 15:     private Color colour;

 17:     /** Create a circle with the given radius. */
 18:     public ColourCircle(double r) { 
 19:         this(r, Color.BLACK);
 20:     }

 22:     /** Create a circle with this radius and colour */
 23:     public ColourCircle(double r, Color c) {
 24:         radius = r;
 25:         colour = c;
 26:     }

 28:     /** Return the radius of this circle. */
 29:     public double getRadius() {
 30:         return radius;
 31:     }

 33:     /** Return the circumference of this circle. */
 34:     public double getCircumference() {
 35:         return 2 * Math.PI * radius;
 36:     }

 38:     /** Return the diameter of this circle. */
 39:     public double getDiameter() {
 40:         return 2 * radius;
 41:     }

 43:     /** Return the area of this circle. */
 44:     @Override
 45:     public double getArea() {
 46:         return Math.PI * Math.pow(radius, 2);
 47:     }

 49:     /** Return the perimeter of this circle. */
 50:     @Override
 51:     public double getPerimeter () {
 52:         return getCircumference();
 53:     }

 55:     /** Change this object's colour. */
 56:     public void setColor(Color c) {
 57:         colour = c;
 58:     }

 60:     /** Return this object's colour. */
 61:     public Color getColor() {
 62:         return colour;
 63:     }

 65:     /** Return a String representing this circle. */
 66:     @Override
 67:     public String toString() {
 68:         return "Circle (r = " + radius + ", " + colour.toString() + ")";
 69:     }

 71:     /** Return whether this circle is equals to that object. */
 72:     @Override
 73:     public boolean equals(Object obj) {
 74:         // check for quick equality
 75:         if (obj == this) {
 76:             return true;
 77:         }

 79:         // I am not a null
 80:         if (obj == null) {
 81:             return false;
 82:         }

 84:         // if the object is a circle
 85:         if (obj instanceof ColourCircle) {
 86:             // make a Circle variable to refer to it
 87:             ColourCircle c = (ColourCircle) obj;
 88:             // and return whether it has the same radius
 89:             return c.radius == this.radius && c.colour.equals(this.colour);
 90:         }

 92:         // not a circle?  not equals!
 93:         return false;
 94:     }

 96: }