static void Main(string[] args) { Animal[] animals = new Animal[] { new Tomcat("Pesho", 2), new Kitten("Mimi", 4), new Dog("Sharo", 3, Sex.Male), new Frog("Kermit", 5, Sex.Male) }; Cat[] cats = new Cat[] { new Kitten("Mimi", 2), new Tomcat("Gosho", 6) }; Console.WriteLine("# Animals"); foreach (Animal animal in animals) { Console.WriteLine(animal); } Console.WriteLine("# Produce sound"); foreach (ISound animal in animals) { Console.WriteLine(animal.ProduceSound()); } Console.WriteLine("# Average"); Console.WriteLine(animals.Average(animal => animal.Age)); Console.WriteLine(cats.Average(cat => cat.Age)); Console.ReadKey(); }
static void Main() { Animal[] animals = new Animal[] { new Tomcat("Tom", 2), // creating new animals new Kitten("Toodles Galore", 4), new Dog("Kudjo", 3, Sex.Male), new Frog("Kermit", 5, Sex.Male) }; Cat[] cats = new Cat[] { new Kitten("Toodles Galore", 2), new Tomcat("Tom", 6) }; Console.WriteLine("# Animals"); foreach (Animal animal in animals) { Console.WriteLine(animal); } Console.WriteLine("\n# Sounds"); foreach (ISound animal in animals) { Console.WriteLine(animal.ProduceSound()); } Console.WriteLine("\n# Average age"); Console.WriteLine(animals.Average(animal => animal.Age) + " years"); Console.WriteLine(cats.Average(cat => cat.Age) + " years"); }
static void Main(string[] args) { Frog[] frogs = { new Frog(2, "tryn", 'f'), new Frog(8, "mryn", 'f'), new Frog(6, "zwyn", 'f'), new Frog(7, "lqlq", 'f'), }; Dog[] dogs = { new Dog(4, "Doggy", 'f'), new Dog(7, "charly", 'f'), new Dog(8, "Tot", 'f'), new Dog(10, "Sparki", 'f') }; Cat[] cats = { new Kitten(3, "Tomas"), new TomCat(5, "Age"), new Kitten(2, "Mila"), new TomCat(1, "Miss") }; dogs[2].IdentifyAnimal(); cats[1].IdentifyAnimal(); frogs[3].ProduceSound(); Console.WriteLine("Average age of dogs " + Animal.Average(dogs)); Console.WriteLine("Average age of frogs " + Animal.Average(frogs)); Console.WriteLine("Average age of cats " + Animal.Average(cats)); }
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() { 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 double AverageAge(Animal[] list) { return list.Average(animal => animal.Age); }
public static double AverageAge(Animal[] animals) { return animals.Average(animal => animal.Age); }
public static double AverageAge(Animal[] animalsArr) { return animalsArr.Average(x => x.Age); }
public static double AverageAge(Animal[] animals) { return animals.Average(x => x.age); }
//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). static void Main() { Kitten[] kittens = { new Kitten(3, "Macka", Sex.female), new Kitten(6, "Pisanka", Sex.female), new Kitten(5, "Kotanka", Sex.female), }; Tomcat[] tomcats = { new Tomcat(5, "Kotaran", Sex.male), new Tomcat(1, "Pisan", Sex.male), new Tomcat(3, "Macan", Sex.male), }; Dog[] dogs = { new Dog(3, "Sharo", Sex.male), new Dog(5, "Rex", Sex.male), new Dog(3, "Lora", Sex.female), new Dog(3, "Casper", Sex.male), }; Frog[] frogs = { new Frog(1, "Jabok", Sex.male), new Frog(1, "Jaborana", Sex.female), new Frog(1, "Jabo", Sex.other), }; Console.WriteLine("Kittens:"); foreach (Kitten kitten in kittens) { Console.Write(kitten); Console.WriteLine(new string('-', 40)); } Console.WriteLine(new string('=', 40)); Console.WriteLine("Tomcats:"); foreach (Tomcat tomcat in tomcats) { Console.Write(tomcat); Console.WriteLine(new string('-', 40)); } Console.WriteLine(new string('=', 40)); Console.WriteLine("Dogs:"); foreach (Dog dog in dogs) { Console.Write(dog); Console.WriteLine(new string('-', 40)); } Console.WriteLine(new string('=', 40)); Console.WriteLine("Frogs:"); foreach (Frog frog in frogs) { Console.Write(frog); Console.WriteLine(new string('-', 40)); } Console.WriteLine(new string('=', 40)); Console.WriteLine("Kittens average age = {0:0.00}", Animal.Average(kittens)); Console.WriteLine("Tomcats average age = {0:0.00}", Animal.Average(tomcats)); Console.WriteLine("Dogs average age = {0:0.00}", Animal.Average(dogs)); Console.WriteLine("Frogs average age = {0:0.00}", Animal.Average(frogs)); }
public static double AvarageAge(Animal[] animals) { var query = animals.Average(x => x.Age); return query; }