Java 30 Days of Code - day 4 - Class vs Instance

We have solved the previous problems of Operators and Conditional statements in our previous articles. In this article we will try to go through the concept of Class vs Instance and explain the logics behind the concepts.

Java 30 Days of Code - Day 4- Class vs Instance

Problem statement - 

Write a Person class with an instance variable, , and a constructor that takes an integer, , as a parameter. The constructor must assign  to  after confirming the argument passed as  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:

  1. yearPasses() should increase the  instance variable by .
  2. amIOld() should perform the following conditional actions:
    • If , print You are young..
    • If  and , print You are a teenager..
    • Otherwise, print You are old..
import java.io.*;
import java.util.*;

public class Person {
    private int age;    
  
    public Person(int initialAge) {
        // Add some more code to run some checks on initialAge
          this.age = initialAge;
    }

    public void amIOld() {
        // Write code determining if this person's age is old and print the correct statement:
          if(age<=0){
              age=0;
              System.out.println("Age is not valid, setting age to 0.");
              System.out.println("You are young.");
          }
          else if(age<13){
            System.out.println("You are young.");
          }
          else if(age>=13 && age<18){
              System.out.println("You are a teenager.");
          }
          else{
        System.out.println("You are old.");
          }
        // System.out.println(/*Insert correct print statement here*/);
    }

    public void yearPasses() {
        // Increment this person's age.
          age=age+1;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();
        for (int i = 0; i < T; i++) {
            int age = sc.nextInt();
            Person p = new Person(age);
            p.amIOld();
            for (int j = 0; j < 3; j++) {
                p.yearPasses();
            }
            p.amIOld();
            System.out.println();
        }
        sc.close();
    }
}

Program explanation -
  • In this program, we are using concept of constructor of a class, getters and setters, this keyword and instance variable.
  • Age is an instance variable of class Person because its value changes every instance.
  • amIOld and yearPasses are instance methods because they are changing the value of age.
  • Input is taken once, let's say 4
  • Loop will run depending upon the input, let's say 4 times.
    • First loop
      • Age = -1
      • We are passing age = -1 to the constructor
      • Initial age = -1
      • Age = -1
      • amIOld method is called.
      • age = 0
      • Prints 
        • Age is not valid, setting age to 0.
        • You are young.
      • Now J = 0
      • yearsPasses is called. 
      • age = age + 1 will make age = 1
      • J= 1,2
      • Will change age to 3
      • amIOld is called again. this time it will print
        • You are young.
      • End of i loop.
      • Again new loop will begin with new age value.



Comments

Popular posts from this blog

Azure Tutorials Series - Azure Networking

Coforge Interview Questions | Automation Testing profile

Testing in CI/CD