In PHP**** We have a class Person, a person has name, last name and birth date.
ID: 3847768 • Letter: I
Question
In PHP****
We have a class Person, a person has name, last name and birth date. But let's say we need also to calculate the generation of that person, we have to define a new private property called $generation that is going to be equal to one of these values:
genz: Born 1996 to today.
millennial: Born from 1977 to 1995.
genx: Born from 1965 to 1976.
boomers: Born from 1946 to 1964.
We have set the birthDate property private because want to force ourselves to set the value of the birthDate though a function, that way we can calculate the Person generation on the same moment that the birthDate is set.
Instructions
Fill the calculateGeneration function with the proper code needed to set the generation to the right value between: 'genz','millennial','genx','boomer'
HINT
Create the necessary if..else conditions to compare the $year with the table specified at the beginning of this exercise.
---------------------------------------
class Person {
public $name; //string
public $lastName; //string
private $birthDate; //datetime
private $generation; //string
public function setBirthDate($auxDate) {
$this->birthDate = new DateTime($auxDate);
$this->calculateGeneration($this->birthDate->format('y'));
}
public function calculateGeneration($year) {
// Help with code here! set here the value of the $this->generation property
//depending on the $year
$this->generation = null;
}
public function getGeneration() {
return $this->generation; }
}
/** DO NOT EDIT AFTER THIS LINE**/
$per = new Person();
$per->setBirthDate('1985-10-20');
$isMillenial = ($per->getGeneration() === 'millennial');
$per->setBirthDate('1997-10-20');
$isGenz = ($per->getGeneration() === 'genz');
if($isGenz and $isMillenial)
echo 'success' . PHP_EOL;
else
echo 'Please set the $generation property value properly' . PHP_EOL;
Explanation / Answer
public function calculateGeneration($year) {
// Help with code here! set here the value of the $this->generation property
//depending on the $year
$this->generation = null;
if($year>=1946 && $year<=1964)
{
$this->generation = "boomer";
}
elseif($year>=1965 && $year<=1976)
{
$this->generation = "genx";
}
elseif($year>=1977 && $year<=1995)
{
$this->generation = "millennial";
}
elseif($year>=1996 && $year<=2017)
{
$this->generation = "genz";
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.