letsdoitjava

Person

Jul 17th, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.59 KB | None | 0 0
  1. // The Person Class uses get methods and returns appropriate data
  2. // Renee Waggoner
  3. // July 17, 2019
  4. // Special Requirements: None
  5. package family;
  6.  
  7. public class Person {
  8.     private String name;
  9.     private int age;
  10.     // default constructor, no parameters
  11.     public Person() {
  12.     }
  13.     // constructor with 2 parameters
  14.     public Person(String name, int age)
  15.     {
  16.             this.name = name;
  17.             this.age = age;
  18.     }
  19.     // using toString allows us to return values without writing as much code
  20.     public String toString()
  21.     {
  22.             return "Person [name: " + name + ", age: " + age + "]";
  23.     }
  24.  
  25.     public boolean equals(Object obj)
  26.     {
  27.             // creating path for display data
  28.             if (this == obj)
  29.                     return true;
  30.             if (obj == null)
  31.                     return false;
  32.             if (getClass() != obj.getClass())
  33.                     return false;
  34.             Person other = (Person) obj;
  35.             if (age != other.age)
  36.                     return false;
  37.             if (name == null) {
  38.                     if (other.name != null)
  39.                             return false;
  40.             } else if (!name.equals(other.name))
  41.                     return false;
  42.             return true;
  43.     }
  44.  
  45.     // call getName method to return name
  46.     public String getName() {
  47.             return name;
  48.     }
  49.  
  50.     public void setName(String name) {
  51.             this.name = name;
  52.     }
  53.  
  54.     public int getAge() {
  55.             return age;
  56.     }
  57.  
  58.     public void setAge(int age) {
  59.             this.age = age;
  60.     }
  61.  
  62. }
Advertisement
Add Comment
Please, Sign In to add comment