I completed the Age Checker activity using Java. The requirement of the age checker is 18 years or older.
I had to import the JOptionPanel class from the swing package and i imported all classes from the util package so i could get the Date class. All code was done within one class, the Age class, and in the main method.
First, I declared my 3 string variables, day month and year. these will turn into the users day month and year of birth.
Next came the option panels. 3 pop up. the first one asks for the day of birth in 2 digits. Then another pops up asking for the month, again in two digits. Lastly one pops up asking for the year, however, instead if the mandatory two digits like before, the year requires 4. The users input will be stored within the string variables day, month and year.
Now, strings will not work for what this code needs to do, so they need to be converted to integers. The day variable gets turned into d, and is now an integer. The same thing happens from month to m, and year to y.
The users birthday has been gathered, now the current date needs to be gathered to verify if the user is 18. to do this, I created a new GregorianCalendar object. this object can supply alot of information using the imported Date class. I declared a new integer variable cd (stands for current day). cd is equal to calendar.get(Calendar.DAY_OF_MONTH). This snags the day of the month and puts it as an integer stored as cd. this is all done within the imported Date class. Similar code with minor, yet important differences is written for cm (current month), and cy (current year).
Finally the program has all the data it needs. Now it just needs to do a few things with it. there is an if statement with a condition creating a new Date object set up (year,month,day). the variables represent the user's birthday. the .before method asks if this date is before the following date, which is another new Date with variables representing the current date plugged in, except the year is subtracted by 18 becuase the required age is 18. In English, this is saying "Is this date before this other date?" but in java it is comparing the time in milliseconds between them both since January first, 1970. if the first one is less than the second, then the statement will be true. Now if the user is in fact over 18, then an option panel will pop up saying "Access Granted.". What if the user is not old enough? well a very similar line of code is written to solve this, however, instead of .before, .after is used. Also the month needs to have 1 added to it, otherwise The program would say you were not old enough if you turned 18 in the current month! but what about if you just turned 18 today? Well, that's what the final else is for. that else ensures that if you are exactly 18 on the current day, you can still get "Access Granted.".
That is how I designed my Age Checker Program.