|
| 1 | +/* |
| 2 | +
|
| 3 | +
|
| 4 | +Write a Person class with an instance variable,age , and a constructor that takes an integer,initialAge , as a parameter. The constructor must assign initialAge to age after confirming the argument passed as initialAge is not negative; if a negative argument is passed as , the constructor should set to and print Age is not valid, setting age to 0.. In addition, you must write the following instance methods: |
| 5 | +
|
| 6 | +yearPasses() should increase the instance variable by . |
| 7 | +amIOld() should perform the following conditional actions: |
| 8 | +If , print You are young.. |
| 9 | +If and , print You are a teenager.. |
| 10 | +Otherwise, print You are old.. |
| 11 | +To help you learn by example and complete this challenge, much of the code is provided for you, but you'll be writing everything in the future. The code that creates each instance of your Person class is in the main method. Don't worry if you don't understand it all quite yet! |
| 12 | +
|
| 13 | +Note: Do not remove or alter the stub code in the editor. |
| 14 | +
|
| 15 | +Input Format |
| 16 | +
|
| 17 | +Input is handled for you by the stub code in the editor. |
| 18 | +
|
| 19 | +The first line contains an integer, (the number of test cases), and the subsequent lines each contain an integer denoting the of a Person instance. |
| 20 | +
|
| 21 | +Constraints |
| 22 | +
|
| 23 | +Output Format |
| 24 | +
|
| 25 | +Complete the method definitions provided in the editor so they meet the specifications outlined above; the code to test your work is already in the editor. If your methods are implemented correctly, each test case will print or lines (depending on whether or not a valid was passed to the constructor). |
| 26 | +
|
| 27 | +Sample Input |
| 28 | +
|
| 29 | +4 |
| 30 | +-1 |
| 31 | +10 |
| 32 | +16 |
| 33 | +18 |
| 34 | +Sample Output |
| 35 | +
|
| 36 | +Age is not valid, setting age to 0. |
| 37 | +You are young. |
| 38 | +You are young. |
| 39 | +
|
| 40 | +You are young. |
| 41 | +You are a teenager. |
| 42 | +
|
| 43 | +You are a teenager. |
| 44 | +You are old. |
| 45 | +
|
| 46 | +You are old. |
| 47 | +You are old. */ |
| 48 | + |
| 49 | + |
| 50 | +public class Person { |
| 51 | + private int age; |
| 52 | + |
| 53 | + public Person(int initialAge) { |
| 54 | + // Add some more code to run some checks on initialAge |
| 55 | + if(initialAge<0) |
| 56 | + { |
| 57 | + age=0; |
| 58 | + System.out.println("Age is not valid, setting age to 0."); |
| 59 | + |
| 60 | + } |
| 61 | + else |
| 62 | + { |
| 63 | + age=initialAge; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + public void amIOld() { |
| 68 | + // Write code determining if this person's age is old and print the correct statement: |
| 69 | + if(age<13) |
| 70 | + { |
| 71 | + System.out.println("You are young."); |
| 72 | + } |
| 73 | + else if(age>=13 && age<18) |
| 74 | + { |
| 75 | + System.out.println("You are a teenager."); |
| 76 | + } |
| 77 | + else |
| 78 | + { |
| 79 | + System.out.println("You are old."); |
| 80 | + } |
| 81 | + |
| 82 | + |
| 83 | + |
| 84 | + } |
| 85 | + |
| 86 | + public void yearPasses() { |
| 87 | + // Increment this person's age. |
| 88 | + age++; |
| 89 | + } |
| 90 | + |
0 commit comments