Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Noah's Boat
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
keyboardwalker




PostPosted: Mon Jun 23, 2014 9:37 pm   Post subject: Noah's Boat

Noah has a boat with of every single species of animal on it that have >=2 animals of each species. It is dinner time on the boat and Noah must then call a unique "eat" method for each of the animals to feed them. All of different types of animals are a subclass of Animal.

So the question is, what kind of data structure would allow for all of these semi-unique objects to preform the "eat" method easily. You could of course make an array for each of the different 'species' (classes) and then run through all of the arrays and objects and call the "eat" method but that would be a sizable amount of code. Is there any way to do this more elegantly?


To put it in a slightly more technical way

There are many different classes and each have a varying number of instances (objects).
The said classes are all sub classes of the same class.
All of the objects have a common method that must be called.
The method is slightly different but has the same method name (polymorphism).

Is there a data structure that allows for this en masse method-calling to be easily done?
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: Mon Jun 23, 2014 9:41 pm   Post subject: RE:Noah\'s Boat

Look up virtual methods and interfaces.
Raknarg




PostPosted: Mon Jun 23, 2014 9:47 pm   Post subject: RE:Noah\'s Boat

If you need to have each type of animal separated, you could use an array list of arraylists. Otherwise, use a single arraylist. Here's an example using a single arrayList:

Java:

class Animal {
        void eat() {
                // eat
        }
}

class Cat extends Animal {
        @Override
        void eat() {
                // eat cat stuff
        }
}

class Dog extends Animal {
        @Override
        void eat() {
                // eat dog stuff
        }
}

class Bear extends Animal {
        @Override
        void eat() {
                // eat bear stuff
        }
}

public class MainProgram() {
        public static void main(String[] args) {
                ArrayList<Animal> animalList = new ArrayList<Animal>();

                animalList.add(new Cat());
                animalList.add(new Dog());
                animalList.add(new Bear());

                for (Animal a : animalList) {
                        a.eat();
                }
        }
}


Personally I would make Animal an abstract class instead and make eat an abstract method, or use an interface.
keyboardwalker




PostPosted: Wed Jun 25, 2014 9:03 am   Post subject: RE:Noah\'s Boat

Thank you very much you two! The response time was impeccable.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: