Ejemplo n.º 1
0
 static void Main(string[] args)
 {
     Dog dog = new Dog("Rexy",25,"male");
     Console.WriteLine(dog);
     Console.WriteLine("Press any key to hear Rexy bark..");
     Console.ReadKey();
     dog.ProduceSound();
     Console.WriteLine(Environment.NewLine);
     Frog frog = new Frog("Anabel", 25, "female");
     Console.WriteLine(frog);
     Console.WriteLine("Press any key to hear Anabel creek..");
     Console.ReadKey();
     frog.ProduceSound();
     Console.WriteLine(Environment.NewLine);
     Tomcat tomcat = new Tomcat("Spot", 25);
     Console.WriteLine(tomcat);
     Console.WriteLine("Press any key to hear Spot meow..");
     Console.ReadKey();
     tomcat.ProduceSound();
     Console.WriteLine(Environment.NewLine);
     Kitten kitten = new Kitten("Cassandra", 15);
     Console.WriteLine(kitten);
     Console.WriteLine("Press any key to hear Cassandra bark..");
     Console.ReadKey();
     kitten.ProduceSound();
 }
Ejemplo n.º 2
0
        public static void Main()
        {
            Kitten[] kittens = new[]
            {
                new Kitten("Hello Kitty", 4),
                new Kitten("Catwoman", 6),
                new Kitten("Thunder cat", 11)
            };

            Tomcat[] tomcats = new[]
            {
                new Tomcat("Mr. Bigglesworth", 2),
                new Tomcat("Felix", 5),
                new Tomcat("Tom", 2)
            };

            Dog[] dogs = new Dog[]
            {
                new Dog("Brian Griffin", 2, Genders.Male),
                new Dog("Too stupid dog", 5, Genders.Male),
                new Dog("Eddie from Frasier", 7, Genders.Male)
            };

            Frog[] frogs = new[]
            {
                new Frog("Hypnotoad from Futurama", 13, Genders.Male),
                new Frog("Kermit", 50, Genders.Male),
                new Frog("Prince Charming", 30, Genders.Male),
            };

            Console.WriteLine("Average kitten age: {0}", kittens.Average(n => n.Age));
            Console.WriteLine("Average tomcat age: {0}", tomcats.Average(n => n.Age));
            Console.WriteLine("Average dog age: {0}", dogs.Average(n => n.Age));
            Console.WriteLine("Average frog age: {0}", frogs.Average(n => n.Age));
        }
        static void Main()
        {

            Kitten Lili = new Kitten("Lili",2);
            Dog Sharo = new Dog("Sharo", 4, Gender.Male);
            Frog Frog = new Frog("Frog", 5, Gender.Female);
            Cat Jerry = new Cat("Jerry",7,Gender.Male);
            Tomcat Tom = new Tomcat("Tom", 3);
            Dog Pyhcho = new Dog("Pyhcho", 1, Gender.Male);
            List<Animal> animals = new List<Animal>() { Lili, Sharo, Frog, Tom,Pyhcho,Jerry };

            foreach (var animal in animals)
            {
                Console.WriteLine("{0}\n", animal);
            }

            animals
                .GroupBy(animal => animal.GetType().Name)
                .Select(group => new
                {
                    AnimalName = group.Key,
                    AverageAge = group.Average(a => a.Age)
                })
                .ToList()
                .ForEach(group => Console.WriteLine("{0} average age : {1}",group.AnimalName,group.AverageAge));
        }
