Exemple #1
0
        static void Main()
        {
            var kitten = new Kitten("Joanna", 3);
            Animal dog1 = new Dog("Kuche",6, "male");
            Animal frog1 = new Frog("Froggie", 2, "male");
            Animal kitten2 = new Kitten("Hanna", 7);
            Animal tomcat1 = new TomCat("Tommy", 12);
            Animal dog2 = new Dog("Rex",14, "Male");
            Animal frog2 = new Frog("Freya", 3, "Female");
            Animal tomcat2 = new TomCat("Johny",9);
            kitten.ProduceSound();
            dog1.ProduceSound();
            frog1.ProduceSound();
            tomcat1.ProduceSound();
            Console.WriteLine();
            //Console.WriteLine(kitten.Gender);
            //kitten.ProduceSound();
            List<Animal> animalsList = new List<Animal>() 
            {
                dog1,
                frog1,
                kitten2,
                tomcat1,
                dog2, 
                frog2,
                tomcat2,
                kitten
            };
           
            List<Animal> dogs = new List<Animal>();
            List<Animal> kittens = new List<Animal>();
            List<Animal> frogs = new List<Animal>();
            List<Animal> tomcats = new List<Animal>();
            foreach (var animal in animalsList)
            {
                string type = string.Format("{0}", animal.GetType());
                if (type == "_02Animals.Kitten")
                {
                    kittens.Add(animal);
                }
                else if (type == "_02Animals.Dog")
                {
                    dogs.Add(animal);
                }
                else if (type == "_02Animals.Frog")
                {
                    frogs.Add(animal);
                }
                else if (type == "_02Animals.TomCat")
                {
                    tomcats.Add(animal);
                }
            }
            double avgAgeKittens = (double)kittens.Sum(p => p.Age) / kittens.Count;
            double avgAgeTomcats = (double)tomcats.Sum(p => p.Age) / tomcats.Count;
            double avgAgeDogs = (double)dogs.Sum(p => p.Age) / dogs.Count;
            double avgAgeFrogs = (double)frogs.Sum(p => p.Age) / frogs.Count;

            Console.WriteLine("Dogs: {0}\r\nFrogs: {1}\r\nKittens: {2}\r\nTomcats: {3}", avgAgeDogs, avgAgeFrogs, avgAgeKittens, avgAgeTomcats);
        }
Exemple #2
0
        static void Main(string[] args)
        {
            Dog dogge = new Dog("Groznio", 2, true);
            Dog koche = new Dog("Radka", 5, false);

            Tomcat mrycko = new Tomcat("Mrycko", 5);
            Tomcat dendi = new Tomcat("Dendi", 7);

            Kitten daisy = new Kitten("Daisy",4);
            Kitten stasy = new Kitten("Stasy",6);

            Frog prince = new Frog("Prince", 1, true);
            Frog princess = new Frog("Princess", 1, false);

            Animal[] animals = { dogge, koche, mrycko, dendi, daisy, stasy, prince, princess };

            animals.Where(x => x is Dog).Average(x => x.Age);

            double dogsAvgAge = animals.Where(x => x is Dog).Average(x => x.Age);
            double tomcatsAvgAge = animals.Where(x => x is Tomcat).Average(x => x.Age);
            double kittensAvgAge = animals.Where(x => x is Kitten).Average(x => x.Age);
            double frogsAvgAge = animals.Where(x => x is Frog).Average(x => x.Age);

            Console.Write(dogsAvgAge + "\n" + tomcatsAvgAge + "\n" + kittensAvgAge + "\n" + frogsAvgAge + "\n");
        }