Chapter 24 Example 1

by actionscriptbible
♥0 | Line 21 | Modified 2010-01-28 14:59:22 | MIT License
play

ActionScript3 source code

/**
 * Copyright actionscriptbible ( http://wonderfl.net/user/actionscriptbible )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/2j0M
 */

package {
  import com.actionscriptbible.Example;
  
  public class ch24ex1 extends Example {
    public function ch24ex1() {
      try {
        for (var i:int = 0; i < 10; i++) {
          var n:Number = Math.round(Math.random() * 100 - 50);
          trace("√"+n+" = "+squareRoot(n));
        }
      } catch (err:ArgumentError) {
        trace("ERROR: " + err.message);
      }
    }
      
    protected function squareRoot(n:Number):Number {
      if (n < 0) {
        throw new ArgumentError("squareRoot() doesn't support imaginary numbers.");
      }
      return Math.sqrt(n);
    }
  }
}