0

I am trying to figure out Inheritance and Arrays in Java and I am trying to get these classes to work together. I believe I have the Inheritance down, but I am still struggling with the array part.

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 am getting an error that says: "main" java.lang.ArrayIndexOutOfBoundsException: 8

I can not figure out why this program is not working properly and would appreciate any help to figure this out. Thank you.

Person.java Class

    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 Class

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;
}

}

Family.java Class

public class Family
{
private int famArray = 0;
private Person[] family;

public Family(int size_of_family)
{
    famArray = size_of_family;
    family = new Person[famArray];
}   
public void addPerson(Person p)
{
    boolean isPresent = false;

    int i;
    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");
        }                       
    }      
    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)
{
    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));


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

}
}
5
  • 1
    Which line causes the error? Commented Jul 16, 2014 at 0:32
  • The difference between Family and Student is that Student has an "isa" relationship with Person, while Family had a "hasa" relationship with Person. That is, a Student is a person, while a Family simply contains one or more Persons. Commented Jul 16, 2014 at 0:33
  • As to the ArrayOutOfBounds, that's simply because you tried to access element 8 of an array with fewer than 9 elements. (Remember that the first element of an array is element zero.) The exception gives you the line number, so figuring it out should be quite simple. Commented Jul 16, 2014 at 0:34
  • ArrayIndexOutOfBoundsException is nothing to do with inheritance, and it would happen even without inheritance. Commented Jul 16, 2014 at 1:07
  • That is why I said I am having trouble with the array part if you read the post. Commented Jul 16, 2014 at 1:08

2 Answers 2

2

Here's the problem, in Family#addPerson method:

if(isPresent == false)
    family[i] = p;

You're adding the element in position i. If the element is not found, then i value will be family.length, thus giving you the exception.

Use int famArray field instead:

if(isPresent == false) {
    family[famArray++] = p;
}

Or in an easier way for starters:

if(isPresent == false) {
    family[famArray] = p;
    famArray = famArray + 1;
}

As an addition to your current problem, you should first check if the famArray element is equals to family.length. If they're the same, then increase the array or do not allow more elements.


You state you have the same problem. This is because you're initializing famArray with the length of the array, noted in Family class constructor:

public Family(int size_of_family) {
    famArray = size_of_family; //here
    family = new Person[famArray];
}

Change the code to:

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

And you're done.

Sign up to request clarification or add additional context in comments.

3 Comments

Aw, don't confuse the guy with ++ just yet!
@user3818410 that's because you initialize famArray with the maximum value. Have it at 0 in constructor.
I am not seeing where I did that. Which line are you talking about?
2

The problem is the line

family[i] = p;

This occurs after a for loop which increments i to be equal to the length of the array. I don't have any suggestions to fix it because I'm not sure what you are trying to do here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.