Package at.jku.ssw.fp.person
Class Person
- java.lang.Object
-
- at.jku.ssw.fp.person.Person
-
- All Implemented Interfaces:
Comparable<Person>
public class Person extends Object implements Comparable<Person>
Class representing a person with name and age.- Author:
- Herbert Praehofer
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int
compareTo(Person o)
Compares this person and the given person first based on the name, then on the age.boolean
equals(Object obj)
Compares this person object and the given object for equality.int
getAge()
Returns the age of this person.String
getName()
Returns the name of this person.int
hashCode()
Returns a hash code computed from the name of the person.void
setAge(int age)
Sets the age of this person.String
toString()
Returns a string representation of this 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 personage
- 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.
-
hashCode
public int hashCode()
Returns a hash code computed from the name of the person.
-
equals
public boolean equals(Object obj)
Compares this person object and the given object for equality. Compares the name.
-
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 interfaceComparable<Person>
- Returns:
- -1 if this person is smaller, +1 if other person is smaller, 0 if they are equal
-
-