CircleClass

Updated Dani 0 Tallied Votes 260 Views Share

A demonstration of introductory object oriented programming. A class to define a circle object with radius, area, and perimeter parameters.

package circle;
public class CircleClass
{
    // member variables
    private double radius, circumference, area;
    // constructor function
    public CircleClass() { }
    public CircleClass(double r) { setRadius(r); }
    // accessors
    public double getRadius() { return radius; }
    public double getCircumference() { return circumference; }
    public double getArea() { return area; }
    // data modifiers
    public void setRadius(double x)
    {
        radius = x;
        circumference = 2 * Math.PI * radius;
        area = Math.PI * radius * radius;
    }
    public void setCircumference(double x)
    {
        circumference = x;
        radius = circumference / (2 * Math.PI);
        area = Math.PI * radius * radius;
    }
    private void setArea(double x)
    {
        area = x;
        radius = Math.sqrt(area / Math.PI);
        circumference = 2 * Math.PI * radius;
    }
}