Sort by

recency

|

1898 Discussions

|

  • + 0 comments

    public class Solution { int age;

    public Solution(int initialAge){
        if(initialAge >0){
            age =initialAge;
            }else{
                 System.out.println("Age is not valid, setting age to 0.");
                 this.age=0;
            }
    
        }
    
    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 if(age>=18){
                System.out.println("You are old.");
            }          
    }    
    public void yearPasses(){
        this.age = ++age;
    }
    
    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner scan = new Scanner(System.in);
        int T = scan.nextInt();
    
        for (int i = 0; i<T;i++){
            int age = scan.nextInt();
            Solution p = new Solution (age);
            p.amIOld();
            for(int j=0;j<3;j++){
                p.yearPasses();
            }
            p.amIOld();
            System.out.println();
    
        }
        scan.close();
    

    } }

  • + 1 comment
    using System;
    using System.Collections.Generic;
    using System.IO;
    
    class Person {
        public int age;     
    	 public Person(int initialAge)
         {
            age = initialAge < 0 ? 0 : initialAge;
            if(initialAge <0)
                Console.WriteLine("Age is not valid, setting age to 0.");
         }
         public void amIOld() {
            // Do some computations in here and print out the correct statement to the console 
            if(age < 13)
                Console.WriteLine("You are young.");
            else if(age >= 13 && age <18)
                Console.WriteLine("You are a teenager.");
            else
                Console.WriteLine("You are old.");
                
         }
    
         public void yearPasses() {
            // Increment the age of the person in here
            age ++;
         }
    
    static void Main(String[] args) {
            int T=int.Parse(Console.In.ReadLine());
            for (int i = 0; i < T; i++) {
                int age=int.Parse(Console.In.ReadLine());
                Person p=new Person(age);
                 p.amIOld();
                    for (int j = 0; j < 3; j++) {
                      p.yearPasses();
                    }
                    p.amIOld();
                    Console.WriteLine();
            }
        }
    }
    
  • + 0 comments

    JavaScript solution:

    function Person(initialAge){
        if (initialAge < 0) {
            console.log("Age is not valid, setting age to 0.");
            this.age = 0;
        } else {
            this.age = initialAge;
        }
        
      this.amIOld=function(){
       if (this.age < 13) {
        console.log("You are young.");
       } else if (this.age >= 13 && this.age < 18) {
        console.log("You are a teenager."); 
       } else {
        console.log("You are old.");
       };
       
      };
       this.yearPasses=function(){
              this.age += 1;
       };
    }
    
  • + 0 comments

    4th exercise isnt available in C and idk any other language help

  • + 1 comment

    Python3 solution: On the same point as per my earlier message, I referred to the solution present in the Editorial section as well and tried it. It is still failing with the initial test case where initalAge is -1. Can anyone please advise on what is wrong in the code?

    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:
            print("You are young.")
    
    def yearPasses(self):
        self.age += 1