Day 4: Class vs. Instance | 30 Days of Coding | HackerRank | Java Language

**Before watching this make sure that you’ve tried enough**
Language: Java 7

public class Person {
    private int age;	
  
    public Person(int initialAge) {
  	// Add some more code to run some checks on initialAge
        if(initialAge < 0){
            System.out.println("Age is not valid, setting age to 0.");
            age = 0;
        }
        else age = initialAge;
        
        
    }

    public void amIOld() {
  	// Write code determining if this person's age is old and print the correct statement:
        String stmnt="";
        if(age < 13)stmnt = "You are young.";
        else if(age >=13 && age <18)stmnt = "You are a teenager.";
        else stmnt = "You are old.";
            
        System.out.println(stmnt);
    }

    public void yearPasses() {
  		age++;
    }

 

If this solution helped you, please share this with your friends.
And if you have anything to say, let me know in the comment section.
More solutions are coming so Like our Facebook page and Subscribe to our YouTube channel to stay connected.

Leave a Comment