Answer / Part 1 Explaining - OOP(Ex-1)
Understanding the scenario
—————————
So before we start the coding process we must understand the problem, how Gregorian’s and Ethiopian’s Calendar 📆 work, & how age calculating could be done.
•Okey .... y’all just subtract date of birth from current date😉 easy right !
❓But, how it could be done, mean do we subtract all or each of values such as dates, months & years separately
❓What if month of (date of birth) greater than current month / it becomes negative/
❓Is each of years months or each of months days differs
YES, YES ....... Yes
📌 This is why we need logic and cases to solve low level problems upward to higher
>>start:
📣 </ Gregorian Calendar >
•Each year has 12 months
•Each months has different dates the highest is 31 and the lowest is 28
•Leap year is February which is 2nd month 29 days and on non-leap year 28 days
📣 </ Ethiopian Calendar >
•Each year has 13 months
•Each 12 months has 30 days and the last one has 5 or 6 days
•Leap year is Pagume/ጷግሜን which is 13th month 6 days & on non-leap year 5 days
——————
📆 How to calculate Leap Years 📆
Pseudo Code (Gregorian)
If (year%4 = 0)
February = 29
else
February = 28
•Which means if the year is divided by 4 without the remainder then February month has 29 days and the reverse/else which is false means it has 28 days.
Pseudo Code (Ethiopian)
If (year%4 = 3)
Pagume = 6
else
Pagume = 5
•In the Ethiopian calendar leap is different only the reminder which the year divided by 4 is becomes only 3 then it’s leap year otherwise it’s not-leap year.
——————
Note: declare months[] array with the following variables for months days in Gregorian calendar.
months[] ={31,February,31,30,31,30,31,31,30,31,30,31}
NB. February is a variable declared previously on leap year or not-leap year
——————
There are two cases to deal with to calculate the age from date of birth (DOB)
⭕️ Case 1: subtracting without carry forwarded
Ex: Current: 2019.10.28
DOP: 1992.08.20
————————
Age : 27 years, 2 months, 8 days
⭕️ Case 1:
When we subtract from current date with carry forwarded from month or year
Ex: Current: 2019.10.28
DOP: 1992.12.30
————————
Age : 26, 8, 29
📌So to point the problem in this 👆🏽 example we cannot subtract 30 from 28 it will be come -2 this means there is no date called -2 but we can lend from the month, if we lend from month we must know how many days the month contains so we must check from the array we declared previously. But for the month we can borrow 12 or 13 months from year.
Pseudo Code (Case 1)
If (birthDate > curDate)
curMonth = curMonth - 1
curDate = curDate + months[curMonth]
If (birthMonth > curMonth)
curYear = curYear - 1
curMonth = curMonth +12
Pseudo Code (Case 2)
calcDate = curDate - birthDate
calcMonth = curMonth - birthMonth
calcYear = curYear - birthYear
Age Calculator Exercise Source Codes
Gregorian :
https://t.me/freecodecs/531
Ethiopian :
https://t.me/freecodecs/532
As Always:
Have a Lit 🔥Study Folks 🙌🏽
—————————
Join The Underground Coding Movement!
AI Programming
@freecodecs