Assignment 7: A simple and Advanced Calculator class

class Calculator { # Creats a Class Named Calculator

class Calculator { # Creats a Class Named Calculator
protected $_val1, $_val2; # this makes the values private so they can only be used with in the class.
public function __construct( $val1, $val2 ) { #This creates a contructor for values so we can impleiment them later.
$this->_val1 = $val1; # this links the private class value _$val1 to a public value $val1
$this->_val2 = $val2; # this links the private class value _$val2 to a public value $val2
}
public function add() { # now ceating a public method to add values
return $this->_val1 + $this->_val2; # Now when method is used we are returning the code which uses $this to grab private valuse and add them together.
}
public function subtract() { # now ceating a public method to subtract values
return $this->_val1 - $this->_val2; # Now when method is used we are returning the code which uses $this to grab private valuse and subtract them together.
}
public function multiply() { # now ceating a public method to multiply values
return $this->_val1 * $this->_val2; # Now when method is used we are returning the code which uses $this to grab private valuse and mulitply them together.
}
public function divide() { # now ceating a public method to divide values
return $this->_val1 / $this->_val2; # Now when method is used we are returning the code which uses $this to grab private valuse and divid them together.
}
public function cub(){ # creating a method to cube the classes first value
return $this->_val1 * $this->_val1 * $this->_val1; # this is the formula that will be used when cub is called.
}
}
class CalcAdvanced extends Calculator {# we are adding extra variables to the class with the extends
private static $_allowedFunctions = array( "pow" => 2, "sqrt" => 1, "exp" => 1 ); # this creats a metheod with an array which will become variables for extended class and sets a identifier to values
public function __construct( $val1, $val2=null ) { #This creates a contructor for CalcAdvanced to call on one value from calculator and null the other.
parent::__construct( $val1, $val2 ); # this overides the parents two values so we get one.
}
public function __call( $methodName, $arguments ) { #Now we are trying to call invisable varibles in the method
if ( in_array( $methodName, array_keys( CalcAdvanced::$_allowedFunctions ) ) ) { # a if statement trying to look for an arry with certian funtions
$functionArguments = array( $this->_val1 ); # this makes one of the values from caculator public
if ( CalcAdvanced::$_allowedFunctions[$methodName] == 2 ) array_push( $functionArguments, $this->_val2 ); # this if statement is looking for allowedFuntions array with a value of 2
return call_user_func_array( $methodName, $functionArguments ); #this returns the result of if statement and class varible one.
} else {
die ( "p>Method 'CalcAdvanced::$methodName' doesn't exist /p>" ); # it exits if statement is false.
}
}
}
echo "h2> Simple Calculator function /h2>"; $calc = new Calculator( 3, 4 ); # we create an object named $calc which uses the Calculator class and sets values to 3 and 4 through constructer
echo "p> 3 + 4 = " . $calc->add() . "/p>"; # now we write a string of 3 + 4 = and input our object $calc in to the add method which returns answer
echo "p> 3 - 4 = " . $calc->subtract() . /p>"; #now we write a string of 3 - 4 = and input our object $calc in to the subtracts method which returns answer
echo "p> 3 * 4 = " . $calc->multiply() . /p>"; #now we write a string of 3 x 4 = and input our object $calc in to the multiply method which returns answer
echo "p> 3 / 4 = " . $calc->divide() . "/p>"; #now we write a string of 3 / 4 = and input our object $calc in to the divid method which returns answer
echo "h2> Advanced Calculator function /h2>"; $ca = new CalcAdvanced( 8, 5 ); #we create an object named $ca which uses the the extended class CalcAdvanced and sets values to 3 and 4 as values
echo "p>8 + 5 = " . $ca->add() . "/p>"; # now we write a string of 8 + 5 = and input our object $ca in to the add method which returns answer
echo "p>8 - 5 = " . $ca->subtract() . "/p>"; #now we write a string of 8 - 5 = and input our object $ca in to the subtracts method which returns answer
echo "p>8 * 5 = " . $ca->multiply() . "/p>"; #now we writea string of 8 x 5 = and input our object $ca in to the multiply method which returns answer
echo "p>8 / 5 = " . $ca->divide() . "/p>"; #now we write a string of 8 / 5 = and input our object $ca in to the multiply method which returns answer
echo "p>pow( 8, 5 ) = " . $ca->pow() . "/p>"; # because call method it recongizes two values and returens answer
echo "p>sqrt( 8 ) = " . $ca->sqrt() . "/p>"; # the array gave this a one ident which in turn made it one value which is then squared
echo "p>exp( 8 ) = " . $ca->exp() . "/p>"; # this also had a value of one which gave it one value input which rasies it two the power of the first number
echo "p>cub( 8 ) = " . $ca->cub() . "/p>"; # this will cube the first value of the class

Simple Calculator function

3 + 4 = 7

3 - 4 = -1

3 * 4 = 12

3 / 4 = 0.75

Advanced Calculator function

8 + 5 = 13

8 - 5 = 3

8 * 5 = 40

8 / 5 = 1.6

pow( 8, 5 ) = 32768

sqrt( 8 ) = 2.8284271247462

exp( 8 ) = 2980.9579870417

cub( 8 ) = 512