private static void Main() { Cat tom = new Cat("Tom", "male", 2); Dog spike = new Dog("Spike", "male", 3); Frog kermit = new Frog("Kermit", "male", 2); Tomcat tomjr = new Tomcat("Prinkles", 1); Kitten marge = new Kitten("Marge", 1); Dog lassy = new Dog("Lassy", "female", 2); Frog molly = new Frog("Molly", "female", 4); Animals[] animals = new Animals[7]; animals[0] = tom; animals[1] = spike; animals[2] = kermit; animals[3] = tomjr; animals[4] = marge; animals[5] = lassy; animals[6] = molly; //Took this one from forums - source [borislavml] only difference is this is a separeted as a method. CalculateAndPrintAvarageAge(animals); // Optional(mine desicion) double catAvgAge = GetAvarageAges("Cat", animals); double dogAvgAge = GetAvarageAges("Dog", animals); double frogAvgAge = GetAvarageAges("Frog", animals); Console.WriteLine(catAvgAge); Console.WriteLine(dogAvgAge); Console.WriteLine(frogAvgAge); }
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(); }
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)); }
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); }
static void Main() { Frog f1 = new Frog("Billy", 0.2, "m"); Dog d1 = new Dog("Liza", 4, "f"); Cat c1 = new Cat("Kotaksi", 2, "m"); Kitten k1 = new Kitten("Raya", 1); Tomcat t1 = new Tomcat("Rijo", 2); f1.ProduceSound(); d1.ProduceSound(); c1.ProduceSound(); k1.ProduceSound(); t1.ProduceSound(); Console.WriteLine(); Animal[] animals = new Animal[] { new Frog("Billy", 0.2, "m"), new Dog("Liza", 4, "f"), new Cat("Kotaksi", 2, "m"), new Kitten("Raya", 1), new Tomcat("Rijo", 2), new Frog("Pesho", 2.1, "f"), new Dog("Beti", 5.4, "f"), new Cat("Kotka", 2, "f"), new Kitten("Spaska", 4), new Tomcat("Gosho", 3), new Frog("Marmot", 4, "m"), new Tomcat("Bizen", 0.5), new Dog("India", 2.5, "f"), }; animals. GroupBy(animal => animal.GetType().Name). Select(group => new { name = group.Key, averageYears = group.Average(animal => animal.Age) }). ToList(). ForEach(group => Console.WriteLine("Group: {0}, average age: {1:F2} years!", group.name, group.averageYears)); }
static void Main(string[] args) { Cat pisana = new Cat("Pisana", 10); Cat pisanche = new Cat("Pisanche", 5); Tomcat tomi = new Tomcat("Tomi", 4, Gender.Male); Tomcat tomito = new Tomcat("Tomito", 3, Gender.Male); Kitten macurana = new Kitten("Macurana", 2, Gender.Female); Kitten macuranka = new Kitten("Macuranka", 6, Gender.Female); Dog rex = new Dog("Rex", 3); Dog karaman = new Dog("Karaman", 1); Frog frogy = new Frog("Frogy", 8); Frog frogy1 = new Frog("Frogy1", 2); Console.WriteLine(pisana); Console.WriteLine(tomi); Console.WriteLine(pisanche); Console.WriteLine(rex); Console.WriteLine(frogy); IList <Animal> allAnimal = new List <Animal>(); allAnimal.Add(pisana); allAnimal.Add(pisanche); allAnimal.Add(tomi); allAnimal.Add(tomito); allAnimal.Add(macurana); allAnimal.Add(macuranka); allAnimal.Add(rex); allAnimal.Add(karaman); allAnimal.Add(frogy); allAnimal.Add(frogy1); foreach (var kind in allAnimal.GroupBy(x => x.GetType().Name)) { double averageAge = kind.Select(x => x.Age).Average(); Console.WriteLine("Animal : {0}, AverageAge: {1}", kind.Key, averageAge); } }
static void Main() { var cat = new Cat("Siamka", 2.5, Gender.Female); var dog = new Dog("Labrador", 2, Gender.Male); var frog = new Frog("Jabcho", 3, Gender.Male); var kitten = new Kitten("Kiti", 0.3); var tomcat = new Tomcat("Tomi", 0.5); Animal[] animals = { cat, dog, frog, kitten, tomcat }; var avgAge = animals.Select(a => a.Age).Average(); Console.WriteLine("The average age of the animals is " + avgAge); Console.WriteLine(); foreach (var animal in animals) { Console.WriteLine(animal.GetType().Name + " kaji " + animal.ProduceSound()); Console.WriteLine($"{animal.Name} kaza {animal.ProduceSound()}"); Console.WriteLine(); } }
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}")); }
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); } }