/** * Computes the age based on MOB and YOB * Suggested exercises: * - Use the reverse condition in the if * - Validate YOB * - Validate also the type the input (use Type.java) * - Sort the age into three intevals: kid (<=18), adult (18,65), senior (>=65) * - Add a happy birthday message */ import java.util.Scanner; public class Age { public static void main (String[] args) { Scanner scan = new Scanner(System.in); int MOB, YOB, Months, Years, MonthsLived; final int CurrentMonth = 2; final int CurrentYear = 2009; System.out.print("Enter Month of Birth: "); MOB = scan.nextInt(); System.out.print("Enter Year of Birth: "); YOB = scan.nextInt(); if (MOB<1 || MOB>12) System.out.println("Wrong Month of Birth"); else { MonthsLived = CurrentMonth + CurrentYear * 12 - MOB - YOB * 12; Months = MonthsLived % 12; // Remainder from integer division Years = MonthsLived / 12; // Integer division System.out.println("You are " + Years + " years and " + Months +" months old"); } } }