Thanos T. M. February 2016

Accessing the methods of a member of an Object[] array

I'm pretty new to programming so bare with me. I have an assignment I'm working on in which I have to create an array of objects to then be randomly filled. In this array I need to put 100 random, Person(base) Student(sub), Professor(sub), Course(an array of Student plus a Professor), and a Circle(unrelated). I also have to name and count every Person(including profs and students) I enter into the array.

    Object[] array = new Object[100];
    String[] names = new String[]{"Ben","Anne","Joe","Sue","John","Betty","Robert","Mary",
                                  "Mark","Jane","Paul","Willow","Alex","Courtney","Jack",
                                  "Rachel"};
    int count = 0;
    for(int i=0; i<100; i++){
       int a = (int)(Math.random()*5);
       String n = names[(int)(Math.random()*16)];
       if(a == 0){array[i]= new Person(n); count++;}
       else if(a == 1){array[i]= new Student(n); count++;}
       else if(a == 2){array[i]= new Professor(n); count++;}
       else if(a == 3){
           array[i]= new Course();
           count = count + 11;
           for(int j = 0; j<10; j++){
               String l = names[(int)(Math.random()*16)];
               array[i].getClasslist()[j].setName(l);}
       }
       else if(a == 4){array[i]= new Circle();}
    }

Whenever I try to call a method of one of the members, however, it tells me "Cannot find Symbol- Method getClasslist()" or setName or whatever I'm trying to call. Any idea how to fix this?

Answers


Louis Wasserman February 2016

As far as your actual code goes, you'll need to explicitly cast array[i] the way you've written it:

for(int j = 0; j<10; j++){
   String l = names[(int)(Math.random()*16)];
   ((Course) array[i]).getClasslist()[j].setName(l);
}

...though the whole thing about mixing many different types without a common interface in one array is a huge code smell.


Andreas February 2016

When you have a sequence of mixed objects, you have to check the actual type of object before you can call type-specific methods. To do that, use instanceof:

for (Object obj : array) {
    if (obj instanceof Person) { // includes subclasses Student and Professor
        Person person = (Person)obj;
        // now you can call Person methods
    } else if (obj instanceof Course) {
        Course course = (Course)obj;
        for (Object member : course.getMembers()) {
            if (member instanceof Person) {
                Person person = (Person)member;
                // now you can call Person methods
            }
        }
    }
}


ead February 2016

I think it is totally Okay to put all these different classes in an Object[], as long as in the later part of the program all elements of the array are used only as Objects (Okay, Object is not that a fancy interface - but this only an exercise).

But it would better to initialize the whole Course object and only afterwards to put it into the array:

   else if(a == 3){
       Course course = new Course();
       count = count + 11;
       for(int j = 0; j<10; j++){
           String l = names[(int)(Math.random()*16)];
           course.getClasslist()[j].setName(l);//no casting needed
       }
       array[i]=course;//we can forget the real type of the object now
   }

Post Status

Asked in February 2016
Viewed 2,987 times
Voted 6
Answered 3 times

Search




Leave an answer