π’ Day 4: HackerRank Practice: Day 4 β Class vs Instance in C#

π Hey folks! I'm currently brushing up on my C# skills through HackerRankβs 30 Days of Code series. Hereβs my detailed breakdown and improved solution for Day 4: Class vs Instance.
π Problem Summary
You're asked to create a class Person with:
A constructor that accepts initialAge and validates it.
A method AmIOld() that prints the age category:
< 13: "You are young."
13β17: "You are a teenager."
>= 18: "You are old."
A method YearPasses() that increases the age by 1.
π‘ Bonus Rule:
If the initial age is negative, print:
Age is not valid, setting age to 0.
After printing the initial age category, simulate the passage of 3 years, then print the updated category again.
π‘ Approach
Constructor: Validate age. If negative, set it to 0 and display a message.
AmIOld(): Use if-else to classify age correctly.
YearPasses(): Simple method to increment age by 1.
Main(): Accepts multiple test cases, simulates aging, and prints results.
π¨βπ» Updated C# Implementation
using System;
class Person
{
private int _age;
public int Age => _age;
// Constructor with age validation
public Person(int initialAge)
{
if (initialAge < 0)
{
Console.WriteLine("Age is not valid, setting age to 0.");
_age = 0;
}
else
{
_age = initialAge;
}
}
// Print age category
public void AmIOld()
{
if (_age < 13)
{
Console.WriteLine("You are young.");
}
else if (_age < 18)
{
Console.WriteLine("You are a teenager.");
}
else
{
Console.WriteLine("You are old.");
}
}
// Increment age by 1
public void YearPasses()
{
_age++;
}
// Entry point: handles multiple test cases
static void Main(string[] args)
{
int testCases = int.Parse(Console.ReadLine());
for (int i = 0; i < testCases; i++)
{
int inputAge = int.Parse(Console.ReadLine());
Person person = new Person(inputAge);
person.AmIOld();
for (int j = 0; j < 3; j++)
{
person.YearPasses();
}
person.AmIOld();
Console.WriteLine(); // Spacing between test cases
}
}
}
π’ Sample Input
3
-1
10
16
β Sample Output
Age is not valid, setting age to 0.
You are young.
You are young.
You are young.
You are a teenager.
You are a teenager.
You are old.
π§ Key Takeaways
β Constructor validation ensures objects always have a valid state.
β Encapsulation using private fields and read-only properties improves class design.
β Precise formatting is critical on HackerRank; avoid extra or missing lines.
β Edge case testing (e.g., -1, 13, 17, 18) helps catch off-by-one errors.
β οΈ Common Pitfalls
β Forgetting to simulate the 3-year time skip.
β Printing messages in the wrong order.
β Not handling negative input correctly.
β Adding extra Console.WriteLine() or missing line breaks.
π Final Note
This problem is a great way to reinforce basic class design, conditionals, and method interaction in C#. It also emphasizes clean output formatting β a crucial skill in competitive coding platforms.
π¬ Feel free to share your thoughts, solutions, or questions in the comments below.
π Until next time, happy coding!
Subscribe to my newsletter
Read articles from Priya directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
