0

I've been working on a basic class inheritance exercise, and even though I think I've got the jist of it, my program isn't working the way it should. I'm getting compile errors and haven't been able to figure out why- it'd be great if you all could help me out here.

So to start off, there are three files 1. Person.java -base class 2. Student.java -a derived class of Person.java 3. Family.java -not quite sure, I think it's its own base class

Person.java has two instance variables, String name and int age, and an assortment of constructors, toString, equals, and set/get methods

Student.java, again, a derived class of Person, by definition will have all the stuff contained within Person, as well as two more instance vars, String major, and double gpa. This class also have get/set methods for major and gpa, an equals method that compares one class student with another class student, and I believe it's called an overidden method of toString that returns name, age, major, and gpa all in one string.

Lastly, Family.java is where the main method resides. It creates an array of type Person, adds "Persons" to this array, then outputs them.

I'm fairly certain my Person.java and my Student.java is fine (I will include them at the very bottom just in case), but my problem is with Family.java. There is something wrong with how I'm adding more people to the array (addPerson method), but I just can't figure out what.


Family.java

public class Family
{
private int famSize;
private Person[] family;

public Family(int size_of_family)
{
    famSize = size_of_family;
}   
public void addPerson(Person p)
{
    boolean isPresent = false;

    int i;
    for(i = 0; i < family.length; i++)
    {

        if(family[i].equals(p) == true)
        {
            isPresent = true;
            System.out.println(p.getName() + 
            " is already present in the family");
        }                       
    }       
    if(isPresent == false)
        family[i] = p;
}
public void printOutFamily()
{
    for(int i = 0; i < family.length; i++)
    {
        System.out.println(family[i].toString());
    }
}   
public static void main(String[] args)
{
   Person fred= new Person("Fred Flintstone", 50);
   System.out.println("created " + fred);

   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(fred);
   f.addPerson(wilma);
   f.addPerson(george);
   f.addPerson(sue);
   f.addPerson(anotherGeorge);
   f.addPerson(yetAnotherGeorge);

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

   f.printOutFamily();
}
}

Person.java

public class Person 
{
private String name;
private int age;

public Person()
{
    name = "John Smith";
    age = 1;
}

public Person(String n, int a)
{
    name = n;
    age = a;
}

public String toString()
{
    return ("Name: " + getName() + ", Age: " + age + " ");
}

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

public String getName() 
{
    return name;
}

public void setName(String newName)
{
    name = newName;
}

public int getAge() 
{
    return age;
}

public void setAge(int newAge)
{
    age = newAge;
}
}

Student.java

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

public Student()
{
    super();
    major = "Undecided";
    gpa = 0.0;
}

public Student(String theName, int theAge, String theMajor, double theGpa)
{
    super(theName, theAge);
    setMajor(theMajor);
    setGpa(theGpa);
}

public String toString()
{
    return ("Name: " + getName() + ", Age: " + getAge() + ", Major: " + major + ", GPA: " + gpa);
}

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

public String getMajor() 
{
    return major;
}

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

public double getGpa() 
{
    return gpa;
}

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

}
Jeffrey
  • 44,417
  • 8
  • 90
  • 141
HelloMyNameIsRay
  • 73
  • 2
  • 3
  • 8

2 Answers2

3

You should add

family = new Person[famSize];

in your Family constructor to initialize your array. It becomes:

public Family(int size_of_family)
{
    famSize = size_of_family;
    family = new Person[famSize];
}

As @Pshemo and @MarkM noted, you also need to add a check in your if statement to make sure family[i] isn't null:

for(i = 0; i < family.length; i++)
{

    if(family[i] != null && family[i].equals(p))
    {
        isPresent = true;
        System.out.println(p.getName() + 
        " is already present in the family");
    }                       
} 
go-oleg
  • 19,272
  • 3
  • 43
  • 44
  • Thanks, you're right, my constructor was incomplete. Problem is, I still hit an error at the first if statement "if(family[i].equals(p) == true" within the addPerson method. Do you me asking if you know why? does the equals method not accept classes? – HelloMyNameIsRay Jul 22 '13 at 01:42
  • after initializing, the array will have null values, you need to add Person objects to the array before comparing. also, change the compare statement to this: if(true == p.equals(family[i])) – Jayz Jul 22 '13 at 01:46
  • @HelloMyNameIsRay when you create new array of objects it is filled with `null`s and you can't invoke any non-static methods on `null`s. To solve this issue you can test if you you are trying to invoke your `equals` method on null with `if (family[i] != null && family[i].equals(p) == true)`, but better way would be adding counter in your `Family` class, iterate with `for (i = 0; i < counter; i++)` and when you are adding new element do it with `family[counter++] = p;` instead `family[i] = p`. Oh, you should also test before adding if counter wouldn't be grater than size of array. – Pshemo Jul 22 '13 at 01:50
  • You can also add a null check to `family[i]` before the `if` statement. – Mark M Jul 22 '13 at 01:51
0

The problem is at this line:

for(i = 0; i < family.length; i++)

at this point, family has not yet been initialized and it will be null. You have 3 options to initialize family before calling addPerson method:

1) initialize family at the time of creation

private Person[] family = new Person[5];

2) initialize family in the constructor

private Person[] family = new Person[famSize];

3) create an instance of Family and initialize family attribute before calling addPerson (this is error-prone and should be avoided)

Jayz
  • 1,174
  • 2
  • 19
  • 43