static void Main(string[] args) { Dog sharo = new Dog("Sharo", 12, "Male"); Dog laika = new Dog("Laika", 20, "Female"); Cat maci = new Cat("Maci", 5, "male"); Cat cezar = new Cat("Cezar", 2, "male"); Frog niki = new Frog("Niki", 4, "male"); Kitten qia = new Kitten("Qia", 1); Kitten misha = new Kitten("Misho", 2); Tomcat gosho = new Tomcat("Gosho", 4); List <Animal> zoo = new List <Animal>() { sharo, laika, maci, cezar, niki, qia, misha, gosho }; sharo.ProduceSound(); qia.ProduceSound(); niki.ProduceSound(); var groupedZoo = from a in zoo group a by a.GetType().Name into g select new { GROUP = g.Key, AVAGE = g.Average(x => x.Age) }; foreach (var animal in groupedZoo) { Console.WriteLine("Average age of {0} is {1}", animal.GROUP, animal.AVAGE); } }
static void Main(string[] args) { Dog dog = new Dog("sharo", AnimalSex.male, 18); Console.WriteLine(dog.ToString()); Frog frog = new Frog("Kermit", AnimalSex.male, 20); Console.WriteLine(frog.ToString()); Cat regularCat = new Cat("CAT", AnimalSex.female, 4); Console.WriteLine(regularCat.ToString()); Kitten kitten = new Kitten("kittie", AnimalSex.female, 1); //Kitten kitten = new Kitten("kittie", AnimalSex.male, 1); - this will explode Console.WriteLine(kitten.ToString()); Tomcat tomcat = new Tomcat("Topcat", AnimalSex.male, 2); //Tomcat tomcat = new Tomcat("Topcat", AnimalSex.female, 2); - this too will explode Console.WriteLine(tomcat.ToString()); //sounds Console.WriteLine(); Console.WriteLine(dog.ProduceSound()); Console.WriteLine(frog.ProduceSound()); Console.WriteLine(regularCat.ProduceSound()); Console.WriteLine(kitten.ProduceSound()); Console.WriteLine(tomcat.ProduceSound()); Console.WriteLine(); List <Dog> dogs = new List <Dog>(); dogs.Add(dog); dogs.Add(new Dog("me4o", AnimalSex.female, 69)); dogs.Add(new Dog("FU", AnimalSex.female, 96)); double averageAge = dogs.Average((x) => x.Age); Console.WriteLine("Dogs average age: {0}", averageAge); List <Frog> frogs = new List <Frog>(); frogs.Add(frog); frogs.Add(new Frog("Big Toad", AnimalSex.male, 2)); frogs.Add(new Frog("Lesser Toad", AnimalSex.female, 2)); averageAge = frogs.Average((x) => x.Age); Console.WriteLine("Frogs average age: {0}", averageAge); List <Kitten> kittens = new List <Kitten>(); kittens.Add(kitten); kittens.Add(new Kitten("little kittenn", AnimalSex.female, 2)); kittens.Add(new Kitten("big kitten", AnimalSex.female, 2)); averageAge = kittens.Average((x) => x.Age); Console.WriteLine("Kittens average age: {0}", averageAge); }
static void Main() { Kitten molly = new Kitten("Molly", 13); Console.WriteLine(molly); molly.ProduceSound(); Frog kiro = new Frog("Kiro", 1, Gender.Female); Console.WriteLine(kiro); kiro.ProduceSound(); Dog sharo = new Dog("Sharo", 5, Gender.Male); Console.WriteLine(sharo); sharo.ProduceSound(); Tomcat pesho = new Tomcat("Pesho", 3); Console.WriteLine(pesho); pesho.ProduceSound(); Cat pena = new Cat("Pena", 2, Gender.Female); Console.WriteLine(pena); pena.ProduceSound(); Kitten dolly = new Kitten("Dolly", 2); Frog biro = new Frog("Biro", 3, Gender.Male); Dog paro = new Dog("Paro", 10, Gender.Female); Tomcat mesho = new Tomcat("Mesho", 1); Cat mena = new Cat("Mena", 1, Gender.Male); Animal[] animals = new Animal[] { molly, kiro, sharo, pesho, pena, dolly, biro, paro, mesho, mena }; var averageAge = from o in animals group o by new { GroupName = o.GetType().Name } into gr select new { gr.Key.GroupName, AvarageAge = gr.Average(o => o.Age) }; foreach (var animal in averageAge) { Console.WriteLine(String.Format("Type: {0}, Average age: {1:0.##}", animal.GroupName, animal.AvarageAge)); } }
static void Main() { Tomcat cat = new Tomcat("jonny", 4); Kitten cat2 = new Kitten("Holly", 2); Frog frog = new Frog("Jwey", 6, "male"); Dog dog = new Dog("Sharo", 3, "male"); cat.ProduceSound(); cat2.ProduceSound(); frog.ProduceSound(); dog.ProduceSound(); Animal[] animals = { cat, cat2, frog, dog, new Dog("Polly", 12, "female"), new Frog("Kolio", 2, "male"), new Tomcat("Nuni", 3), new Kitten("Loly", 9), new Frog("Josh", 1, "male"), new Kitten("Jenny", 5), new Dog("Bond", 5, "male"), new Tomcat("Josh", 14) }; var dogs = from animal in animals where animal is Dog select animal; var frogs = from animal in animals where animal is Frog select animal; var cats = from animal in animals where animal is Cat select animal; Console.WriteLine("Dogs avg age: " + ((double)dogs.Sum(a => a.Age)) / dogs.Count()); Console.WriteLine("Frogs avg age: " + ((double)frogs.Sum(a => a.Age)) / frogs.Count()); Console.WriteLine("Cats avg age: " + ((double)cats.Sum(a => a.Age)) / cats.Count()); }
static void Main(string[] args) { Dog dog = new Dog("sharo", AnimalSex.male, 18); Console.WriteLine(dog.ToString()); Frog frog = new Frog("Kermit", AnimalSex.male, 20); Console.WriteLine(frog.ToString()); Cat regularCat = new Cat("CAT", AnimalSex.female, 4); Console.WriteLine(regularCat.ToString()); Kitten kitten = new Kitten("kittie", AnimalSex.female, 1); //Kitten kitten = new Kitten("kittie", AnimalSex.male, 1); - this will explode Console.WriteLine(kitten.ToString()); Tomcat tomcat = new Tomcat("Topcat", AnimalSex.male, 2); //Tomcat tomcat = new Tomcat("Topcat", AnimalSex.female, 2); - this too will explode Console.WriteLine(tomcat.ToString()); //sounds Console.WriteLine(); Console.WriteLine(dog.ProduceSound()); Console.WriteLine(frog.ProduceSound()); Console.WriteLine(regularCat.ProduceSound()); Console.WriteLine(kitten.ProduceSound()); Console.WriteLine(tomcat.ProduceSound()); Console.WriteLine(); List<Dog> dogs = new List<Dog>(); dogs.Add(dog); dogs.Add(new Dog("me4o", AnimalSex.female, 69)); dogs.Add(new Dog("FU", AnimalSex.female, 96)); double averageAge = dogs.Average((x) => x.Age); Console.WriteLine("Dogs average age: {0}", averageAge); List<Frog> frogs = new List<Frog>(); frogs.Add(frog); frogs.Add(new Frog("Big Toad", AnimalSex.male, 2)); frogs.Add(new Frog("Lesser Toad", AnimalSex.female, 2)); averageAge = frogs.Average((x) => x.Age); Console.WriteLine("Frogs average age: {0}", averageAge); List<Kitten> kittens = new List<Kitten>(); kittens.Add(kitten); kittens.Add(new Kitten("little kittenn", AnimalSex.female, 2)); kittens.Add(new Kitten("big kitten", AnimalSex.female, 2)); averageAge = kittens.Average((x) => x.Age); Console.WriteLine("Kittens average age: {0}", averageAge); }