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.
It just don't make sense, my code output result same as the output but got errors, the only difference was their is a space or newline for every sysout of my code.
importjava.util.*;publicclassPerson{privateintage;publicPerson(intinitialAge){// Add some more code to run some checks on initialAgeif(initialAge<0){this.age=0;System.out.println("Age is not valide, setting age to 0");}else{this.age=initialAge;}}publicvoidamIOld(){// Write code determining if this person's age is old and print the correct statement:System.out.println(/*Insert correct print statement here*/);if(age<13){System.out.println("You are young.");}elseif(age>=13&&age<18){System.out.println("You are a teenager.");}else{System.out.println("You are old.");}}publicvoidyearPasses(){// Increment this person's age.age++;}publicstaticvoidmain(String[]args){Scannersc=newScanner(System.in);intT=sc.nextInt();for(inti=0;i<T;i++){intage=sc.nextInt();Personp=newPerson(age);p.amIOld();for(intj=0;j<3;j++){p.yearPasses();}p.amIOld();System.out.println();}sc.close();}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Day 4: Class vs. Instance
You are viewing a single comment's thread. Return to all comments →
It just don't make sense, my code output result same as the output but got errors, the only difference was their is a space or newline for every sysout of my code.