static void Main() { Frog kerm = new Frog("Kerm", 4, "male"); kerm.ProduceSound(); Tomcat tommy = new Tomcat("Tommy", 8, ""); tommy.ProduceSound(); Animal[] animals = new Animal[] { new Dog("SnoopDog", 5, "male"), new Kitten("Mia", 2, ""), new Tomcat("Tom", 5, ""), new Frog("Kermit", 12, "male"), new Dog("Max", 1, "male"), }; foreach (var animal in animals) { Console.WriteLine(animal); } //AvarageAgeOfAll\\ double avarageOfAll = animals.Average(animal => animal.Age); Console.WriteLine("Avarage: {0}", avarageOfAll); //AvarageOfDog\\ double avarageOfDog = animals.OfType <Dog>().Average(dog => dog.Age); Console.WriteLine("Avarage Dog:{0}", avarageOfDog); //AvarageOfCat\\ double avarageOfCat = animals.OfType <Cat>().Average(cat => cat.Age); Console.WriteLine("Avarage Cat:{0}", avarageOfCat); }
static void Main(string[] args) { int[] animalAges = new int[5]; Animal frog = new Frog("Froggy", 1, "male"); frog.ProduceSound(); animalAges[0] = frog.Age; Animal kitten = new Kitten("Kitty", 4); kitten.ProduceSound(); animalAges[1] = kitten.Age; Animal dog = new Dog("Sharo", 10, "male"); dog.ProduceSound(); animalAges[2] = dog.Age; Animal tomcat = new Tomcat("Tiger", 6); tomcat.ProduceSound(); animalAges[3] = tomcat.Age; Animal cat = new Cat("Fluffy", 3, "female"); cat.ProduceSound(); animalAges[4] = cat.Age; Animal cat2 = new Cat("Maui Mauski", 18, "male"); cat2.ProduceSound(); Console.WriteLine("Average: {0}", calcAverage(animalAges)); }
static void Main(string[] args) { var dog = new Dog("Sharo", 3, Gender.Male); var dog1 = new Dog("Pumiar", 9, Gender.Male); dog.ProduceSound(); var frog = new Frog("Jabcho", 2, Gender.Male); var frog1 = new Frog("Cermit", 4, Gender.Male); frog.ProduceSound(); var tomcat = new Tomcat("Tom", 5); var tomcat1 = new Tomcat("Garfield", 1); tomcat.ProduceSound(); var kitten = new Kitten("Kitty", 2); var kitten1 = new Kitten("Sarra", 7); kitten.ProduceSound(); Animal[] animals = { dog, dog1, frog, frog1, tomcat, tomcat1, kitten, kitten1 }; double dogsAverageAge = animals.Where(a => a is Dog).Average(a => a.Age); double frogsAverageAge = animals.Where(a => a is Frog).Average(a => a.Age); double kittensAverageAge = animals.Where(a => a is Kitten).Average(a => a.Age); double tomcatsAverageAge = animals.Where(a => a is Tomcat).Average(a => a.Age); Console.WriteLine("Dogs average age is {0:F2}", dogsAverageAge); Console.WriteLine("Frogs average age is {0:F2}", frogsAverageAge); Console.WriteLine("Tomcats average age is {0:F2}", tomcatsAverageAge); Console.WriteLine("Kittens average age is {0:F2}", kittensAverageAge); }