Example #1
0
        public static void Main()
        {
            Cat    cat    = new Cat("Macana", 2, Gender.female);
            Kitten kitten = new Kitten("", 1);
            Tomcat tomcat = new Tomcat("Tom", 4);
            Dog    dog    = new Dog("Rex", 6, Gender.male);
            Frog   frog   = new Frog("Kikirana", 2, Gender.female);

            var animals = new Animal[]
            {
                cat,
                kitten,
                tomcat,
                dog,
                frog
            };

            foreach (var animal in animals)
            {
                Console.Write("{0}", animal);
                animal.ProduceSound();
            }

            var groupedAnimalsByType = animals.GroupBy(GetAnimalType,
                                                       (t, a) => new { type = t, averageAge = a.Average(animal => animal.Age) });

            foreach (var group in groupedAnimalsByType)
            {
                Console.WriteLine("The average age of {0}s is {1:f2}.", group.type, group.averageAge);
            }
        }
        static void Main()
        {
            Frog[] frogs = new Frog[18];
            for (int i = 0; i < frogs.Length; i++)
            {
                frogs[i] = new Frog("Frogname " + (i + 1), i * 3 + 1, (i % 2 == 0) ? true : false); // male:female distribution 50/50
                Console.WriteLine(frogs[i] + " and says " + frogs[i].Say());
            }

            Dog[] dogs = new Dog[15];
            for (int i = 0; i < dogs.Length; i++)
            {
                dogs[i] = new Dog("Dogname " + (i + 1), i * 5 + 1, (i % 3 == 0) ? true : false); // male:female distribution 30/70
                Console.WriteLine(dogs[i] + " and says " + dogs[i].Say());
            }

            Kitten[] kittens = new Kitten[13];
            for (int i = 0; i < kittens.Length; i++)
            {
                kittens[i] = new Kitten("Kittenname " + (i + 1), i * 2 + 1); // all are female
                Console.WriteLine(kittens[i] + " and says " + kittens[i].Say());
            }
            Tomcat[] tomcats = new Tomcat[14];
            for (int i = 0; i < tomcats.Length; i++)
            {
                tomcats[i] = new Tomcat("Tomcatname " + (i + 1), i * 4 + 1); // all are male
                Console.WriteLine(tomcats[i] + " and says " + tomcats[i].Say());
            }

            // average age calculation for each kind of animal
            Console.WriteLine("Average frogs age: " + Animal.AverageAge(frogs));
            Console.WriteLine("Average dogs age: " + Animal.AverageAge(dogs));
            Console.WriteLine("Average kittens age: " + Animal.AverageAge(kittens));
            Console.WriteLine("Average tomcats age: " + Animal.AverageAge(tomcats));

            Console.WriteLine("\r\n\r\nPress Enter to finish");
            Console.ReadLine();
        }