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

Download the file from this link at the bottom. https://www.murach.com/shop/mura

ID: 3675144 • Letter: D

Question

Download the file from this link   at the bottom.

https://www.murach.com/shop/murach-s-java-programming-detail

1. Open project named ch13_ex2_AgeCalculator thats in the ex_starts directory. Then , review the code in the Age calculatorApp class.

2. 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.

3. Add code to create, format, and print the users birth date and to format and print the current date.

4 . Add code to the calculate and print the users age.

5. Test this project with a variety of dates to be sure it works.     Make sure there is a validator class.

Explanation / Answer

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, 2016);
        //SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
        Date date = new Date();
        System.out.println("Current date is:"+date.getDate());
      
       Calendar cal = Calendar.getInstance();

        cal.set(Calendar.DAY_OF_MONTH, Integer.parseInt(birthMonth);
        cal.set(Calendar.MONTH, Integer.parseInt(birthDay);
        cal.set(Calendar.YEAR, Integer.parseInt(birthYear);
        Date firstDate = cal.getTime();

        Date secondDate = cal.getDate();


        long diff = secondDate.getTime() - firstDate.getTime();

        System.out.println ("Days: " + diff / 1000 / 60 / 60 / 24);

      
    }
}

/* icould not get the 4th line that is caliculate and print age but the code will caliculate the difference*/

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;
    }
}

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