Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java practice homework problem. Create a class called Person . Person should hav

ID: 3773875 • Letter: J

Question

 Java practice homework problem.  Create a class called Person.  Person should have (instance variables should be private):   private String name; private int age;  2 Constructors: Default constructor (no parameters) Constructor which takes 2 parameters for name and age  Methods: toString equals  Appropriate set and get methods for setting and accessing the Person data.  ----------------------------------------------------------  Create a class called Student which extends Person. Student has the private data:  private String major; private double gpa;  Similar to the Person class, add get and set methods.   Add 2 constructors:  default constructor Constructor which sets all necessary data in the Person and  Student class (name, age, major and gpa)  Methods: toString equals ------------------------------------------------------------  Create a Family class with the following:  Constructor:  Family (int size_of_family)  Family contains an array of Person that is the size of "size_of_family".  Family has a method called: void addPerson(Person p); The addPerson will call equals to make sure that the Person p hasn't already been added.  If a duplicate is found, print out  a message and continue without adding the duplicate person.    void printOutFamily();   This routine calls all of the toString methods in the family. --------------------------------------------------------------  Run your Family with the following main routine: This output should be put into the JH6_worksheet.txt       public static void main(String[] args)     {         Family f = new Family(8);         Person fred= new Person("Fred Flintstone", 50);         System.out.println("created " + fred);         f.addPerson(fred);         f.addPerson(fred);          Student fredStudent = new Student("Fred Flintstone", 50, "Math", 3.1);         System.out.println("created "+ fredStudent);         f.addPerson(fredStudent);          Person wilma = new Person("Wilma Flintstone", 48);         f.addPerson(wilma);           Student george= new Student("George", 21, "Politics", 3.1);         System.out.println("created " + george);         f.addPerson(george);          george.setName("Georgie");         f.addPerson(new Student("George", 21, "Politics", 3.1));                    f.addPerson(new Student("John", 18, "Geology", 2.9));         f.addPerson(new Student("Jane", 21, "Music", 3.2));         f.addPerson(new Student("Tarzan", 22, "Gymnastics", 4.0));         f.addPerson(new Student("Jim", 21, "Physics", 2.5));         f.addPerson(new Person("Robert", 18));         f.addPerson(new Person("Clemente", 32));           System.out.println("****** family listing: ");         f.printOutFamily();      } 

Explanation / Answer


class Person {
   public Person(String name, int age) {
       super();
       this.name = name;
       this.age = age;
   }

   private String name;
   private int age;

   public Person() {
       // TODO Auto-generated constructor stub
   }

   public String getName() {
       return name;
   }

   public void setName(String name) {
       this.name = name;
   }

   public int getAge() {
       return age;
   }

   public void setAge(int age) {
       this.age = age;
   }

   @Override
   public String toString() {
       return "Person [name=" + name + ", age=" + age + "]";
   }

   @Override
   public boolean equals(Object o) {
       Person p = null;
       if (o instanceof Person) {
           p = (Person) o;
       }
       if (this instanceof Student) {
           p = (Student) o;

       }
       if ((p.getAge() == this.getAge()) && (p.getName().equals(this.getName())))
           return true;
       else
           return false;

   }
}

class Student extends Person {
   public Student() {
       // TODO Auto-generated constructor stub
   }

   public Student(String name, int age, String major, double gpa) {
       super(name, age);
       this.major = major;
       this.gpa = gpa;
   }

   private String major;
   private double gpa;

   public String getMajor() {
       return major;
   }

   public void setMajor(String major) {
       this.major = major;
   }

   public double getGpa() {
       return gpa;
   }

   public void setGpa(double gpa) {
       this.gpa = gpa;
   }

   @Override
   public String toString() {
       return "Student [major=" + major + ", gpa=" + gpa + "]";
   }

   @Override
   public boolean equals(Object o) {

       if (o instanceof Student) {

           Student s = (Student) o;

           if (s.getAge() == this.getAge() && s.getName().equals(this.getName()))
               return true;
           else
               return false;

       } else if (o instanceof Person) {
           Person s = (Person) o;

           this.equals(s);

       }
       return false;
   }
}


class Family {

   public Family(int size_of_family) {
       super();
       this.size_of_family = size_of_family;
       p = new Person[this.size_of_family];
   }

   private int size_of_family;
   Person p[];
   int counter = -1;

   public int getSize_of_family() {
       return size_of_family;

   }

   public void setSize_of_family(int size_of_family) {
       this.size_of_family = size_of_family;
   }

   public void addPerson(Person person) {
       boolean flag = false;
       for (int i = 0; i < counter; i++) {

           if (this.p[counter].equals(person)) {

               flag = true;
               System.out.println("person already exists didn't added into database");
               break;
           }
       }
       if (flag == false) {
           this.p[++counter] = new Person();
           this.p[counter].setAge(person.getAge());
           this.p[counter].setName(person.getName());

       }

   }

   public void printOutFamily() {
       this.toString();
   }

   @Override
   public String toString() {
       System.out.println("name age");

       for (int i = 0; i < counter; i++) {
           System.out.println(p[i].getName() + " " + p[i].getAge());
       }
       return null;
   }
}

class Snippet {
   public static void main(String[] args) {
       Family f = new Family(8);
       Person fred = new Person("Fred Flintstone", 50);
       System.out.println("created " + fred);
       f.addPerson(fred);
       f.addPerson(fred);

       Student fredStudent = new Student("Fred Flintstone", 50, "Math", 3.1);
       System.out.println("created " + fredStudent);
       f.addPerson(fredStudent);

       Person wilma = new Person("Wilma Flintstone", 48);
       f.addPerson(wilma);

       Student george = new Student("George", 21, "Politics", 3.1);
       System.out.println("created " + george);
       f.addPerson(george);

       george.setName("Georgie");
       f.addPerson(new Student("George", 21, "Politics", 3.1));

       f.addPerson(new Student("John", 18, "Geology", 2.9));
       f.addPerson(new Student("Jane", 21, "Music", 3.2));
       f.addPerson(new Student("Tarzan", 22, "Gymnastics", 4.0));
       f.addPerson(new Student("Jim", 21, "Physics", 2.5));
       f.addPerson(new Person("Robert", 18));
       f.addPerson(new Person("Clemente", 32));

       System.out.println("****** family listing: ");
       f.printOutFamily();

   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote