Find the Age of a person C# Handson Solution Cognizant
Write a C# program that gets a person’s date of birth as input and calculates his/her age and display the age. The program should also check whether the person is an adult or child. Display the results as given in the sample output.
Create a class called Person.
1. Populate the Person class with the following private fields. :
· String firstName // stores the first name of the person
· String lastName //stores the last name of the person.
· DateTime dob // stores the date of birth of the person
2. Add read-write properties for the above three instance fields and store the information
3. Add read-only propertiy Adult that return the following computed information:
public String Adult // Check the age ,of the person , and if he is 18 or above return a string “Adult” and if he is below 18 return “Child”.
4. Include a Method DisplayDetails
public void DisplayDetails() // Displays the details of the person.
5. Include a method with the below signature that returns the age of the person.
public int GetAge(DateTime dob)
Note:
In Sample input we have assumed the date as 16/07/2021, but you assume the Date as Today’s Date.
Create objects for person from a class called Program that contains the Main method and display the details from the DisplayDetails method.
Note:
Don’t create new namespaces.
Create classes with pubic access specifier.
Follow the naming conventions strictly.
Sample input 1:
Enter first name
Alice
Enter last name
Moses
Enter date of birth in yyyy/mm/dd/ format
1999/12/23
Sample Output 1:
First Name: Alice
Last Name: Moses
Age: 21
Adult
Sample input 2:
Enter first name
Joe
Enter last name
Noel
Enter date of birth in yyyy/mm/dd/ format
2006/10/15
Sample Output 2:
First Name: Joe
Last Name: Noel
Age: 14
Child
BirthdayProject.cs
using System; public class Person { private String firstName; private String lastName; private DateTime dob; public String FirstName { get { return firstName; } set { firstName = value; } } public String LastName { get { return lastName; } set { lastName = value; } } public DateTime Dob { get { return dob; } set { dob = value; } } public string Adult { get { if (GetAge(dob) < 18) return "Child"; else return "Adult"; } } public void DisplayDetails() { Console.WriteLine("First Name: " + firstName); Console.WriteLine("Last Name: " + lastName); Console.WriteLine("Age: " + GetAge(dob)); Console.WriteLine(Adult); } public int GetAge(DateTime dob) { DateTime Now = DateTime.Now; int age = DateTime.Now.Year - dob.Year; if (DateTime.Now.DayOfYear < dob.DayOfYear) { age = age - 2; } //This is for leap year return age; } } public class BirthdayProject //DO NOT change the class name { //implement code here public static void Main(string[] args) { Person person = new Person(); Console.WriteLine("Enter first name"); person.FirstName = Console.ReadLine(); Console.WriteLine("Enter last name"); person.LastName = Console.ReadLine(); Console.WriteLine("Enter date of birth in yyyy/mm/dd format"); person.Dob = Convert.ToDateTime(Console.ReadLine()); person.DisplayDetails(); } }