Class Vs Instance problem solution 30 days of code HackerRank

Input Format
Input is handled for you by the stub code in the editor.
The first line contains an integer,  (the number of test cases), and the  subsequent lines each contain an integer denoting the  of a Person instance.
Output Format
Complete the method definitions provided in the editor so they meet the specifications outlined above; the code to test your work is already in the editor. If your methods are implemented correctly, each test case will print  or  lines (depending on whether or not a valid  was passed to the constructor).
Class Vs Instance problem solution 30 days of code HackerRank

Problem solution in c++ programming language.

class Person{
    public:
       int age;
       Person(int initialAge);
       void amIOld();
       void yearPasses();
    };
    
    Person::Person(int initialAge){
      if(initialAge < 0){
          cout << "Age is not valid, setting age to 0." << endl;
          this->age = 0;
      }else{
          this->age = initialAge;
      }
    }
    
    void Person::amIOld(){
        if(age >= 18){
            cout << "You are old." << endl;
        }
        else if(age >= 13){
            cout << "You are a teenager." << endl;
        }
        else{
            cout << "You are young." << endl;
        }
    }

    void Person::yearPasses(){
        this->age++;
    }

Problem solution in Java programming language.

public class Person {
    private int age; 
  
    public Person(int initialAge) {
        if(initialAge < 0){ // initial age is invalid
            System.out.println("Age is not valid, setting age to 0.");
            this.age = 0;
        }
        else{ // set age to valid initial age
            this.age = initialAge;
        }
    }

    public void amIOld() {
        // Set correct statement to print
        String result = "";
        if(age >= 18){
            result = "You are old.";
        }
        else if(age >=13){
            result = "You are a teenager.";
        }
        else{
            result = "You are young.";
        }
        System.out.println(result);
    }

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

Problem solution in C sharp programming language.

class Person {
    public int age;     
    public Person(int initialAge) {
        if(initialAge < 0){ // initial age is invalid
            Console.WriteLine("Age is not valid, setting age to 0.");
            this.age = 0;
        }
        else{ // set age to valid initial age
            this.age = initialAge;
        }
     }
     public void amIOld() {
        Console.WriteLine(
            (age >= 18) 
            ? "You are old." 
            : (
                (age >=13) 
                ? "You are a teenager." 
                : "You are young." 
            )
        );
     }

     public void yearPasses() {
        this.age++;
     }

Problem solution in python 2 programming language.

class Person:
    def __init__(self, initialAge):
        if initialAge >= 0:
            self.age = initialAge
        else:
            self.age = 0
            print("Age is not valid, setting age to 0.")
            
    def amIOld(self):
        if self.age >= 18:
            print("You are old.")
        elif self.age >= 13:
            print("You are a teenager.")
        else: # age < 13
            print("You are young.")
            
    def yearPasses(self):
        self.age += 1

Problem solution in Python 3 programming language.

class Person:
    def __init__(self, initialAge):
        if(initialAge < 0):
            print("Age is not valid, setting age to 0.")
            self.age = 0
        else:
            self.age = initialAge
            
    def amIOld(self):
        if self.age >= 18:
            print("You are old.")
        elif self.age >= 13:
            print("You are a teenager.")
        else: # age < 13
            print("You are young.")
            
    def yearPasses(self):
        self.age += 1

Problem solution in Ruby programming language.

class Person
    attr_accessor :age
    
    def initialize(initialAge)
        if initialAge < 0
            print("Age is not valid, setting age to 0.\n")
            @age = 0
        else
            @age = initialAge
        end
    end
    
    def amIOld()    
        if @age >= 18
            print("You are old.\n")
        elsif @age >= 13
            print("You are a teenager.\n")
        else
            print("You are young.\n")
        end
    end
    
    def yearPasses()
        @age = age + 1
    end
end

Problem solution in the Swift programming language.

class Person {
    var age: Int = 0

    init (initialAge: Int) {
        if initialAge < 0 {
            print("Age is not valid, setting age to 0.")
        }
        else {
            age = initialAge
        }
    }

    func amIOld () {
        if age >= 18 {
            print("You are old.")
        }
        else if age >= 13 {
            print("You are a teenager.")
        }
        else { // age is < 13
            print("You are young.")
        }
    }

    func yearPasses() {
        age += 1
    }
}

Post a Comment

0 Comments