1

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?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
  • Mixing different types of objects in one array without any common interface is generally a sign that something in your design is _really deeply problematic._ – Louis Wasserman Feb 08 '16 at 20:35
  • Are you sure that's what your assignment is requiring you to do? – Amit Feb 08 '16 at 20:38

3 Answers3

1

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
   }
ead
  • 32,758
  • 6
  • 90
  • 153
0

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.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
0

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
            }
        }
    }
}
Andreas
  • 154,647
  • 11
  • 152
  • 247