Question
1. Add code to AgeCalculator.java to get current date and then use the current year to validate the birth year the user enters. The user should not be allowed to enter a year after the current year or more than 112 years before the current year 2. Add code to create, format and print user
Explanation / Answer
importjava.util.*; importjava.io.*; publicclassAgeCalculation{ publicstaticvoidmain(String[]args)throwsIOException{ intday=1,month=0,year=1,ageYears,ageMonths,ageDays; BufferedReaderin=newBufferedReader(newInputStreamReader(System.in)); Calendarcd=Calendar.getInstance(); try{ System.out.print("Enteryearofyourdateofbirth:"); year=Integer.parseInt(in.readLine()); if(year>cd.get(Calendar.YEAR)){ System.out.print("Invaliddateofbirth."); System.exit(0); } System.out.print("Entermonthofyourdateofbirth:"); month=Integer.parseInt(in.readLine()); if(month12){ System.out.print("Pleaseentermonthebetween1to12."); System.exit(0); } else{ month--; if(year==cd.get(Calendar.YEAR)){ if(month>cd.get(Calendar.MONTH)){ System.out.print("Invalidmonth!"); System.exit(0); } } } System.out.print("Enterdayofyourdateofbirth:"); day=Integer.parseInt(in.readLine()); if(month==0||month==2||month==4||month==6||month==7|| month==9||month==11){ if(day>31||day30||daycd.get(Calendar.DAY_OF_MONTH)){ System.out.print("Invaliddate!"); System.exit(0); } } } } catch(NumberFormatExceptionne){ System.out.print(ne.getMessage()+"isnotalegalentry!"); System.out.print("Pleaseenternumber."); System.exit(0); } Calendarbd=newGregorianCalendar(year,month,day); ageYears=cd.get(Calendar.YEAR)-bd.get(Calendar.YEAR); if(cd.before(newGregorianCalendar(cd.get(Calendar.YEAR),month,day))){ ageYears--; ageMonths=(12-(bd.get(Calendar.MONTH)+1))+(bd.get(Calendar.MONTH)); if(day>cd.get(Calendar.DAY_OF_MONTH)){ ageDays=day-cd.get(Calendar.DAY_OF_MONTH); } elseif(daycd.get(Calendar.DAY_OF_MONTH)) ageDays=day-cd.get(Calendar.DAY_OF_MONTH)-day; elseif(day