001    /**
002     * A class that exercises the Rodent inheritance hierarchy. Part of exercise 7.6
003     * and 7.7.
004     * 
005     * @author Kristian Torp, torp (at) cs (dot) aau (dot) dk
006     * @version 1.0
007     */
008    public class UseRodent {
009    
010            public static void main(String[] args) {
011                    // How many rodents do we use?
012                    int size = 10;
013                    // Array of rodents
014                    Rodent[] rodents = new Rodent[size];
015    
016                    for (int i = 0; i < size; i++) {
017                            // make a random integer in the interval 0-2
018                            int rand = (int) (Math.random() * 3);
019    
020                            // depending on the random number create a new object. Note we
021                            // do not make any Rodent objects since the Rodent class is
022                            // abstract
023                            switch (rand) {
024                            case 0:
025                                    rodents[i] = new Mouse();
026                                    break;
027                            case 1:
028                                    rodents[i] = new Gerbil();
029                                    break;
030                            case 2:
031                                    rodents[i] = new Hamster();
032                                    break;
033                            default:
034                                    // Just in case something goes wrong tell this
035                                    System.out.println("This cannot happen value is " + rand);
036                            }
037                    }
038                    // now print what is in the array
039                    for (int j = 0; j < size; j++) {
040                            // call the method on the rodent
041                            rodents[j].myself();
042                    }
043            }
044    }