In this challenge, we are going to learn about the difference between a class and an instance.
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 initialAge, the constructor should set age to 0 and print "Age is not valid, setting age to 0.".
In addition, you must write the following instance methods:
class Person { private int age; public Person(int initialAge) { if (initialAge < 0) { System.out.println("Age is not valid, setting age to 0."); } else {//from www . ja v a 2s .c o m age = initialAge; } } public void amIOld() { String response = ""; if (age < 13) { response = "You are young."; } else if (age >= 13 && age < 18) { response = "You are a teenager."; } else { response = "You are old."; } System.out.println(response); } public void yearPasses() { // Increment this person's age. age += 1; } } public class Main { public static void main(String[] args) { Person p = new Person(15); p.amIOld(); for (int j = 0; j < 3; j++) { p.yearPasses(); } p.amIOld(); System.out.println(); } }