Beispiel #1
0
 public static double CalculateAverageAge(Animal[] animals)
 {
     double averageAge =
         (from animal in animals
          select animal.Age)
         .Average();
     return averageAge;
 }
        static void Main()
        {
            Animal[] testAnimals = new Animal[] // Create arrays of different kinds of animals
            {
                new Dog("Rex", 3, Gender.Male),
                new Dog("Ricardo", 2, Gender.Male),
                new Dog("Patricia", 4, Gender.Female),

                new Frog("Nicolette", 2, Gender.Female),
                new Frog("Titan", 15, Gender.Male),

                new Cat("Hell", 7, Gender.Male),
                new Cat("Kitty", 3, Gender.Female),

                new Kittens("Lolly", 1),
                new Kittens("Dolly", 6),

                new Tomcats("Pan", 1),
                new Tomcats("Zeus", 2),

            };

            var grouped = testAnimals.GroupBy(x => x.GetType()); // First Goup the animals based on their type

            foreach (var kind in grouped)
            {
                // than calculate the average age of each kind
                Console.WriteLine("Average {0} age is {1}", kind.Key.Name, kind.Average(x => x.Age));
            }

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

            // Test ToString() method
            Console.WriteLine(testAnimals[0]);
            Console.WriteLine(testAnimals[3]);
            Console.WriteLine(testAnimals[8]);

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

            //Test MakeSound method
            var groupSound = from animal in testAnimals
                             group animal by animal.GetType() // group by kind
                             into groups
                             select groups.First(); // and selecting the first element out of the group

            foreach (var animal in groupSound)
            {
                animal.MakeSound();
            }

            Console.WriteLine(new string('=', 50));
        }
Beispiel #3
0
        static void Main()
        {
            Animal[] animals = new Animal[]

            {
                new Dog(3, "Rex", sex.male),
                new Dog(14, "OldSox", sex.male),
                new Frog(1, "Froggie", sex.female),
                new Frog(4, "Qwackstein", sex.male),
                new Tomcat(6, "Timmy"),
                new Tomcat(14, "Mr. Mittens"),
                new Kitten(7, "FluffBall")
            };

            double averageDogsAge = animals.Where(x => x is Dog).Average(x => x.Age);
            double averageFrogsAge = animals.Where(x => x is Frog).Average(x => x.Age);
            double averageCatsAge = animals.Where(x => x is Cat).Average(x => x.Age);
            Console.WriteLine("Average age of the dogs: {0}", averageDogsAge);
            Console.WriteLine("Average age of the frogs: {0}", averageFrogsAge);
            Console.WriteLine("Average age of the cats: {0}", averageCatsAge);
        }
Beispiel #4
0
        static void Main()
        {
            Dog dog = new Dog("Sharo", 2, Gender.Male);
            Console.WriteLine(dog);
            Frog frog = new Frog("Froggy", 1, Gender.Female);
            Console.WriteLine(frog);
            Tomcat tomcat = new Tomcat("Stalin", 5);
            Console.WriteLine(tomcat.Sex);
            Console.WriteLine(tomcat);
            Kitten kitty = new Kitten("Kitty", 3);
            Console.WriteLine(kitty.Sex);
            Console.WriteLine(kitty);

            Animal[] animals = new Animal[]
            {
                new Dog("Balkan",7,Gender.Male),
                new Dog("Ceaser",3,Gender.Male),
                new Dog("Chara",5,Gender.Female),
                new Cat("Lucky",4, Gender.Female),
                new Cat("Rijko",2,Gender.Male),
                new Cat("Sivcho",3,Gender.Male),
                new Frog("Sims",2, Gender.Male),
                new Frog("Jojo",6,Gender.Female),
                new Kitten("Djudji",6),
                new Kitten("Charlie",3),
                new Tomcat("Al Gor",5),
                new Tomcat ("Prince",8)
            };

            Console.WriteLine();
            Console.WriteLine("Average ages:");

            var averageAges=Animal.AverageAge(animals);

            foreach (var animalType in averageAges)
            {
                Console.WriteLine(animalType.Item1 +"-->"+ animalType.Item2);
            }
        }
        public static void Main(string[] args)
        {
            var cats = new Cat[]
            {
                new Cat(25, "Mufasa", Gender.Male),
                new Kitten(5, "Butch"),
                new Tomcat(5, "Snowball"),
            };

            Console.WriteLine("Avarage cat years {0:0.0}", cats.Average(x => x.Age));

            var dogs = new Dog[]
            {
                new Dog(7, "Sharo", Gender.Male),
                new Dog(5, "Strela", Gender.Female),
                new Dog(5, "Droopy", Gender.Male),
            };

            Console.WriteLine("Avarage dog years {0:0.0}", dogs.Average(x => x.Age));

            var animalKingdom = new Animal[]
            {
                new Dog(7, "Sharo", Gender.Male),
                new Dog(5, "Strela", Gender.Female),
                new Frog(65, "Kermit", Gender.Male),
                new Cat(25, "Mufasa", Gender.Male),
                new Kitten(5, "Butch"),
                new Tomcat(5, "Snowball"),
                new Dog(5, "Droopy", Gender.Male),
            };

            foreach (var animal in animalKingdom)
            {
                animal.MakeSound();
            }
        }
 public Cat(string name, int age, Animal.Gender sex)
     : base(name, age, sex)
 {
 }
 public static double AverageAge(Animal[] arr)
 {
     return arr.Average(x => x.Age);
 }
Beispiel #8
0
		public static decimal AverageAge(Animal[] arr)
		{
			return arr.Average(x => (decimal) x.Age);
		}