static void Main() { Animal max = new Dog("Max", 3, Gender.Male); Animal duke = new Dog("Duke", 1, Gender.Male); Animal fluffy = new Dog("Fluffy", 6, Gender.Male); Animal wink = new Dog("Wink", 3, Gender.Male); Animal abby = new Frog("Abby", 1, Gender.Female); Animal boogy = new Frog("Boogy", 2, Gender.Male); Animal pookie = new Frog("Pookie", 6, Gender.Female); Animal hermit = new Frog("Hermit", 2, Gender.Male); Animal abbey = new Kitten("Abbey", 1, Gender.Female); Animal panda = new Kitten("Panda", 1, Gender.Female); Animal ravi = new Tomcat("Ravi", 3, Gender.Male); Animal lexus = new Tomcat("Lexus", 9, Gender.Male); Animal biggie = new Cat("Biggie", 7, Gender.Male); Animal fig = new Cat("Fig", 7, Gender.Male); Animal[] animals = new Animal[] { max, duke, fluffy, wink, abby, boogy, pookie, hermit, abbey, panda, ravi, lexus, biggie, fig }; var groupedAnimals = from an in animals group an by new { GroupName = an.GetType().Name } into grp select new { GroupName = grp.Key.GroupName, AverageAge = grp.Average(an => an.Age) }; foreach (var animal in groupedAnimals) { Console.WriteLine("Group: {0} | Average Age: {1:F2}", animal.GroupName, animal.AverageAge); } }
static void Main() { Animal[] animals = new Animal[10] { new Cat("Pesho", 2, "male"), new Kitten("Penka", 3), new Tomcat("Gosho", 1), new Frog("Mitko", 3, "male"), new Dog("Goran", 2, "male"), new Dog("Dimitrichka", 2, "female"), new Cat("Ani", 1, "female"), new Frog("Geri", 4, "female"), new Tomcat("Pesho", 5), new Kitten("Marta", 6) }; animals.ToList().ForEach(Console.WriteLine); Console.WriteLine(); animals .GroupBy(animal => animal.GetType().Name) .Select(group => new { AnimalName = group.Key, AverageAge = group.Average(a => a.Age) }) .OrderByDescending(group => group.AverageAge) .ToList() .ForEach(group => Console.WriteLine("{0}'s average age is: {1}", group.AnimalName, group.AverageAge)); }
static void Main() { var scooby = new Dog("Scooby", 2, 'm'); var frogy = new Frog("Frogy", 0.5, 'm'); var tommy = new Cat("Tommy", 1, 'm'); var kitty = new Kitten("Kitty", 0.8, 'f'); var speedy = new Tomcat("Speedy", 1.5, 'f'); var spark = new Dog("Spark", 3, 'm'); var carmit = new Frog("Carmit", 1, 'm'); var jenny = new Kitten("Jenny", 1.2, 'f'); var animals = new Animal[] { scooby, spark, frogy, tommy, kitty, speedy }; var differentAnimals = new Animal[] { tommy, kitty, carmit, jenny }; foreach (var animal in animals) { Console.Write(animal); animal.ProduceSound(); Console.WriteLine(); } var groupedAnimals = from anim in animals group anim by (anim is Cat) ? typeof (Cat) : anim.GetType() into g select new {GroupName = g.Key, AverageAge = g.ToList().Average(an => an.Age)}; foreach (var animal in groupedAnimals) { Console.WriteLine("{0} - average age: {1:N2}", animal.GroupName.Name, animal.AverageAge); } }
static void Main(string[] args) { Animal[] animals = new Animal[10] { new Cat("Ivanka", 10, "Female"), new Dog("Doge",12,"Male"), new Frog("Jabcho",15,"Male"), new Kitten("Gergina",8), new Tomcat("Goshko",6), new Cat("Dragan",1,"Male"), new Dog("Dogina",4,"Female"), new Frog("Jabka",9,"Female"), new Kitten("Dora",19), new Tomcat("Tom", 20) }; animals.ToList().ForEach(Console.WriteLine); Console.ReadKey(); animals.GroupBy(animal => animal.GetType().Name).Select(group => new { AnimalName = group.Key, AverageAge = group.Average(age => age.Age) }) .OrderBy(group => group.AverageAge) .ToList() .ForEach(group => Console.WriteLine($"{group.AnimalName}'s average age is: {group.AverageAge}")); }
public static void Print(Animal[] animals) { foreach (var animal in animals) { Console.WriteLine(animal); } }
static void Main(string[] args) { Dog Sharo = new Dog(10, "Sharo", Sex.Male, "bao lau"); Console.WriteLine(Sharo); Animal[] Animals = new Animal[] { new Tomcat(15, "Маци", "miau"), new Tomcat(11, "Котан", "miau miau"), new Tomcat(1, "Джери", "bau miau"), }; Cat[] cats = new Cat[] { new PussyCat(1, "Мац", "bau miau"), new Tomcat(2, "Тим", "bau miau"), }; Console.WriteLine("# Animals"); foreach (Animal animal in Animals) Console.WriteLine(animal); Console.WriteLine("# Average"); Console.WriteLine(Animals.Average(animal => animal.age)); Console.WriteLine(cats.Average(cat => cat.age)); }
static void Main() { // create some animals from each class and print them Dog dog = new Dog("Sharo", 3, Gender.Male); Console.WriteLine(dog); dog.FetchStick(); Console.WriteLine(); Frog frog = new Frog("Kermit", 33, Gender.Male); Console.WriteLine(frog); frog.Jump(); Console.WriteLine(); Kitten kitty = new Kitten("Kitty", 1); Console.WriteLine(kitty); kitty.Cry(); Console.WriteLine(); Tomcat tomcat = new Tomcat("Tom", 12); Console.WriteLine(tomcat); tomcat.Piss(); Console.WriteLine(); // create an array of Animals Animal[] animals = new Animal[] { new Dog("Rex",3, Gender.Male), new Frog("Kekerica", 1, Gender.Female), new Kitten("Pisi", 1), new Tomcat("Tom",2), new Dog("Erik", 4, Gender.Male), new Frog("Penka", 1, Gender.Female), new Kitten("Jasmin", 2), new Tomcat("Kolio",6), new Dog("Bender",2, Gender.Male), new Frog("Ginka", 6, Gender.Female), new Kitten("Tedy", 1), new Tomcat("Muncho",4), }; // calculate the aveage age of each animal and print them var averageAge = from an in animals group an by new { GroupName = an.GetType().Name } into gr select new { GroupName = gr.Key.GroupName, AvarageAge = gr.Average(an => an.Age) }; foreach (var animal in averageAge) { Console.WriteLine(String.Format("Group: {0}, AvarageAge: {1:0.00}.", animal.GroupName, animal.AvarageAge)); } }
public static double AverageAge(Animal[] animals) { double ages = 0; for (int i = 0; i < animals.Length; i++) { ages += animals[i].Age; } return ages / animals.Length; }
public static double Average(Animal[] array) { double sum = 0; for (int i = 0; i < array.Length; i++) { sum = sum + array[i].Age; } return sum / array.Length; }
private static IEnumerable<Tuple<string, double>> GetAverageAges(Animal[] animals) { var averageAges = from animal in animals group animal by animal.GetType() into animalType select new Tuple<string, double>(animalType.Key.Name, animalType.Average(a => a.Age)); return averageAges; }
static void Main() { // Attention! - No animals were harmed during these tests Console.Title = "Animals"; var dog = new Dog("Gosho", 3, Gender.Male); Console.WriteLine(dog); dog.FetchStick(); Console.Write(Environment.NewLine); var frog = new Frog("Masha", 16, Gender.Female); Console.WriteLine(frog); frog.Transform(); Console.Write(Environment.NewLine); var kitty = new Kitten("Maca", 0); Console.WriteLine(kitty); kitty.MeltYourHeart(); Console.Write(Environment.NewLine); var tomcat = new Tomcat("Tom", 4); Console.WriteLine(tomcat); tomcat.Piss(); Console.Write(Environment.NewLine); var animals = new Animal[] { new Dog("Freya", 1, Gender.Female), new Dog("Sharo", 4, Gender.Male), new Frog("Penka", 20, Gender.Female), new Frog("Fred", 32, Gender.Male), new Kitten("Merlin", 1), new Kitten("Shusia", 0), new Tomcat("Felix",6), new Tomcat("Silvester", 5), }; var averageAge = from an in animals group an by new { GroupName = an.GetType().Name } into gender select new { gender.Key.GroupName, AvarageAge = gender.Average(an => an.Age) }; foreach (var animal in averageAge) { Console.WriteLine(String.Format("Group: {0}, AvarageAge: {1:0.00}.", animal.GroupName, animal.AvarageAge)); } // And what does the Fox say? Oh, no foxes here... }
public static double Average(Animal[] animal) { double sum = 0; foreach (var an in animal) { sum += an.Age; } return sum / animal.Length; }
static double AverageAge(Animal[] animls) { double overalAge = 0; int count = 0; foreach (var animal in animls) { count++; overalAge = overalAge + animal.Age; } return overalAge / count; }
static double AverageAge(Animal[] animalList) { double average = 0; foreach (var animal in animalList) { average += animal.Age; //average = average + animal.Age; } return average / animalList.Length; }
static void Main() { Animal[] dogArray = new Animal[5]; dogArray[0] = new Dog("Sharo", 4, "male"); dogArray[1] = new Dog("Rex", 1, "male"); dogArray[2] = new Dog("Lasi", 10, "male"); dogArray[3] = new Dog("Djinka", 3, "female"); dogArray[4] = new Dog("Princess", 2, "female"); foreach (var animal in dogArray) { Console.WriteLine(animal); } var dogArrayAvg = dogArray.Average(d => d.Age); Console.WriteLine("Average dog age: {0:F2}{1}",dogArrayAvg, Environment.NewLine); Console.WriteLine(); Cat[] catArray = new Cat[5]; catArray[0] = new Kitten("Mici", 3); catArray[1] = new Tomcat("Tom", 5); catArray[2] = new Kitten("Grozdanka", 2); catArray[3] = new Tomcat("Grozdan", 10); catArray[4] = new Kitten("Micana", 7); foreach (var cat in catArray) { Console.WriteLine(cat); } var catArrayAvg = catArray.Average(c => c.Age); Console.WriteLine("Average cat age: {0:F2}{1}",catArrayAvg, Environment.NewLine); Console.WriteLine(); Frog[] frogArray = new Frog[5]; frogArray[0] = new Frog("Kyrmit", 4, "male"); frogArray[1] = new Frog("Kyrmita", 2, "female"); frogArray[2] = new Frog("Kyrmin", 6, "male"); frogArray[3] = new Frog("Kyrmina", 3, "female"); frogArray[4] = new Frog("Kyrtin", 8, "male"); foreach (var frog in frogArray) { Console.WriteLine(frog); } var frogArrayAvg = frogArray.Average(f => f.Age); Console.WriteLine("Average frog age: {0:F2}{1}", frogArrayAvg, Environment.NewLine); Console.WriteLine(); }
public static string GetAnimalKind(Animal animal) { string kind = ""; if (animal.GetType().BaseType.Name == "Animal") { kind = animal.GetType().Name; } else { kind = animal.GetType().BaseType.Name; } return kind; }
static void Main(string[] args) { Dog[] dogs = { new Dog("Dog1", 1, 'm'), new Dog("Dog2", 4, 'f'), new Dog("Dog3", 2, 'f')}; Cat[] cats = { new Cat("Cat1", 6, 'f'), new Kitten("Cat2", 3), new Tomcat("Cat3", 2)}; Frog[] frogs = { new Frog("Frog1", 1, 'm'), new Frog("Frog2", 3, 'f'), new Frog("Frog3", 2, 'm')}; Animal[] animals = new Animal[dogs.Length + cats.Length + frogs.Length]; int ind = 0; foreach (Dog dog in dogs) { Console.Write("{0} and I say: ", dog.Introduce()); dog.ProduceSound(); animals[ind] = dog; ind++; } foreach (Cat cat in cats) { Console.Write("{0} and I say: ", cat.Introduce()); cat.ProduceSound(); animals[ind] = cat; ind++; } foreach (Frog frog in frogs) { Console.Write("{0} and I say: ", frog.Introduce()); frog.ProduceSound(); animals[ind] = frog; ind++; } Console.WriteLine("The Average Age of the dogs is: {0:0.00}", Animal.AverageAge(dogs)); Console.WriteLine("The Average Age of the cats is: {0:0.00}", Animal.AverageAge(cats)); Console.WriteLine("The Average Age of the frogs is: {0:0.00}", Animal.AverageAge(frogs)); Console.WriteLine("The Average Age of the animals is: {0:0.00}", Animal.AverageAge(animals)); }
static void Main() { var animals = new Animal[] { new Dog("Miro", 2, "male"), new Dog("Laila", 5, "female"), new Cat("Kotka", 3, "male"), new Cat("Murla", 7, "female"), new Frog("Kraker", 4, "male"), new Frog("Angelica", 6, "female"), new Tomcat("Tom", 1), new Tomcat("Frank", 1), new Kitten("Sashka", 1), new Kitten("Igrivka", 1) }; int totalAnimalAge = 0; foreach (var animal in animals.OfType<Dog>()) { totalAnimalAge += animal.Age; } Console.WriteLine("Average age of animals of type Dog is " + (totalAnimalAge / 2)); totalAnimalAge = 0; foreach (var animal in animals.OfType<Cat>()) { totalAnimalAge += animal.Age; } Console.WriteLine("Average age of animals of type Cat is " + ((totalAnimalAge - 4) / 2)); totalAnimalAge = 0; foreach (var animal in animals.OfType<Frog>()) { totalAnimalAge += animal.Age; } Console.WriteLine("Average age of animals of type Frog is " + (totalAnimalAge / 2)); totalAnimalAge = 0; foreach (var animal in animals.OfType<Tomcat>()) { totalAnimalAge += animal.Age; } Console.WriteLine("Average age of animals of type Tomcat is " + (totalAnimalAge / 2)); totalAnimalAge = 0; foreach (var animal in animals.OfType<Kitten>()) { totalAnimalAge += animal.Age; } Console.WriteLine("Average age of animals of type Kitten is " + (totalAnimalAge / 2)); }
public static void Main() { // Create couple of different Animals and add to array var animals = new Animal[9] { new Dog("Sharo", 10, Gender.Male), new Dog("Baro", 4, Gender.Male), new Dog("Ceca", 5, Gender.Female), new Dog("Tochka", 7, Gender.Female), new Frog("Kvaki", 54, Gender.Female), new Frog("To", 9, Gender.Male), new Kitten("Tomka", 2), new Tomcat("Tom", 10), new Tomcat("Slon", 12) }; // Print the average age per Animal type using LINQ Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("Average age calculation using method based on LINQ"); Console.ForegroundColor = ConsoleColor.White; foreach (var animal in AverageAgeLinq(animals)) { Console.WriteLine("Average age for {0}s is: {1}", animal.Key, animal.Value); } // Print the average age per Animal type using Extension methods Console.WriteLine(); Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("Average age calculation using method based on extension methods"); Console.ForegroundColor = ConsoleColor.White; foreach (var animal in AverageAgeExt(animals)) { Console.WriteLine("Average age for {0}s is: {1}", animal.Key, animal.Value); } // Print the average age per Animal type using normal C# language methods Console.WriteLine(); Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("Average age calculation using method based on C# language methods/operators"); Console.ForegroundColor = ConsoleColor.White; foreach (var animal in AverageAge(animals)) { Console.WriteLine("Average age for {0}s is: {1}", animal.Key, animal.Value); } }
static void Main(string[] args) { Animal[] animals = new Animal[5]; animals[0]=new Cat("Mac",10,Gender.Male); animals[1]=new Kitten("Max",2); animals[2]=new Kitten("Missi",1); animals[3]=new Frog("Kermit",3,Gender.Male); animals[4]=new Frog("Tom",5,Gender.Male); animals.ToList(); foreach (var animal in animals) { Console.WriteLine(animal); animal.ProduceSoun(); } animals.GroupBy(t=>t.GetType().Name) .Select(group=>new {AnimalName=group.Key,AvarageAge=group.Average(a=>a.Age)}) .ToList() .ForEach(group=>Console.WriteLine("{0}{1}",group.AnimalName,group.AvarageAge)); }
static void Main() { Tomcat tom = new Tomcat("Tom", 12); Console.WriteLine(tom); Kitten kitty = new Kitten("Kitty", 3); Console.WriteLine(kitty); Console.WriteLine(); List<ISound> listAnimals = new List<ISound>(); listAnimals.Add(new Cat("Pepi", 3, "female")); listAnimals.Add(new Frog("Kurmit", 10, "male")); listAnimals.Add(new Dog("Sharo", 5, "male")); listAnimals.Add(new Cat("Jorko", 6, "male")); listAnimals.Add(new Dog("Balkan", 10, "male")); listAnimals.Add(kitty); listAnimals.Add(tom); foreach (var animal in listAnimals) { Console.WriteLine(animal + " " + animal.MakeSound()); } Console.WriteLine(); Animal[] animals = new Animal[] { new Cat("Pepi", 3, "female"), new Frog("Kurmit", 10, "male"), new Dog("Sharo", 5, "male"), new Cat("Jorko", 6, "male"), new Cat("Tom",12,"male") }; Console.WriteLine(Animal.GetAvaregeAge(animals)); var ages = (from animal in animals where animal.GetType() == typeof(Cat) select animal.age).Average(); Console.WriteLine(ages); }
static void Main() { var animals = new Animal[] { new Dog("Belcho", 13, Gender.Male), new Cat("Buzlio", 9, Gender.Female), new Frog("Fizz", 1, Gender.Male), new Kitten("Darcy", 5), new Tomcat("Tommy", 3) }; animals.ToList().ForEach(Console.WriteLine); Console.WriteLine(); var animalsBygroups = animals.GroupBy(GetAnimalKind, (g, a) => new { kind = g, averagAge = a.Average(animal => animal.Age) }); foreach (var animalGroup in animalsBygroups) { Console.WriteLine("The average age of {0}s is {1:f2} years.", animalGroup.kind, animalGroup.averagAge); } }
static void Main(string[] args) { var noisyCreatures = new ISound[] { new Dog(5, "Rex", "male"), new Frog(49, "Kermit", "male"), new Dog(15, "Stella", "female"), new Tomcat(8, "Hoho"), new Kitten(5, "Cleopatra"), new Dog(3, "Ceaser", "male"), new Kitten(6, "Joro"), new Tomcat(7, "Borko"), new Dog(13,"Sharo", "male") }; var animals = new Animal[] { new Dog(5, "Rex", "male"), new Frog(49, "Kermit", "male"), new Dog(15, "Stella", "female"), new Tomcat(8, "Hoho"), new Kitten(5, "Cleopatra"), new Dog(3, "Ceaser", "male"), new Kitten(6, "Joro"), new Tomcat(7, "Borko"), new Dog(13,"Sharo", "male") }; foreach (var noisyCreature in noisyCreatures) { noisyCreature.MakeSound(); } var averageAges = GetAverageAges(animals); foreach (var item in averageAges) { Console.WriteLine(item.Item1 + " " + item.Item2); } }
static void Main() { Animal[] animals = new Animal[10] { new Dog("Sharo",12,true), new Dog("Rex",10,true), new Cat("Puffy",8,false), new Cat("Tom",5,true), new Frog("Jaba",3,true), new Frog("Princes-Frog",22,false), new Tomcat("Garfield",13), new Tomcat("Gaspar",9), new Kitten("Shely",7), new Kitten("Cherry",9) }; //foreach (var item in animals) //{ // item.ProduceSound(); //} //animals.ToList().ForEach(Console.WriteLine); //Console.WriteLine(); animals .GroupBy(animal => animal.GetType().Name) .Select(group => new { AnimalName = group.Key, AverageAge = group.Average(a => a.Age) } ) .OrderBy(group => group.AverageAge) .ToList() .ForEach(group => Console.WriteLine("{0}'s average age is: {1}",group.AnimalName,group.AverageAge)); }
public static double AverageAge(Animal[] list) { return list.Average(animal => animal.Age); }
static void Main(string[] args) { Dog dog = new Dog("Pesho", 3, Gender.Male); Console.WriteLine(dog); dog.FetchStick(); Console.WriteLine(); Frog frog = new Frog("Peter", 33, Gender.Male); Console.WriteLine(frog); frog.Jump(); Console.WriteLine(); Kitten kitty = new Kitten("Ioio", 1); Console.WriteLine(kitty); kitty.Cry(); Console.WriteLine(); Tomcat tomcat = new Tomcat("Gogo", 12); Console.WriteLine(tomcat); tomcat.Piss(); Console.WriteLine(); // create an array of Animals Animal[] animals = new Animal[] { new Dog("Ares",3, Gender.Male), new Frog("Mitko", 1, Gender.Female), new Kitten("Mac", 1), new Tomcat("Cotka",2), new Dog("Saires", 4, Gender.Male), new Frog("Stilian", 1, Gender.Female), new Kitten("Ioio", 2), new Tomcat("Antony",6), new Dog("Roy",2, Gender.Male), new Frog("Angelina", 6, Gender.Female), new Kitten("Alex", 1), new Tomcat("Mecjo",4), }; // calculate the aveage age of each animal and print them var averageAge = from an in animals group an by new { GroupName = an.GetType().Name } into gr select new { GroupName = gr.Key.GroupName, AvarageAge = gr.Average(an => an.Age) }; foreach (var animal in averageAge) { Console.WriteLine(String.Format("Group: {0}, AvarageAge: {1:0.00}.", animal.GroupName, animal.AvarageAge)); } }
static IEnumerable AverageAgeByKind(Animal[] arrAnimals) { return arrAnimals.GroupBy(a => a.GetType().Name).Select(k => new { AnimalType = k.Key, AverageAge = k.Average(a => a.Age)}); }
public static double AverageAge(Animal[] animalsArr) { return animalsArr.Average(x => x.Age); }
public void attack(Animal animal) { }
public static double AverageAge(Animal[] animals) { return animals.Average(animal => animal.Age); }