Exemple #1
0
  public static void main(String[] args) {
    Animal myPet = new Animal("tommy"); // object is created to animal class
    // and value is passed to Animal
    // constructor
    Animal myPet2 = new Animal("tiger ");
    Animal myPet3 = new Animal("Browny", 5);
    System.out.println("My pet name is " + myPet.name);
    System.out.println("My second pet name is " + myPet2.name);
    System.out.println(
        "my third pet is "
            + myPet3.name
            + " "
            + "and its weight is "
            + myPet3.weight
            + " "
            + "pounds");
    Lion wildAnimal = new Lion(); // object is created to lion class and
    // lion constructor is called
    wildAnimal.eat(); // methods are called with reference
    wildAnimal.sleep();
    wildAnimal.animalType();
    System.out.println("my pets are not " + animalType + " animals");

    Lion[] Lions = new Lion[1000]; // created 1000 lions
    String name = "Tim";
    int i = 1;

    for (i = 1; i <= 10; i++) {
      System.out.println("lions name " + name);
    }

    LinkedList LionLinkedList = new LinkedList<Lion>(); // initialized lions
    // linked list
  }
 public Animal getElement(int index) {
   Animal ani = animals.get(index);
   switch (ani.getSort()) {
     case DEER:
       Deer deer = (Deer) ani;
       return (Deer) deer.clone();
     case DOG:
       Dog dog = (Dog) ani;
       return (Dog) dog.clone();
     case GIFRAFFE:
       Gifraffe gifraffe = (Gifraffe) ani;
       return (Gifraffe) gifraffe.clone();
     case HORSE:
       Horse horse = (Horse) ani;
       return (Horse) horse.clone();
     case LION:
       Lion lion = (Lion) ani;
       return (Lion) lion.clone();
     case WOLF:
       Wolf wolf = (Wolf) ani;
       return (Wolf) wolf.clone();
     case CHEETAH:
       Cheetah cheetah = (Cheetah) ani;
       return (Cheetah) cheetah.clone();
     default:
       // Don't know that type of creature.
       return (Animal) ani.clone();
   }
 }