static void Main() { Animal jaba = new Frog("baba jaba", 1, Genders.Female); Animal kekerica = new Frog("kekerica", 3, Genders.Female); Animal sharo = new Dog("sharo", 3, Genders.Male); Animal sara = new Dog("sara", 2, Genders.Female); Animal oldy = new Dog("oldy", 12, Genders.Male); Animal puhi = new Kitten("puhi", 2); Animal tommy = new Tomcat("tommy", 4); Animal mouseKiller = new Cat("mousy", 5, Genders.Male); List<Animal> animals = new List<Animal>() { jaba, kekerica, sharo, sara, puhi, tommy, mouseKiller, oldy }; var groupedAnimals = from animal in animals group animal by (animal is Cat) ? typeof(Cat) : animal.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); } puhi.ProduceSound(); oldy.ProduceSound(); jaba.ProduceSound(); }
static void Main(string[] args) { // create a few dogs Dog[] dogs = new Dog[] { new Dog("Gosho", 4, Sex.Male), new Dog("Rex", 5, Sex.Male), new Dog("Linda", 3, Sex.Female), new Dog("Ivan", 6, Sex.Male), }; // create a few frogs Frog[] frogs = new Frog[] { new Frog("Misho", 3, Sex.Male), new Frog("Tisho", 6, Sex.Male), new Frog("Pesho", 4, Sex.Male), }; // create a few cats Cat[] cats = new Cat[] { new Cat("Pisa", 7, Sex.Female), new Kitten("Lubov", 3), new Tomcat("Tom", 10), new Cat("Vesko", 7, Sex.Male), }; // calculate the average age of all dogs Console.WriteLine("Dogs' average age is {0}", Animal.CalculateAverageAge(dogs)); // calculate the average age of all cats Console.WriteLine("Cats' average age is {0}", Animal.CalculateAverageAge(cats)); // calculate the average age of all frogs Console.WriteLine("Frogs' average age is {0}", Animal.CalculateAverageAge(frogs)); }
static void Main() { //Create list of Animals //Tomcat and Kiten have constructor with only two parameters, third is constant Animal[] animals = new Animal[] { new Dog("Sharo",4,Sex.Male), new Cat("Kitty",3,Sex.Female), new Frog("Kyrmit",2,Sex.Male), new Kitten("Swetty",5), new Tomcat("Garfild",7), new Dog("Rex",3,Sex.Male), new Cat("Lussi", 6,Sex.Female), new Tomcat("SomeName",4)}; Animal.AveargeAge(animals); //Print average age for every kind of animal in list animals[0].Sound("bayyyyyyyyyy"); Cat myCat = new Cat("Garfild", 3, Sex.Female); myCat.Sound(); Frog myFrog = new Frog("Kyrmit",3, Sex.Male); myFrog.Sound(); }
static void Main(string[] args) { Dictionary <string, Cat> cats = new Dictionary <string, Cat>(); Dictionary <string, Dog> dogs = new Dictionary <string, Dog>(); Dictionary <string, Snake> snakes = new Dictionary <string, Snake>(); string input = Console.ReadLine(); while (input != "I'm your Huckleberry") { string[] inputTokens = input.Split(' '); string animalOrCommand = inputTokens[0]; string name = inputTokens[1]; if (animalOrCommand == "talk") { if (dogs.ContainsKey(name)) { dogs[name].ProduceSound(); } else if (cats.ContainsKey(name)) { cats[name].ProduceSound(); } else if (snakes.ContainsKey(name)) { snakes[name].ProduceSound(); } } else { if (animalOrCommand == "Dog") { Dog currentDog = Dog.Parse(input); dogs[name] = currentDog; } else if (animalOrCommand == "Cat") { Cat currentCat = Cat.Parse(input); cats[name] = currentCat; } else if (animalOrCommand == "Snake") { Snake currentSnake = Snake.Parse(input); snakes[name] = currentSnake; } } input = Console.ReadLine(); } foreach (var dog in dogs.Values) { Console.WriteLine($"Dog: {dog.Name}, Age: {dog.Age}, Number Of Legs: {dog.NumberOfLegs}"); } foreach (var cat in cats.Values) { Console.WriteLine($"Cat: {cat.Name}, Age: {cat.Age}, IQ: {cat.IntelligenceQuotient}"); } foreach (var snake in snakes.Values) { Console.WriteLine($"Snake: {snake.Name}, Age: {snake.Age}, Cruelty: {snake.CrueltyCoefficient}"); } }