Ejemplo n.º 4
0
        static void Main()
        {
            Cat cat1 = new Cat("Tom", 2, "Male");
            cat1.ProduceSound();
            Cat cat2 = new Cat("Minka", 3, "Female");
            cat2.ProduceSound();
            Cat cat3 = new Cat("Gosho", 4, "Male");
            cat3.ProduceSound();

            Dog dog1 = new Dog("Lassie", 3, "Female");
            dog1.ProduceSound();
            Dog dog2 = new Dog("Haruki", 4, "Male");
            dog2.ProduceSound();
            Dog dog3 = new Dog("Murdjo", 5, "Male");
            dog3.ProduceSound();

            Frog frog1 = new Frog("Prince", 1, "Male");
            frog1.ProduceSound();
            Frog frog2 = new Frog("Igor", 2, "Male");
            frog2.ProduceSound();
            Frog frog3 = new Frog("Jivka", 1, "Female");
            frog3.ProduceSound();

            Kitten kitten1 = new Kitten("Bira", 1);
            kitten1.ProduceSound();
            Kitten kitten2 = new Kitten("Chertichka", 4);
            kitten2.ProduceSound();
            Kitten kitten3 = new Kitten("Pepa", 2);
            kitten3.ProduceSound();

            Tomcat tomcat1 = new Tomcat("Coolio", 4);
            tomcat1.ProduceSound();
            Tomcat tomcat2 = new Tomcat("Pantelei", 3);
            tomcat2.ProduceSound();
            Tomcat tomcat3 = new Tomcat("Kotyo", 6);
            tomcat3.ProduceSound();

            Console.WriteLine(new string('=', 30));

            Animal[] animals = { cat1, cat2, cat3, dog1, dog2, dog3, frog1, frog2, frog3, kitten1, kitten2, kitten3, tomcat1, tomcat2, tomcat3 };

            double averageAge = animals.Average(x => x.Age);
            Console.WriteLine("Average age of all animals is {0:0.##} years.", averageAge);

            double averageAgeCat = animals.Where(x => x is Cat).Average(x => x.Age);
            Console.WriteLine("Average age of the cats is {0:0.##} years.", averageAgeCat);

            double averageAgeDog = animals.Where(x => x is Dog).Average(x => x.Age);
            Console.WriteLine("Average age of the dogs is {0:0.##} years.", averageAgeDog);

            double averageAgeFrog = animals.Where(x => x is Frog).Average(x => x.Age);
            Console.WriteLine("Average age of the frogs is {0:0.##} years.", averageAgeFrog);

            double averageAgeKitten = animals.Where(x => x is Kitten).Average(x => x.Age);
            Console.WriteLine("Average age of the kitten is {0:0.##} years.", averageAgeKitten);

            double averageAgeTomcat = animals.Where(x => x is Tomcat).Average(x => x.Age);
            Console.WriteLine("Average age of the tomcats is {0:0.##} years.", averageAgeTomcat);
        }
Ejemplo n.º 5
0
        private static void Main(string[] args)
        {
            Animal[] animals = new Animal[10];
            animals[0] = new Frog("Gosho", 3, "male");
            animals[1] = new Kitten("Kitty", 5);
            animals[2] = new Dog("Rex", 12, "male");
            animals[3] = new Tomcat("Tom", 1);
            animals[4] = new Kitten("Bety", 6);
            animals[5] = new Frog("Frogy", 1, "female");
            animals[6] = new Tomcat("Pesho", 7);
            animals[7] = new Dog("Sarah", 4, "female");
            animals[8] = new Kitten("Katy", 1);
            animals[9] = new Frog("Kwiky", 3, "female");

            animals.GroupBy(a => a.GetType().Name)
                .Select(group => new
                {
                   AnimalName = group.Key,
                   AverageAge = group.Average(a => a.Age)
                })
                .OrderByDescending(group => group.AverageAge)
                .ToList()
                .ForEach(group => Console.WriteLine($"{group.AnimalName}'s average age is: {group.AverageAge}"));
        }
Ejemplo n.º 6
0
        static void Main(string[] args)
        {
            Kitten pipi   = new Kitten("Pipi", 8);
            Tomcat momo   = new Tomcat("Momo", 5);
            Cat    chocho = new Cat("Chocho", 11, "Male");
            Cat    puhi   = new Cat("Puhi", 3, "Female");

            List <Cat> cats = new List <Cat>
            {
                pipi,
                momo,
                chocho,
                puhi
            };

            foreach (var cat in cats)
            {
                Console.Write(cat + " , Says: ");
                cat.ProduceSound();
            }
            Dog sharo  = new Dog("Sharo", 9, "Male");
            Dog loki   = new Dog("Loki", 2, "Male");
            Dog shaila = new Dog("Shaila", 7, "Female");

            List <Dog> dogs = new List <Dog>
            {
                sharo,
                loki,
                shaila
            };

            foreach (var dog in dogs)
            {
                Console.Write(dog + " , Says: ");
                dog.ProduceSound();
            }

            Frog frogy  = new Frog("Frogy", 2, "Male");
            Frog greeny = new Frog("Greeny", 6, "Female");
            Frog doty   = new Frog("Doty", 1, "Female");

            List <Frog> frogs = new List <Frog>
            {
                frogy,
                greeny,
                doty
            };

            foreach (var frog in frogs)
            {
                Console.Write(frog + " , Says: ");
                frog.ProduceSound();
            }

            List <Animal> averageAge = new List <Animal>
            {
                pipi,
                momo,
                chocho,
                puhi,
                sharo,
                loki,
                shaila,
                frogy,
                greeny,
                doty
            };

            var aveAge =
                from average in averageAge
                group average by new { Group = average.GetType().Name } into grouped
                select new
            {
                Group      = grouped.Key.Group,
                AverageAge = grouped.Average(a => a.Age)
            };

            foreach (var animal in aveAge)
            {
                Console.WriteLine(animal);
            }
        }