static void Main() { // Create arrays of different kinds of animals and calculate the average age of each kind of animal using a static method (you may use LINQ). List <Animal> animals = new List <Animal>(); animals.Add(new Cat("Garfield", 5, Gender.Male)); animals.Add(new Dog("Scooby Doo", 8, Gender.Male)); animals.Add(new Frog("Mr. Toad", 2, Gender.Female)); animals.Add(new Kitten("IWatchYouSleepAtNight", 3)); animals.Add(new Tomcat("Tom", 5)); animals.Add(new Cat("Pisana", 8, Gender.Female)); animals.Add(new Dog("Mark", 4, Gender.Male)); animals.Add(new Frog("Froggy", 2, Gender.Male)); animals.Add(new Kitten("HoomanSlaya", 0)); animals.Add(new Tomcat("PussInBoots", 3)); animals.Add(new Cat("Pesho", 3, Gender.Male)); animals.Add(new Dog("Sharo", 5, Gender.Male)); animals.Add(new Frog("Kwak", 9, Gender.Female)); animals.Add(new Kitten("DreamCrusher", 10)); animals.Add(new Tomcat("Kotio", 9)); var dogs = animals.Where(d => d is Dog); var frogs = animals.Where(f => f is Frog); var cats = animals.Where(c => c is Cat); var tomcats = animals.Where(t => t is Tomcat); var kittens = animals.Where(k => k is Kitten); Console.WriteLine($"Dogs' average age is: {Dog.CalculateAverageAge(dogs)}"); Console.WriteLine($"Frogs' average age is: {Frog.CalculateAverageAge(frogs)}"); Console.WriteLine($"Cats' average age is: {Cat.CalculateAverageAge(cats)}"); Console.WriteLine($"Tomcats' average age is: {Tomcat.CalculateAverageAge(tomcats)}"); Console.WriteLine($"Kittens' average age is: {Kitten.CalculateAverageAge(kittens)}"); }
static void Main() { Animal[] animals = new Animal[] { new Kitten(GetRandomName(), GetRandomAge()), new Tomcat(GetRandomName(), GetRandomAge()), new Dog(GetRandomName(), GetRandomAge(), Gender.Male), new Cat(GetRandomName(), GetRandomAge(), Gender.Male), new Frog(GetRandomName(), GetRandomAge(), Gender.Female), new Frog(GetRandomName(), GetRandomAge(), Gender.Male) }; Console.WriteLine("Some animals:"); foreach (Animal animal in animals) { Console.WriteLine(animal); } Console.WriteLine(); var kittens = new List <Kitten>(); for (int i = 0; i < 5; i++) { Kitten currerntKitten = new Kitten(GetRandomName(), GetRandomAge()); kittens.Add(currerntKitten); } double avrKitten = Kitten.CalculateAverageAge(kittens); Console.WriteLine("Kitten average age: " + avrKitten); var dogs = new List <Dog>(); for (int i = 0; i < 5; i++) { Dog currerntDog = new Dog(GetRandomName(), GetRandomAge(), Gender.Male); dogs.Add(currerntDog); } double avrDog = Dog.CalculateAverageAge(dogs); Console.WriteLine("Dog average age: " + avrDog); }