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

I don\'t want a new source code, just troubleshooting. When I run the program, i

ID: 3539663 • Letter: I

Question

 I don't want a new source code, just troubleshooting. When I run the program, it returns:
created Tom Flintstone 50
created George Flintstone 21 Politics 3.1

In the Family class, addPerson part, if I took out the else from for loop and add it at the end, it returns:
created Tom Flintstone 50
created George Flintstone 21 Politics 3.1
Already Added!
Already Added!
Already Added!
Already Added!
Already Added!
Already Added!
Tom Flintstone 50
Wilma Flintstone 48
George Flintstone 21 Politics 3.1
Sue Flintstone 24 Nursing 3.3
George Flintstone 21 Math 3.4
George Flintstone 21
George Flintstone 21 Math 3.4



Create a class called Person. Person should have (instance variables should be private): String name; 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: String major; 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: public static void main(String[] args) { Person tom= new Person("Tom Flintstone", 50); System.out.println("created " + tom); Person wilma = new Person("Wilma Flintstone", 48); Student george= new Student("George Flintstone", 21, "Politics", 3.1); System.out.println("created " + george); Student sue= new Student("Sue Flintstone", 24, "Nursing", 3.3); Student anotherGeorge= new Student("George Flintstone", 21, "Math", 3.4); Person yetAnotherGeorge= new Person("George Flintstone", 21); Family f = new Family(10); f.addPerson(tom); f.addPerson(wilma); f.addPerson(george); f.addPerson(sue); f.addPerson(anotherGeorge); f.addPerson(yetAnotherGeorge); anotherGeorge.setName("Georgie Flintstone"); f.addPerson(anotherGeorge); f.printOutFamily(); }

I have so far:

public class Person {

private String name;
private int age;

public Person(){
name = "noname";
age = 0;
}

public Person(String thename, int theage){
name = thename;
age = theage;
}

public Person(Person orginalO){
name = orginalO.name;
age = orginalO.age;
}

public String getName(){
return name;
}

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

public int getAge(){
return age;
}

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

public String toString(){
return (name + " " + age);
}

public boolean equals(Person otherPerson){
return (name.equals(otherPerson.name)&&(age == (otherPerson.age)));
}
}



public class Student extends Person {
private String major;
private double gpa;

public Student(){
super();
major = "nomajor";
gpa = 0;
}

public Student(String thename, int theage, String themajor, double thegpa){
super(thename, theage);
if ((themajor != "nomajor")&&(thegpa != 0)){
major = themajor;
gpa = thegpa;
}
}

public Student(Student orginalO){
super(orginalO);
major = orginalO.major;
gpa = orginalO.gpa;
}

public String getMajor(){
return major;
}

public double getGpa(){
return gpa;
}

public void setMajor(String takeMajor){
major = takeMajor;
}

public void setGpa(double recGpa){
gpa = recGpa;
}

public String toString(){
return (getName() + " " + getAge() + " " + major + " " + gpa);
}

public boolean equals(Student otherStudent){
return (getName().equals(otherStudent.getName())&&(getAge() == otherStudent.getAge())&&(major.equals(otherStudent.major))&&
gpa == otherStudent.gpa);
}


}



public class Family extends Person{
public int size_of_family;
public Person[] Fam;

public Family(){
super();
size_of_family = 0;
Fam = new Person[20];
}

public Family(int ager){
super();
size_of_family = 0;
Fam = new Person[ager];

}


void addPerson(Person p){
for (int i = 0; i<size_of_family; i++){
if (Fam[i].equals(p)) {
System.out.println("Already Added!");
}
else {
Fam[size_of_family] = p;
size_of_family++;
}
}
}

void printOutFamily(){
for (int i = 0; i < size_of_family; i++){
System.out.println(Fam[i]);
}
}


public static void main(String[] args)
{
Person tom= new Person("Tom Flintstone", 50);
System.out.println("created " + tom);

Person wilma = new Person("Wilma Flintstone", 48);
Student george= new Student("George Flintstone", 21, "Politics", 3.1);
System.out.println("created " + george);

Student sue= new Student("Sue Flintstone", 24, "Nursing", 3.3);
Student anotherGeorge= new Student("George Flintstone", 21, "Math", 3.4);
Person yetAnotherGeorge= new Person("George Flintstone", 21);

Family f = new Family(10);
f.addPerson(tom);
f.addPerson(wilma);
f.addPerson(george);
f.addPerson(sue);
f.addPerson(anotherGeorge);
f.addPerson(yetAnotherGeorge);

anotherGeorge.setName("Georgie Flintstone");
f.addPerson(anotherGeorge);

f.printOutFamily();

}

}


