In this tutorial, we will guide you through the process of solving the Day 4: Class Vs. Instance programming problem from HackerRank’s “ 30 Days of Code ” challenge.
We will provide comprehensive code solutions in C++, Python, and JavaScript programming languages. With our help, you will gain a better understanding of the problem and learn how to approach and solve similar coding challenges.
Disclaimer: We encourage you to solve this challenge yourself before reading our tutorial. We have provided a detailed explanation of the problem and our solutions to help you check your work.
Hackerrank 30 days of code - Day 4: Class vs. Instance
It’s part of Hackerank’s 30 days of code. A series of 30-day programming challenges. Where you can learn new programming languages by solving the coding challenges.
Problem Statement and Explanation
In this problem, we will learn the difference between class and instance using object-oriented programming. we will return the following string based on the following conditions:
- If the age is 0 or less, we will print
Age is not valid, setting age to 0.
and set the age to 0. - There are 3 conditions to check the age:
- If the age is less than 13, we will print
You are young.
- If the age is greater than or equal to 13 and less than 18, we will print
You are a teenager.
- Otherwise, we will print
You are old.
- If the age is less than 13, we will print
- There will be two methods:
-
amIOld()
method to check if the person is old or not. -
yearPasses()
method to increment the age of the person.
-
The Person
class is provided in the editor. We have to write the following methods inside the class:
Operators Hackerrank solution in Python
Operators Hackerrank solution in JavaScript
Test Case of Class vs. Instance Hackerrank Solution
Tested on the Hackerrank platform for the following test case and passed all the test cases. Enclosed below is the screenshot of the test case.
Problem statement is taken from Hackerrank, and the solutions are implemented by CodePerfectPlus team
Top comments (0)