DEV Community

Akande Joshua
Akande Joshua

Posted on

HackerRank: Class vs. Instance Solution using PHP (Day 4 - 30 Days of Code)

Task

Write a Person class with an instance variable, age, and a constructor that takes an integer, initialAge, as a parameter. The constructor must assign initialAge to age after confirming the argument passed as initialAge is not negative; if a negative argument is passed as initialAge, the constructor should set age to 0 and print Age is not valid, setting age to 0. In addition, you must write the following instance methods:

  1. yearPasses() should increase the age instance variable by 1
  2. amIOld() should perform the following conditional actions:
  3. If age < 13, print You are young.
  4. If age>=13 and age<18, print You are a teenager.
  5. Otherwise, print You are old.
  6. To help you learn by example and complete this challenge, much of the code is provided for you, but you’ll be writing everything in the future. The code that creates each instance of your Person class is in the main method. Don’t worry if you don’t understand it all quite yet!

Note: Do not remove or alter the stub code in the editor.

Solution


class Person {
    public $age;

    public function __construct($initialAge) {
        if ($initialAge < 0) {
            echo "Age is not valid, setting age to 0.\n";
            $this->age = 0;
        } else {
            $this->age = $initialAge;
        }
    }

    public function amIOld() {
        if ($this->age < 13) {
            echo "You are young.\n";
        } else if ($this->age >= 13 && $this->age < 18) {
            echo "You are a teenager.\n";
        } else {
            echo "You are old.\n";
        }
    }

    public function yearPasses() {
        $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";
}
Enter fullscreen mode Exit fullscreen mode

This is part of 30 Days of Code - Day 4

Top comments (0)