wilded
6/27/2017 - 5:54 PM

Class With Constructor

Class With Constructor

<?php
class Person{
   public $age;
   public function __construct($initialAge){
          // Add some more code to run some checks on initialAge
              if($initialAge >= -5 && $initialAge <= 30){
                  if($initialAge < 0){
                    print_r ('Age is not valid, setting age to 0.');
                    echo "\n";
                    $this->age = 0;
                  } else {
                      $this->age = $initialAge;
                  }
              }
              
              
    }
   public  function amIOld(){
            // Do some computations in here and print out the correct statement to the console 
                if($this->age >= 18){
                  print_r ("You are old.");         
                  echo "\n";
                }
                if($this->age >= 13 && $this->age < 18){
                  print_r ("You are a teenager.");         
                  echo "\n";
                }
                if($this->age < 13){
                  print_r ("You are young.");         
                  echo "\n";
                }        
 
    }
   public  function yearPasses(){
          // Increment the age of the person in here
          $this->age = $this->age + 1;
          
    }
   
      
}
$T = intval(fgets(STDIN));
 for($i=0;$i<$T;$i++){
     $age=intval(fgets(STDIN));
     $p=new Person($age);
     $p->amIOld();
     for($j=0;$j<3;$j++){
         $p->yearPasses();
     }
     $p->amIOld();
     echo "\n";
         
 }
?>