fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Person {
  9. private int age;
  10. String str="";
  11. public Person(int initialAge) {
  12. if(initialAge > 0)
  13. {
  14. age = initialAge;
  15. }
  16. else
  17. {
  18. System.out.println("Age is not valid, setting age to 0.");
  19. age = 0;
  20. }
  21. }
  22.  
  23. public void amIOld() {
  24. if(age < 13)
  25. {
  26. str = "You are young.";
  27. }
  28. else if(age > 13 && age < 18)
  29. {
  30. str = "You are a teenager.";
  31. }
  32. else
  33. {
  34. str = "You are old.";
  35. }
  36. System.out.println(str);
  37. System.out.println(age);
  38. }
  39.  
  40. public void yearPasses() {
  41. age += 1;
  42. }
  43. public static void main(String[] args) {
  44. Scanner sc = new Scanner(System.in);
  45. int T = sc.nextInt();
  46. for (int i = 0; i < T; i++) {
  47. int age = sc.nextInt();
  48. Person p = new Person(age);
  49. p.amIOld();
  50. for (int j = 0; j < 3; j++) {
  51. p.yearPasses();
  52. }
  53. p.amIOld();
  54. System.out.println();
  55. }
  56. sc.close();
  57. }
  58. }
Success #stdin #stdout 0.05s 711680KB
stdin
4
-1
10
16
18
stdout
Age is not valid, setting age to 0.
You are young.
0
You are young.
3

You are young.
10
You are old.
13

You are a teenager.
16
You are old.
19

You are old.
18
You are old.
21