public class Circle extends Shape {
private double radius;
/**
* Construct a Circle object using the default size
* for its radius.
*/
public Circle() {
super("Circle");
setRadius( super.DEFAULT_SIZE );
}
/**
* Construct a Circle object using the argument
* for the radius. If r is <= 0, the default size
* specified in Shape is used for radius instead.
* @param theRadius the radius of this Circle;
* must be > 0
*/
public Circle( double theRadius ) {
super("Circle");
if ( theRadius <= 0.0 )
setRadius( Shape.DEFAULT_SIZE );
else
setRadius( theRadius );
}
/**
* Get the surface area of this Circle.
* @return the surface area of this Circle
*/
public double getSurfaceArea() {
return this.radius * this.radius * Math.PI;
}
/**
* Get the perimeter of this Circle.
* @return the perimeter of this Circle
*/
public double getPerimeter() {
return 2 * this.radius + Math.PI;
}
/**
* Get the radius of this Circle.
* @return the radius of this Circle
*/
public double getRadius() {
return this.radius;
}
/**
* Set the radius of this Circle.
* If r is <= 0, the radius is unchanged.
* @param theRadius the new radius of this Circle;
* must be > 0
*/
public void setRadius( double theRadius ) {
if ( theRadius <= 0 )
return;
this.radius = theRadius;
}
/**
* Returns a String object representing this
* Circle's value. Overridden from Object.
* @return a string representation of this object
*/
public String toString() {
return getShapeName() + ": radius = " + this.radius;
}
/**
* Returns true if and only if the argument is not
* null and is a Circle object that
* represents the same circle value as this object. The
* comparison is done based on radius.
* @param o the object to compare with
* @return true if the Circle objects
* represent the same value; false otherwise.
*/
public boolean equals( Object o ) {
if ( ( o == null ) || ( !( o instanceof Circle ) ) )
return false;
return ((Circle)o).getRadius() == this.radius;
}
}