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

Can someone please help me finishing this \"Calculate the user\'s age\" program?

ID: 653862 • Letter: C

Question

Can someone please help me finishing this "Calculate the user's age" program?

First off, this is what it has to display: (example)

Enter the month you were born (1 to 12) : 5

Enter the day of the month you were born: 16

Enter the year you were born (four digits): 1959

Output should be:

-----------------------------------------------------

Your birth day is May 16, 1959

Today's date is May 21, 2015

Your age is: 55

----------------------------------------------------

1. Add code to this class that gets the current date and then uses 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 110 years before the current year.

2. Add code to create, format, and print the user's birth date and to format and print the current date.

3. Add code to calculate and print the user's age.

Here is the code I have.

AgeCalculatorApp class:

import java.util.*;
import java.text.DateFormat;

public class AgeCalculatorApp
{
    public static void main(String[] args)
    {
        //Get the user's birthdate
        System.out.println("Welcome to the age calculator.");
        Scanner sc = new Scanner(System.in);
        int birthMonth = Validator.getInt(sc, "Enter the month you were born (1 to 12): ",
            0, 13);
        int birthDay = Validator.getInt(sc, "Enter the day of the month you were born: ",
            0, 32);
        int birthYear = Validator.getInt(sc, "Enter the year you were born (four digits): ",
        1874, 2005);

    }
}

Validator class:

import java.util.Scanner;

public class Validator
{
    public static String getString(Scanner sc, String prompt)
    {
        System.out.print(prompt);
        String s = sc.next();        // read the first string on the line
        sc.nextLine();               // discard any other data entered on the line
        return s;
    }

    public static String getLine(Scanner sc, String prompt)
    {
        System.out.print(prompt);
        String s = sc.nextLine();        // read the whole line
        return s;
    }

    public static int getInt(Scanner sc, String prompt)
    {
        boolean isValid = false;
        int i = 0;
        while (isValid == false)
        {
            System.out.print(prompt);
            if (sc.hasNextInt())
            {
                i = sc.nextInt();
                isValid = true;
            }
            else
            {
                System.out.println("Error! Invalid integer value. Try again.");
            }
            sc.nextLine(); // discard any other data entered on the line
        }
        return i;
    }

    public static int getInt(Scanner sc, String prompt,
    int min, int max)
    {
        int i = 0;
        boolean isValid = false;
        while (isValid == false)
        {
            i = getInt(sc, prompt);
            if (i <= min)
                System.out.println(
                    "Error! Number must be greater than " + min);
            else if (i >= max)
                System.out.println(
                    "Error! Number must be less than " + max);
            else
                isValid = true;
        }
        return i;
    }

    public static double getDouble(Scanner sc, String prompt)
    {
        boolean isValid = false;
        double d = 0;
        while (isValid == false)
        {
            System.out.print(prompt);
            if (sc.hasNextDouble())
            {
                d = sc.nextDouble();
                isValid = true;
            }
            else
            {
                System.out.println("Error! Invalid decimal value. Try again.");
            }
            sc.nextLine(); // discard any other data entered on the line
        }
        return d;
    }

    public static double getDouble(Scanner sc, String prompt,
    double min, double max)
    {
        double d = 0;
        boolean isValid = false;
        while (isValid == false)
        {
            d = getDouble(sc, prompt);
            if (d <= min)
                System.out.println(
                    "Error! Number must be greater than " + min);
            else if (d >= max)
                System.out.println(
                    "Error! Number must be less than " + max);
            else
                isValid = true;
        }
        return d;
    }
}

Thanks for your time!

Explanation / Answer

import java.util.Scanner;
import java.util.*;
import java.text.DateFormat;

class AgeCalculatorApp
{
     public static void main(String[] args)
     {
         //Get the user's birthdate
         System.out.println("Welcome to the age calculator.");
         Scanner sc = new Scanner(System.in);
         int birthMonth = getInt(sc, "Enter the month you were born (1 to 12): ",
             0, 13);
         int birthDay = getInt(sc, "Enter the day of the month you were born: ",
             0, 32);
         int birthYear = getInt(sc, "Enter the year you were born (four digits): ",
         1874, 2005);
           LocalDate dob = new LocalDate(birthYear, birthMonth, birthDay);
        Date date=new Date();

        Period period = new Period(dob, date, PeriodType.yearMonthDay());
        System.out.println(period.getYears() + " years and " +
                           period.getMonths() + " months");

    }


//Validator class:



     public static String getString(Scanner sc, String prompt)
     {
         System.out.print(prompt);
         String s = sc.next();        // read the first string on the line
         sc.nextLine();               // discard any other data entered on the line
         return s;
     }

    public static String getLine(Scanner sc, String prompt)
     {
         System.out.print(prompt);
         String s = sc.nextLine();        // read the whole line
         return s;
     }

    public static int getInt(Scanner sc, String prompt)
     {
         boolean isValid = false;
         int i = 0;
         while (isValid == false)
         {
             System.out.print(prompt);
             if (sc.hasNextInt())
             {
                 i = sc.nextInt();
                 isValid = true;
             }
             else
             {
                 System.out.println("Error! Invalid integer value. Try again.");
             }
             sc.nextLine(); // discard any other data entered on the line
         }
         return i;
     }

    public static int getInt(Scanner sc, String prompt,
     int min, int max)
     {
         int i = 0;
         boolean isValid = false;
         while (isValid == false)
         {
             i = getInt(sc, prompt);
             if (i <= min)
                 System.out.println(
                     "Error! Number must be greater than " + min);
             else if (i >= max)
                 System.out.println(
                     "Error! Number must be less than " + max);
             else
                 isValid = true;
         }
         return i;
     }

    public static double getDouble(Scanner sc, String prompt)
     {
         boolean isValid = false;
         double d = 0;
         while (isValid == false)
         {
             System.out.print(prompt);
             if (sc.hasNextDouble())
             {
                 d = sc.nextDouble();
                 isValid = true;
             }
             else
             {
                 System.out.println("Error! Invalid decimal value. Try again.");
             }
             sc.nextLine(); // discard any other data entered on the line
         }
         return d;
     }

    public static double getDouble(Scanner sc, String prompt,
     double min, double max)
     {
         double d = 0;
         boolean isValid = false;
         while (isValid == false)
         {
             d = getDouble(sc, prompt);
             if (d <= min)
                 System.out.println(
                     "Error! Number must be greater than " + min);
             else if (d >= max)
                 System.out.println(
                     "Error! Number must be less than " + max);
             else
                 isValid = true;
         }
         return d;
     }
    
    
    
    
}