Exemplo n.º 1
0
        static void Main(string[] args)
        {
            // create a few dogs
            Dog[] dogs = new Dog[]
            {
                new Dog("Gosho", 4, Sex.Male),
                new Dog("Rex", 5, Sex.Male),
                new Dog("Linda", 3, Sex.Female),
                new Dog("Ivan", 6, Sex.Male),
            };

            // create a few frogs
            Frog[] frogs = new Frog[]
            {
                new Frog("Misho", 3, Sex.Male),
                new Frog("Tisho", 6, Sex.Male),
                new Frog("Pesho", 4, Sex.Male),
            };

            // create a few cats
            Cat[] cats = new Cat[]
            {
                new Cat("Pisa", 7, Sex.Female),
                new Kitten("Lubov", 3),
                new Tomcat("Tom", 10),
                new Cat("Vesko", 7, Sex.Male),
            };

            // calculate the average age of all dogs
            Console.WriteLine("Dogs' average age is {0}", Animal.CalculateAverageAge(dogs));

            // calculate the average age of all cats
            Console.WriteLine("Cats' average age is {0}", Animal.CalculateAverageAge(cats));

            // calculate the average age of all frogs
            Console.WriteLine("Frogs' average age is {0}", Animal.CalculateAverageAge(frogs));
        }
Exemplo n.º 2
0
        static void Main()
        {
            Animal jaba = new Frog("baba jaba", 1, Genders.Female);
            Animal kekerica = new Frog("kekerica", 3, Genders.Female);

            Animal sharo = new Dog("sharo", 3, Genders.Male);
            Animal sara = new Dog("sara", 2, Genders.Female);
            Animal oldy = new Dog("oldy", 12, Genders.Male);

            Animal puhi = new Kitten("puhi", 2);
            Animal tommy = new Tomcat("tommy", 4);
            Animal mouseKiller = new Cat("mousy", 5, Genders.Male);

            List<Animal> animals = new List<Animal>()
            {
                jaba,
                kekerica,
                sharo,
                sara,
                puhi,
                tommy,
                mouseKiller,
                oldy
            };
            var groupedAnimals = from animal in animals
                                 group animal by (animal is Cat) ? typeof(Cat) : animal.GetType() into g
                                 select new { GroupName = g.Key, AverageAge = g.ToList().Average(an => an.Age) };

            foreach (var animal in groupedAnimals)
            {
                Console.WriteLine("{0} - average age: {1:N2}", animal.GroupName.Name, animal.AverageAge);
            }

            puhi.ProduceSound();
            oldy.ProduceSound();
            jaba.ProduceSound();
        }
Exemplo n.º 3
0
        static void Main()
        {
            //Create list of Animals
            //Tomcat and Kiten have constructor with only two parameters, third is constant
            Animal[] animals = new Animal[] {
            new Dog("Sharo",4,Sex.Male),
            new Cat("Kitty",3,Sex.Female),
            new Frog("Kyrmit",2,Sex.Male),
            new Kitten("Swetty",5),
            new Tomcat("Garfild",7),
            new Dog("Rex",3,Sex.Male),
            new Cat("Lussi", 6,Sex.Female),
            new Tomcat("SomeName",4)};

            Animal.AveargeAge(animals); //Print average age for every kind of animal in list
            
            animals[0].Sound("bayyyyyyyyyy");

            Cat myCat = new Cat("Garfild", 3, Sex.Female);
            myCat.Sound();

            Frog myFrog = new Frog("Kyrmit",3, Sex.Male);
            myFrog.Sound();
        }