Explanation / Answer

please rate - thanks


the reason all the georges aren't being added is family is made up of persons, so only names are compared. the .equals in student is never looked at

Family isn't a subclass of person

and the sets are this.x=x not x=this.x


public class main
{
public static void main(String[] args)
      {
              Person tom= new Person("Tom Flintstone", 50);
               System.out.println("created " + tom);

                   Person wilma = new Person("Wilma Flintstone", 48);
                  Student george= new Student("George Flintstone", 21, "Politics", 3.1);
              System.out.println("created " + george);

                Student sue= new Student("Sue Flintstone", 24, "Nursing", 3.3);
             Student anotherGeorge= new Student("George Flintstone", 21, "Math", 3.4);
                   Person yetAnotherGeorge= new Person("George Flintstone", 21);

                   Family f = new Family(10);
                  f.addPerson(tom);
                   f.addPerson(wilma);
                 f.addPerson(george);
                f.addPerson(sue);
                   f.addPerson(anotherGeorge);
                 f.addPerson(yetAnotherGeorge);

                  anotherGeorge.setName("Georgie Flintstone");
                f.addPerson(anotherGeorge);

             f.printOutFamily();

}

}

------------------------------------------

public class Person {

private String name;
        private int age;
   
    public Person(){
            name = "noname";
            age = 0;
    }
  
    public Person(String thename, int theage){
          name = thename;
             age = theage;
       }
  
    public Person(Person orginalO){
             name = orginalO.name;
               age = orginalO.age;
}
  
    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;
     }
  
    public String toString(){
           return (name + " " + age);
}
  
    public boolean equals(Person otherPerson){
          return (name.equals(otherPerson.name)&&(age == (otherPerson.age)));
}
}

--------------------------------------------------------


public class Student extends Person {
private String major;
       private double gpa;

    public Student(){
           super();
            major = "nomajor";
          gpa = 0;
    }
  
    public Student(String thename, int theage, String themajor, double thegpa){
         super(thename, theage);
             if ((themajor != "nomajor")&&(thegpa != 0)){
                        major = themajor;
                   gpa = thegpa;
               }
   }
  
    public Student(Student orginalO){
           super(orginalO);
            major = orginalO.major;
             gpa = orginalO.gpa;
}
  
    public String getMajor(){
           return major;
       }
  
    public double getGpa(){
             return gpa;
}
  
    public void setMajor(String takeMajor){
             major = takeMajor;
}
  
    public void setGpa(double recGpa){
          gpa = recGpa;
       }

    public String toString(){
           return (getName() + " " + getAge() + " " + major + " " + gpa);
      }
  
    public boolean equals(Student otherStudent){System.out.println("student");
                return (super.equals(otherStudent)&&(major.equals(otherStudent.major))&&
                         gpa == otherStudent.gpa);
   }


}


-----------------------------------

public class Family //extends Person
{
   public int size_of_family;
public Person[] Fam;
       
    public Family(){
            //super();
            size_of_family = 0;
         Fam = new Person[20];
       }
  
    public Family(int ager){
           // super();
            size_of_family = 0;
         Fam = new Person[ager];
            
    }
  
   
    void addPerson(Person p){
           for (int i = 0; i<size_of_family; i++)
                  if (Fam[i].equals(p)) {
                             System.out.println("Already Added!");
                                    return;
                       }
           
             Fam[size_of_family] = p;
             size_of_family++;
   }
  
    void printOutFamily(){
              for (int i = 0; i < size_of_family; i++){
                        System.out.println(Fam[i]);
         }
   }
}