We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Hiring developers?
  1. Prepare
  2. Tutorials
  3. 30 Days of Code
  4. Day 4: Class vs. Instance
  5. Discussions

Day 4: Class vs. Instance

Problem
Submissions
Leaderboard
Discussions
Editorial
Tutorial

Sort 1831 Discussions, By:

recency

Please Login in order to post a comment

  • elbiadfarah2
    1 week ago+ 1 comment

    what is the problem plz java8

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

    public void amIOld() {
         // Write code determining if this person's age is old and print the correct statement:
        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++;
    }
    
    0|
    Permalink
  • sushanthpentako1
    1 week ago+ 0 comments
    class Person:
        def __init__(self,initialAge):
            self.initialAge=initialAge
            if self.initialAge<0:
                self.initialAge=0
                print('Age is not valid, setting age to 0.')
            else:
                self.initialAge=initialAge
            
        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=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|
    Permalink
  • deshsamu
    4 weeks ago+ 3 comments

    Java 15 Solution-- I am getting wrong answer what's wrong with this code Please anybody tell me.

    import java.io.; import java.util.;

    public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
    
        class Person {
            private int age;
    
            public Person (int initialAge) {
                    if(initialAge < 0 ) {
                            System.out.println(" Age is not valid, setting age to 0.");
                    }
                    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(){
                    this.age++;
            }
        }
    }
    

    }

    0|
    Permalink
  • shreyasgowda399
    1 month ago+ 0 comments

    Simple solution in java public class Person { private int age;

    public Person(int initialAge) 
    {
        if(initialAge <=0)
        {
            age=0;
            System.out.println("Age is not valid, setting age to 0.");
        }
        this.age=initialAge;
        // Add some more code to run some checks on initialAge
    }
    
    public void amIOld() {
        // Write code determining if this person's age is old and print the correct statement:
          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++;
    

    }

    0|
    Permalink
  • lokeshreddy93817
    1 month ago+ 0 comments

    java solution

    import java.io.; import java.util.;

    public class Person { private int age;

    public Person(int initialAge) {
        this.age=initialAge;
    }
    
    public void amIOld() {
     if(this.age<0){
         System.out.println("Age is not valid, setting age to 0.");
         System.out.println("You are young.");
     }else if(this.age<13) {
         System.out.println("You are young.");
     }else if(this.age>=13 &&this.age<18){
         System.out.println("You are a teenager.");
     }else{
         System.out.println("You are old.");
     }
    
    }
    
    public void yearPasses() {
      if(this.age<0){
          this.age=this.age*-1;
      }else{
          this.age++;
      }
    }
    
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();
        for (int i = 0; i < T; i++) {
            int age = sc.nextInt();
            Person p = new Person(age);
            p.amIOld();
            for (int j = 0; j < 3; j++) {
                p.yearPasses();
            }
            p.amIOld();
            System.out.println();
        }
        sc.close();
    }
    

    }

    0|
    Permalink
Load more conversations

Need Help?


View tutorial
View editorial
View top submissions
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy