Class Vs Instance In C#

We all know the OOPs concept is very popular and easy to work with and there are many advantages as well to using it. Now, in C# whenever we are going to perform any functional activity or the operations to divide inside the different classes then this class vs. instance properties comes into use.

Today we are going to solve the particular basic challenge of the hackerrank problem statement where we have the following as the task to solve,

  • We have an age of the particular person 
  • We have to compare it with the different criteria and print the message accordingly as mentioned below,
     
    • Age less then 0 print age not valid set age to 0.
    • Age between 0 to 13 print you are young
    • Age between 13 to 18 print you are teenager
    • Age greater than 18 print you are old
       
  • One more task we have that after comparing the age we have to calculate the age after 3 years and print the status of that age also.

So, now we have the task and we are going to divide the task function-wise, So, our first task is going to be to get the initial age and check it should not be less than zero if this so then set it to zero.

  • First task should be to check the age is not less then zero if this so then set to zero
  • Second task to calculate the age and print the status as per the age.
  • The third task to create the function which we can call multiple times as per requirement and which is going to increment the age by one year.

So, for the first task, our function is going to look like this,

public int age;     
public Person(int initialAge)     //getting the initialAge
{
     // Add some more code to run some checks on initialAge
     if (initialAge >= 0)         //Comparing that it should not be less than zero
     {
        age = initialAge;         //if not less than zero then set to age
     }
     else
     {
        Console.WriteLine("Age is not valid, setting age to 0.");  // less then zero then through 
        error
        age = 0;                  //set age to zero
     }
}

Now, as we have checked the person should be less then zero year and we got the person age as zero year if it less then zero.

Now we are going to do some computational tasks and work to check whether the person is young, teenager, or old.

Also, for this task, we are going to use the different functions as below,

public void amIOld() 
{
    if (age < 13)
    {
        Console.WriteLine("You are young.");
    }
    else if (age >= 13 && age < 18)
    {
        Console.WriteLine("You are a teenager.");
    }
    else
    {
        Console.WriteLine("You are old.");
    }
}

And now the third task is to check the age status after three years so, for that task we are just going to make a function which we can used to call multiple times as per we want to check the increment of the age.

As in our case we want to check the age after three years then we are going to call the below function three times. which is simply incrementing the age by one year only.

public void yearPasses() 
{
     // Increment the age of the person in here
     age += 1;
     age = age + 1;
}

Now, our three tasks are done so check the main function for calling these functions in the sequence, from which we can achieve our desired output for the multiple age input.

static void Main(String[] args) 
{
    int T=int.Parse(Console.In.ReadLine());
    for (int i = 0; i < T; i++) 
    {
          int age=int.Parse(Console.In.ReadLine());
          Person p=new Person(age);
          p.amIOld();
          for (int j = 0; j < 3; j++) 
          {
              p.yearPasses();
          }
          p.amIOld();
          Console.WriteLine();
     }
}

NOTE
The following full source code contains the solution for the Hackerrank 30daysofcode Day 4 solution.

using System;
using System.Collections.Generic;
using System.IO;

class Person 
{
    public int age;     
	public Person(int initialAge) 
    {
        // Add some more code to run some checks on initialAge
         if (initialAge >= 0)
        {
            age = initialAge;
        }
        else
        {
            Console.WriteLine("Age is not valid, setting age to 0.");
            age = 0;
        }
     }
     public void amIOld() 
     {
        // Do some computations in here and print out the correct statement to the console 
        if (age < 13)
        {
            Console.WriteLine("You are young.");
        }
        else if (age >= 13 && age < 18)
        {
            Console.WriteLine("You are a teenager.");
        }
        else
        {
            Console.WriteLine("You are old.");
        }
     }

     public void yearPasses() 
     {
        // Increment the age of the person in here
        age+=1;
     }
     
     static void Main(String[] args) 
     {
        int T=int.Parse(Console.In.ReadLine());
        for (int i = 0; i < T; i++) 
        {
            int age=int.Parse(Console.In.ReadLine());
            Person p=new Person(age);
            p.amIOld();
            for (int j = 0; j < 3; j++) 
            {
                  p.yearPasses();
            }
            p.amIOld();
            Console.WriteLine();
        }
    }
}

 


Similar Articles