Sort by

recency

|

1915 Discussions

|

  • + 0 comments

    Here is problem solution in Python, Java, C++, C and javascript - https://programmingoneonone.com/hackerrank-day-4-class-vs-instance-30-days-of-code-solution.html

  • + 0 comments

    what is wrong with my code please assist

    this.age=initialAge; if(initialAge<0){ this.age=0; System.out.println("Age is not valid, settin age to 0."); }

    }
    
    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() {
        // Increment this person's age.
        this.age+=1;
    }
    
  • + 0 comments

    TypeScript solution. Note, I added an ageBy helper method to the Person class for to DRY up duplicate aging code :

    function main() {
        // Enter your code here
        
        const testAmount = Number(readLine())
        
        const lastTestIndex = 4
        
        Array.from({length: testAmount}).forEach((_, index) => {
            const p1 = new Person(Number(readLine()))
            p1.amIOld()
            p1.ageByYears(3)
            p1.amIOld()
            
            if (index !== lastTestIndex) process.stdout.write('\n')
        })
    }
    
    class Person {
        age = 0
        
        constructor(initalAge: number) {
            if (initalAge < 0) {
                process.stdout.write('Age is not valid, setting age to 0.\n')
                return
            }
            
            this.age = initalAge
        }
        
        yearPasses(): void {
            this.age++
        } 
        
        amIOld(): void {
            if (this.age < 13) {
                process.stdout.write('You are young.\n')
            } else if (this.age >= 13 && this.age < 18) {
                process.stdout.write('You are a teenager.\n')
            } else {
                process.stdout.write('You are old.\n')
            }
        }
        
        ageByYears(years: number) {
            Array.from({length: years}).forEach(_ => this.yearPasses())
        }
    }
    
  • + 0 comments

    How am i supposed to debug if i am not allowed to change/ update the main[] method at all? This program seems to have problem in itself i.e. the testcase is failing because of extra blank space but i am unable to remove that additional print statement from the main[] method.

  • + 0 comments

    C# code of the Person class..

    public int age;     
        
        public Person(int initialAge) {
            if (initialAge > 0) {
                age = initialAge;
            } else {
                age = 0;
                Console.WriteLine("Age is not valid, setting age to 0.");
            }
        }
        
        public void amIOld() {
            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() {
            age++;
        }