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(string[] args) { Dog[] dogs = new Dog[] { new Dog("Balkan", 4, Gender.Male), new Dog("Majlo", 3, Gender.Male), new Dog("Strashimirka", 7, Gender.Female) }; Frog[] frogs = new Frog[] { new Frog("Kermit", 2, Gender.Male), new Frog("Zhabcho", 4, Gender.Male) }; Kitten[] kittens = new Kitten[] { new Kitten("Kat", 6), new Kitten("Sweety", 0) }; Tomcat[] tomcats = new Tomcat[] { new Tomcat("Lucky", 7), new Tomcat("Tom", 6) }; var dogsAverageAge = dogs.Average(dog => dog.Age); Console.WriteLine(dogsAverageAge); List <Animal> animals = new List <Animal> { new Dog("Popi", 4, Gender.Male), new Cat("Maca", 3, Gender.Female), new Frog("Keri", 5, Gender.Female), new Kitten("Mici", 2), new Kitten("Maca", 3), new Tomcat("Tom", 4), new Tomcat("Misho", 3) }; 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}.", animalGroup.kind, animalGroup.averagAge); } }
static void Main(string[] args) { Animal animal = new Cat("Mimi",4,Gender.female); Animal animal1 = new Dog("Sharo", 1, Gender.female); Animal animal2 = new Frog("Kikirica", 5, Gender.female); Animal animal3 = new Kitten("Mimi", 7); Animal animal4 = new Tomcat("Mimi", 5); Animal animal5 = new Frog("Mimi", 4, Gender.female); Animal[] zoo = new Animal[]{ animal,animal1,animal2,animal3,animal4,animal }; var avarageAge = zoo.Average(a=>a.Age); Console.WriteLine("{0:0.00}",avarageAge); }
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() { //some tests Dog d = new Dog("Joe", 4, Sex.male, FurColour.black); d.MakeSound(); Console.WriteLine(d.GetSpecies()); d.Bite(); Console.WriteLine("----------------------"); Kitten k = new Kitten("Kittie", 5, 10, 10); k.MakeSound(); Console.WriteLine(k.GetSpecies()); k.SayAge(); k.Fight(); Console.WriteLine("----------------------"); Tomcat t = new Tomcat("Tommie", 3, 15, 25); Console.WriteLine(t.GetSpecies()); t.SayAge(); t.Fight(); t.MakeSound(); Console.WriteLine("----------------------"); Frog f = new Frog("Froggy", 1, Sex.male, 3); Console.WriteLine(f.GetSpecies()); Console.WriteLine("Froggy has {0} green spots", f.greenSpots); f.SayAge(); f.MakeSound(); Console.WriteLine("----------------------"); //instantiate arrays of dogs and tomcats Dog[] dogs = InstantiateDogs(); Tomcat[] tomcats = InstantiateTomcats(); //calculate average age Console.WriteLine("Average age of dogs: {0}", Animal.CalcAverageAge(dogs)); Console.WriteLine("Average age of tomcats: {0}", Animal.CalcAverageAge(tomcats)); }
static void Main() { Dog rex = new Dog("Rex", 3, "male"); Dog max = new Dog("Max", 1, "male"); Dog sara = new Dog("Sara", 6, "female"); Frog frog = new Frog("Frog", 7, "male"); Frog jaba = new Frog("Jaba", 4, "female"); Frog kermit = new Frog("Kermit", 10, "male"); Cat kitty = new Kitten("Kitty", 2); Cat snowball = new Tomcat("Snowball", 5); List <Animal> animals = new List <Animal>() { rex, max, sara, frog, jaba, kermit, kitty, snowball }; var groupedAnimals = from animal in animals group animal by animal.GetType().Name into g select new { GroupName = g.Key, AverageAge = g.ToList().Average(an => an.Age) }; foreach (var animal in groupedAnimals) { Console.WriteLine("{0}s - average age: {1:N2}", animal.GroupName, animal.AverageAge); } rex.ProduceSound(); kermit.ProduceSound(); snowball.ProduceSound(); }
public static void Main() { Cat[] cats = new Cat[] { new Cat("Pesho", 2, Sex.male), new Cat("Rijko", 6, Sex.male), new Cat("Bella", 3, Sex.female), new Cat("Maca", 1, Sex.female), new Cat("Mara", 8, Sex.female), }; Console.WriteLine("Cats:"); foreach (var cat in cats) { Console.WriteLine(cat); } Console.WriteLine("\nAverage age of all cats is: {0}\n", Animal.CalculateAverageAge(cats)); Console.WriteLine("Dogs:"); Dog[] dogs = new Dog[] { new Dog("Roby", 4, Sex.male), new Dog("Charlie", 5, Sex.male), new Dog("Molly", 3, Sex.female), new Dog("Abby", 1, Sex.female), new Dog("Sara", 10, Sex.female), }; foreach (var dog in dogs) { Console.WriteLine(dog); } Console.WriteLine("\nAverage age of all dogs is: {0}\n", Animal.CalculateAverageAge(dogs)); Kitten[] kittens = new Kitten[] { new Kitten("Bella", 3), new Kitten("Maca", 1), new Kitten("Mara", 8), }; Tomcat[] tomcats = new Tomcat[] { new Tomcat("Pesho", 2), new Tomcat("Rijko", 6), }; var allAnimals = new List <Animal>(); allAnimals.AddRange(tomcats); allAnimals.AddRange(kittens); allAnimals.AddRange(dogs); Console.WriteLine("Average age of all animals is: " + Animal.CalculateAverageAge(allAnimals)); var dogsFromAll = from animal in allAnimals where animal.GetType() == typeof(Dog) select animal; Console.WriteLine("\nAverage age of all dogs from a list of all animals is :" + Animal.CalculateAverageAge(dogsFromAll)); }
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); }