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.
importjava.io.*;importjava.util.*;publicclassPerson{privateintage;publicPerson(intinitialAge){// Add some more code to run some checks on initialAgeif(initialAge>0){age=initialAge;// System.out.print("Age is not valid, setting age to 0.");// this.age=0;}else{// this.age=initialAge;System.out.println("Age is not valid, setting age to 0.");this.age=0;}}publicvoidamIOld(){// Write code determining if this person's age is old and print the correct statement:// System.out.print(/*Insert correct print statement here*/);if(age<13){System.out.print("You are young.\n");}if(age>=13&&age<18){System.out.print("You are a teenager.\n");}if(age>=18){System.out.print("You are old.\n");}}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 →