In this HackerRank Day 4: Class vs. Instance problem solution Write a Person class with an instance variable, age, and a constructor that takes an integer, initialAge, as a parameter. The constructor must assign initialAge to age after confirming the argument passed as initialAge is not negative; if a negative argument is passed as initialAge, the constructor should set age to 0 and print Age is not valid, setting age to 0.. In addition, you must write the following instance methods:
  1. yearPasses() should increase the age instance variable by 1.
  2. amIOld() should perform the following conditional actions:
  3. If age < 13, print You are young..
  4. If age >= 13 and age < 18, print You are a teenager..
  5. Otherwise, print You are old..

HackerRank Day 4: Class vs. Instance problem solution


Problem solution in Python programming.

class Person:
        def __init__(self,initialAge):
       		# Add some more code to run some checks on initialAge
            if initialAge < 0:
                print('Age is not valid, setting age to 0.')
                self.age = 0
            else:
                self.age = initialAge
        def amIOld(self):
            # Do some computations in here and print out the correct statement to the console
            if self.age < 13:
                print('You are young.')
            elif self.age >=13 and self.age < 18:
                print('You are a teenager.')
            else:
                print('You are old.')
        def yearPasses(self):
            # Increment the age of the person in here      
            self.age = self.age + 1



Problem solution in Java programming.

public class Person {
    private int age;	
  
	public Person(int initialAge) {
  		// Add some more code to run some checks on initialAge
        if (initialAge<0){
            System.out.println("Age is not valid, setting age to 0.");
            this.age=0;
        }else{
            this.age=initialAge;
        }
            
	}

	public void amIOld() {
  		// Write code determining if this person's age is old and print the correct statement:
        int tempage=this.age;
        if (tempage<13)
            System.out.println("You are young.");
        else if (tempage>=13 && tempage<18)
            System.out.println("You are a teenager.");
        else
            System.out.println("You are old.");
	}

	public void yearPasses() {
  		// Increment this person's age.
        this.age++;
	}




Problem solution in C++ programming.

class Person{
public:
   int age ;
   Person(int initialAge);
   void amIOld();
   void yearPasses();
};
Person::Person(int initialAge){
  // Add some more code to run some checks on initialAge
    if (initialAge < 0) {
        age = 0;
        cout << "Age is not valid, setting age to 0." << endl;
    } else {
        age = initialAge;
    }
}
void Person::amIOld(){
    // Do some computations in here and print out the correct statement to the console 
    switch (age) {
        case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: case 10: case 11: case 12:
            cout << "You are young." << endl;
            break;
        case 13: case 14: case 15: case 16: case 17:
            cout << "You are a teenager." << endl;
            break;
        default:
            cout << "You are old." << endl;
            break;
    }
}
    

void Person::yearPasses(){
  // Increment the age of the person in here
    age++;
}



Problem solution in Javascript programming.

function Person(initial_Age){
    if (initial_Age<0){
        console.log('Age is not valid, setting age to 0.')
        initial_Age = 0;
    }
    this.age = initial_Age;
}
Person.prototype.amIOld = function() {
    if (this.age < 13)
        console.log('You are young.');
    else if (this.age < 18)
        console.log('You are a teenager.');
    else
        console.log('You are old.');
}

Person.prototype.yearPasses = function() {
    this.age++;
}