Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program named ComputeAge that determines the user\'s age in years, month

ID: 3633985 • Letter: W

Question

Write a program named ComputeAge that determines the user's age in years, months, and days. The user enters his or her birthday (Using Scanner), and the program determines how old the person is on the current date. The following example shows what the user will see on the screen, assuming that the current date is July 9, 1999:

Enter the year you were born: 1980
Enter the month you were born (1-12): 7
Enter the day you were born: 26
You are 18 years, 11 months, and 13 days old

Requirements:


The words "years," "months," and "days" must be printed in singular form when appropriate.

Your program must detect the following input errors:
1)Input is not numeric
2)Input is too small (the year cannot be less than 1800. The month and day cannot be less than 1)
3)Input is too large (The birth year cannot exceed the current year/The birth month cannot exceed 12)

If any of these errors occur, the program must print an error message and ask the user to renter the offending input:


Reference:

Enter the year you were born: 1700 AD
Input was not an integer
Enter the year you were born: 1700
Input must be at least 1800
Enter the year you were born: 2100
Input must no more than 1999
Enter the year you were born: 1978
Enter the month you were born (1-12): 2
Enter the day you were born: 29
Input must no more than 28
Enter the day you were born: 27
You are 21 years, 4 months, and 12 days old //comes out to be this

Explanation / Answer

Dear, Java Code: (Age.java) import java.io.*; //class age declaration public class Age { public static void main(String[] args) { //variable required to read user input and processing int bd = 0,bm = 0,by = 0,cd=9,cm=7,cy=1999,ad,am,ay; int flag1,flag2,flag3; //loop until user enters valid input do { flag1=1; System.out.println("Enter the year you were born:"); String string = ""; InputStreamReader input = new InputStreamReader(System.in); //read user input as string BufferedReader reader = new BufferedReader(input); //exception handling for input read try{ string = reader.readLine(); } catch(Exception e) {} //exception handling for valid integer try { by = Integer.parseInt(string); } catch(NumberFormatException nfe) { System.out.println("Input was not an integer"); flag1 =0; continue; } //condition to check the input is between the valid range if(by=1999 && flag1!=0) { flag1=0; System.out.println("Input must no more than 1999"); } else flag1=1; }while(flag1!=1); //loop until user enters valid input do { flag2=1; System.out.println("Enter the month you were born (1-12):"); String string = ""; InputStreamReader input = new InputStreamReader(System.in); //read user input as string BufferedReader reader = new BufferedReader(input); //exception handling for input read try{ string = reader.readLine(); } catch(Exception e) {} //exception handling for valid integer try { bm = Integer.parseInt(string); } catch(NumberFormatException nfe) { System.out.println("Input was not an integer"); flag2 =0; continue; } //condition to check the input is between the valid range if(bm=12 && flag2!=0) { flag2=0; System.out.println("Input must no more than 12"); } else flag2=1; }while(flag2!=1); //loop until user enters valid input do { flag3=1; System.out.println("Enter the day you were born: "); String string = ""; InputStreamReader input = new InputStreamReader(System.in); //read user input as string BufferedReader reader = new BufferedReader(input); //exception handling for input read try{ string = reader.readLine(); } catch(Exception e) {} //exception handling for valid integer try { bd = Integer.parseInt(string); } catch(NumberFormatException nfe) { System.out.println("Input was not an integer"); flag3 =0; continue; } //condition to check the input is between the valid range if(bd=28 && flag3!=0) { flag3=0; System.out.println("Input must no more than 28"); } else flag3=1; }while(flag3!=1); //Calculating the days, months and years if(cd
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote