Advertisement
Guest User

OOP practice

a guest
Oct 20th, 2014
459
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.94 KB | None | 0 0
  1. <?php
  2.  
  3. class   problem {
  4.    
  5.     private $operator;
  6.     private $num1;
  7.     private $num2; 
  8.     private $solution;
  9.    
  10.     public function __construct($operator, $num1, $num2) {
  11.         $this->operator = $operator;
  12.         $this->num1 = $num1;
  13.         $this->num2 = $num2;
  14.     }
  15.    
  16.     function problemSolve() {
  17.         switch ($this->operator){
  18.             case '+':
  19.                 $this->solution = $this->num1 + $this->num2;
  20.                 break;
  21.             case '-':
  22.                 $this->solution = $this->num1 - $this->num2;
  23.                 break;
  24.             case '/':
  25.                 $this->solution = $this->num1 / $this->num2;
  26.                 break;
  27.             case '*':
  28.                 $this->solution = $this->num1 * $this->num2;
  29.                 break;
  30.             case 'sqrt':
  31.                 $this->solution = sqrt($this->num1);
  32.                 break;
  33.             default:
  34.                 echo "Error, input not recognised.";
  35.                 break;
  36.         }
  37.     }
  38.    
  39.     function solutionOutput() {
  40.         echo "$this->solution";
  41.     }
  42. }
  43.  
  44. $calc = new problem('put_operator_here', first_number_here, second_number_here);
  45. $calc->problemSolve();
  46. $calc->solutionOutput();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement