Create a class to display personal information and calculate

Create a class to display personal information and calculates the age of the person The class includes: Three data members: First Name Last Name Year of birth A function Age() to print the calculated age in the months, days and years like 22 years, 10 months and 5 days old Give each data member a value Display All information along with calculated age.

Solution

AgeCalculator.java

import java.util.Calendar;
import java.util.Scanner;

public class AgeCalculator {
   // Declaring static variables
   static String firstname, lastname;
   static int birth_year;

   // Creating reference
   static Calendar c = null;

   public static void main(String[] args) {

       // Creating the Object for the Calendar class
       c = Calendar.getInstance();

       // Scanner class object is used to read the inputs entered by the user
       Scanner sc = new Scanner(System.in);
       // Getting the user name entered by the user
       System.out.print(\"Enter Firstname :\");
       firstname = sc.next();

       // Getting the last name entered by the user
       System.out.print(\"Enter Lastname :\");
       lastname = sc.next();

       // validating the Birth year entered by the user
       while (true) {
           System.out.print(\"Enter year of Birth :\");
           birth_year = sc.nextInt();

           /*
           * If birth year is less than current year then it display error
           * message and prompts user to enter again
           */
           if (birth_year > c.get(Calendar.YEAR)) {
               System.out.println(\":: Birth year must be less than \"+ c.get(Calendar.YEAR) + \" ::\");
               continue;
           } else
               break;
       }

       age();

   }

   private static void age() {
       // Declaring local variables
       int years, months, days;
       // Getting the current year
       years = c.get(Calendar.YEAR);

       // Getting the Current month
       months = c.get(Calendar.MONTH);

       // Getting the current day of the month
       days = c.get(Calendar.DAY_OF_MONTH);

       // calculating the age
       years = years - birth_year;

       // Displaying the name
       System.out.println(\"\ Name :\" + firstname + \" \" + lastname);

       // Displaying the age
       System.out.println(\"Age :\" + years + \" years \" + months+ \" months and \" + days + \" days old\");

   }

}

_____________________________________

Output:

Enter Firstname :Kane
Enter Lastname :Williams
Enter year of Birth :1998

Name :Kane Williams
Age :18 years 9 months and 23 days old

_______________Thank You

 Create a class to display personal information and calculates the age of the person The class includes: Three data members: First Name Last Name Year of birth
 Create a class to display personal information and calculates the age of the person The class includes: Three data members: First Name Last Name Year of birth

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site