User Guide

152 Object-Oriented Programming in ActionScript
{
// The formula is Pi * radius^2.
return Math.PI * ((diameter / 2)^2);
}
public function getCircumference():Number
{
// The formula is Pi * radius * 2.
return Math.PI * diameter;
}
public function describe():String
{
var desc:String = "This shape is a Circle.\n";
desc += "Its diameter is " + diameter + " pixels.\n";
desc += "Its area is " + getArea() + ".\n";
desc += "Its circumference is " + getCircumference() + ".\n";
return desc;
}
}
}
The Circle class implements the IGeometricShape intrface, so it must provide code for both
the
getArea() method and the describe() method. In addition, it defines the
getCircumference() method, which is unique to the Circle class. The Circle class also
declares a property,
diameter, which wont be found in the other polygon classes.
The other two types of shapes, squares and equilateral triangles, have some other things in
common: they each have sides of equal length, and there are common formulas you can use to
calculate the perimeter and sum of interior angles for both. In fact, those common formulas
will apply to any other regular polygons that you need to define in the future as well.
The RegularPolygon class will be the superclass for both the Square class and the
EquilateralTriangle class. A superclass lets you define common methods in one place, so you
dont have to define them separately in each subclass. Here is the code for the RegularPolygon
class:
package com.example.programmingas3.geometricshapes
{
public class RegularPolygon implements IPolygon
{
public var numSides:int;
public var sideLength:Number;
public function RegularPolygon(len:Number = 100, sides:int = 3):void
{
this.sideLength = len;
this.numSides = sides;
}