Class Person

    • Field Summary

      Fields 
      Modifier and Type Field Description
      private int age
      The age of the person
      private String name
      The name of the person
    • Constructor Summary

      Constructors 
      Constructor Description
      Person()
      Default constructor.
      Person​(String name)
      Constructor initializing the name of the person.
      Person​(String name, int age)
      Constructor initializing the name and age of the person.
    • Field Detail

      • name

        private final String name
        The name of the person
      • age

        private int age
        The age of the person
    • Constructor Detail

      • Person

        public Person()
        Default constructor.
      • Person

        public Person​(String name)
        Constructor initializing the name of the person.
        Parameters:
        name - the name of the person
      • Person

        public Person​(String name,
                      int age)
        Constructor initializing the name and age of the person.
        Parameters:
        name - the name of the person
        age - the age of the person
    • Method Detail

      • getName

        public String getName()
        Returns the name of this person.
        Returns:
        the name
      • getAge

        public int getAge()
        Returns the age of this person.
        Returns:
        the age
      • setAge

        public void setAge​(int age)
        Sets the age of this person.
        Parameters:
        age - the age
      • toString

        public String toString()
        Returns a string representation of this person.
        Overrides:
        toString in class Object
        Returns:
        the string representation
      • hashCode

        public int hashCode()
        Returns a hash code computed from the name of the person.
        Overrides:
        hashCode in class Object
        Returns:
        the hash code
      • equals

        public boolean equals​(Object obj)
        Compares this person object and the given object for equality. Compares the name.
        Overrides:
        equals in class Object
        Returns:
        true if given object is a person and their names are equal
      • compareTo

        public int compareTo​(Person o)
        Compares this person and the given person first based on the name, then on the age.

        Remark: used for demonstration of Comparator

        Specified by:
        compareTo in interface Comparable<Person>
        Returns:
        -1 if this person is smaller, +1 if other person is smaller, 0 if they are equal