Sort by

recency

|

1910 Discussions

|

  • + 0 comments

    Python

    class Person: def init(self, initialAge): self.age = initialAge

    @property
    def age(self):
        return self.__age
    
    @age.setter
    def age(self, value):
        if value < 0:
            value = 0
            print("Age is not valid, setting age to 0.")
        self.__age = value
    
    def amIOld(self):
        if self.age < 13:
            print("You are young.")
        elif 13 <= self.age < 18:
            print("You are a teenager.")
        else:
            print("You are old.")
    
    def yearPasses(self):
        self.age += 1
    
  • + 0 comments

    Python code

    class Person:
        def __init__(self,initialAge):
            self.initialAge=initialAge
            if self.initialAge<0:
                print("Age is not valid, setting age to 0.")
        def amIOld(self):
            if self.initialAge < 13:
                print("You are young.") 
            elif self.initialAge>=13 and self.initialAge<18:
                print("You are a teenager.")
            else:
                print("You are old.")
        def yearPasses(self):
            self.initialAge+=1
    
    t = int(input())
    for i in range(0, t):
        age = int(input())         
        p = Person(age)  
        p.amIOld()
        for j in range(0, 3):
            p.yearPasses()       
        p.amIOld()
        print("")
    
  • + 0 comments

    public Person(int initialAge) {

        if(initialAge < 0 ){
            System.out.println("Age is not valid, setting age to 0.");
        }
        else{
            this.age =initialAge;
        }
    }
    
    public void amIOld() {
        if(age < 13){
            System.out.println("You are young.");
        }
        else if( age >= 13 && age < 18 ) {
            System.out.println("You are a teenager.");
        }
        else{
            System.out.println("You are old.");
        }
    
    }
    
    public void yearPasses() {
        age ++;
    }
    
    public static void main(String[] args) {
    
  • + 0 comments

    class Person: def init(self, initialAge): # Corrected constructor name 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 < 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): self.age += 1

  • + 0 comments

    constructor is called only once tat to for -1