letsdoitjava

family

Jul 17th, 2019
401
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.82 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. }
  63.  
  64. ---------------------------------------------------------------------------------------------------------------------------------------
  65. // Student Class extends Person Class and uses methods to return data
  66. // Renee Waggoner
  67. // July 17, 2019
  68. // Special Requirements: None
  69.  
  70. package family;
  71.  
  72. class Student extends Person {
  73.     private String major;
  74.     private double gpa;
  75.  
  76.     public Student(String name, int age, String major, double gpa) {
  77.             super(name, age);
  78.             this.major = major;
  79.             this.gpa = gpa;
  80.     }
  81.  
  82.     public Student() {
  83.     }
  84.  
  85.     public String getMajor() {
  86.             return major;
  87.     }
  88.  
  89.     public void setMajor(String major) {
  90.             this.major = major;
  91.     }
  92.  
  93.     public double getGpa() {
  94.             return gpa;
  95.     }
  96.  
  97.     public void setGpa(double gpa) {
  98.             this.gpa = gpa;
  99.     }
  100.  
  101.    
  102.     public String toString() {
  103.             return "Student [major: " + major + ", gpa: " + gpa + ", name: " + getName() + ", age: " + getAge() + "]";
  104.     }
  105.  
  106.    
  107.     public boolean equals(Object obj) {
  108.             if (this == obj)
  109.                     return true;
  110.             if (!super.equals(obj))
  111.                     return false;
  112.             if (getClass() != obj.getClass())
  113.                     return false;
  114.             Student other = (Student) obj;
  115.             if (gpa != other.gpa)
  116.                     return false;
  117.             if (major == null) {
  118.                     if (other.major != null)
  119.                             return false;
  120.             } else if (!major.equals(other.major))
  121.                     return false;
  122.             return true;
  123.     }
  124. }
  125. ---------------------------------------------------------------------------------------------------------------------------------------
  126. // The Family Class contains an array of Person that is the size_of_family, checks size and prints
  127. // Renee Waggoner
  128. // July 17, 2019
  129. // Special Requirements: None
  130. package family;
  131.  
  132. public class Family {
  133.  
  134.     Person familyPersons[];
  135.     int numPersons = 0;
  136.  
  137.     Family(int size_of_family) {
  138.             familyPersons = new Person[size_of_family];
  139.     }
  140.    
  141.     void addPerson(Person p) {
  142.             for (int i = 0; i < numPersons; i++) {
  143.                     if (familyPersons[i].equals(p))
  144.                     {
  145.                         // if duplicate is found
  146.                             System.out.println("Duplicate entry: " + p);
  147.                             return;
  148.                     }
  149.             }
  150.             // continue without adding duplicate
  151.             if (numPersons != familyPersons.length)
  152.             {
  153.                     familyPersons[numPersons++] = p;
  154.             } else {
  155.                     System.out.println("Family array full. Skipping: " + p);
  156.             }
  157.     }
  158.  
  159.     void printOutFamily()
  160.     {
  161.         for (int i = 0; i < numPersons; i++)
  162.         {
  163.             System.out.println (familyPersons[i].toString());
  164.         }
  165.     }
  166.  
  167.     public static void main(String[] args) {
  168.        
  169.             Family f = new Family(8);
  170.             Person fred = new Person("Fred Flintstone", 50);
  171.             System.out.println("created " + fred);
  172.             f.addPerson(fred);
  173.             f.addPerson(fred);
  174.  
  175.             Student fredStudent = new Student("Fred Flintstone", 50, "Math", 3.1);
  176.            
  177.             System.out.println("created " + fredStudent);
  178.             f.addPerson(fredStudent);
  179.  
  180.             Person wilma = new Person("Wilma Flintstone", 48);
  181.             f.addPerson(wilma);
  182.  
  183.             Student george = new Student("George", 21, "Politics", 3.1);
  184.             System.out.println("created " + george);
  185.             f.addPerson(george);
  186.  
  187.             george.setName("Georgie");
  188.             f.addPerson(new Student("George", 21, "Politics", 3.1));
  189.  
  190.             f.addPerson(new Student("John", 18, "Geology", 2.9));
  191.             f.addPerson(new Student("Jane", 21, "Music", 3.2));
  192.             f.addPerson(new Student("Tarzan", 22, "Gymnastics", 4.0));
  193.             f.addPerson(new Student("Jim", 21, "Physics", 2.5));
  194.             f.addPerson(new Person("Robert", 18));
  195.             f.addPerson(new Person("Clemente", 32));
  196.  
  197.             System.out.println("****** family listing: ");
  198.             f.printOutFamily();
  199.     }
  200. }
Advertisement
Add Comment
Please, Sign In to add comment