12. A Circle class
A Circle class
To help us learn more about classes and their object instances, let's build a new class Circle.
The first rule is: write a description of the object.
A Circle is a plane figure that has a radius, a circumference, and an area. circumference = 2 * π * radius area = π * radius2Circles don't really have any behaviour - we are modeling an abstract circle here. This doesn't mean that our object doesn't have any methods. We need methods to extract the state of the circle. The diameter, circumference and area of a circle can all be determined from the radius of the circle, so we don't need any more information than the radius to build the object. We will however need some instance variables to store the values of the circumference and area in. Let's begin the class definition:
/**
* A Circle is a plane figure that has radius,
* circumference and surface area.
*/
public class Circle {
private double radius;
private double circumference;
private double area;
}
Now we need to write the constructor for a Circle.
/** * A Circle is a plane figure that has radius, * circumference and surface area. */ public class Circle { private double radius; private double circumference; private double area; /** *Constructs a Circle of radius aRadius. *@param aRadius the radius of the circle */ public Circle(double aRadius) { radius = aRadius; circumference = 2*Math.PI*radius; area = Math.PI*radius*radius; } }
Note that the constructor initialises all the instance variables, so that we have a completed circle by the time it's done. The constructor takes a single argument aRadius the radius of the circle to be instantiated. Math.PI is a constant of the Math object, i.e. a variable that always has the same value. In Java, constant values are written in upper case, so that we can instantly recognise them as constants.
Theoretically, we already have our Circle class in the sense that what we've done describes the complete Circle; it has no methods however so we can't do anything with it. We will need to implement methods that allow us to see the state of the object, because all the instance variables have been declared private - they aren't visible from outside the class. If we want to know what the area of the circle is, we can't get at it. So we write methods:
/** * Accessor for the radius of the Circle * @return the radius of the circle */ public double getRadius() { return radius; } /** * Accessor for the circumference of the Circle * @return the circumference of the circle */ public double getCircumference() { return circumference; } /** * Accessor for the area enclosed by the Circle * @return the area of the circle */ public double getArea() { return area; }
Do you see that there is no direct access to the instance variables of a Circle object - to read these values you have go through the methods getRadius(), getCircumference(), getArea(). These allow only one kind of transaction, namely they return the current values of the instance variables concerned. They cannot be used to change the state of the object, only to return the state. This protects the Circle from unauthorised intereference with its state.
The Circle class is now complete; compile it.






