static void Main() { string[] names = new string[] { "CALVIN", "CALYPSO", "CALZE", "FANCY", "FANNY", "FANTASIA", "MADDIE", "MADEIRA", "MADISON", "MADMAX", "FROGY" }; Random rand = new Random(); Animal[] cats = new Cat[12]; for (int i = 0; i < cats.Length; i++) { string name = names[rand.Next(0, 10)]; uint age = (uint)rand.Next(1, 10); SexType sex = (SexType)rand.Next(1, 3); Cat cat = new Cat(name, age); cat.Sex = sex; cats[i] = cat; } cats[10] = new Kitten("Kity", 1); cats[11] = new Tomcat("Big", 3); uint catsAverageAge = Animal.CalculateAverageAge(cats); Console.WriteLine("Cats average age: {0}" , catsAverageAge); Animal[] frogs = new Frog[3]; for (int i = 0; i < frogs.Length; i++) { string name = names[rand.Next(8, 11)]; uint age = (uint)rand.Next(1, 5); SexType sex = (SexType)rand.Next(1, 3); Frog frog = new Frog(name, age); frog.Sex = sex; frogs[i] = frog; } Console.WriteLine("Frogs make sound"); foreach (var frog in frogs) { frog.MakeSound(); Console.WriteLine("Wait"); } }
/*3. Create a hierarchy Dog, Frog, Cat, Kitten, Tomcat and define useful constructors and methods. * Dogs, frogs and cats are Animals. * All animals can produce sound (specified by the ISound interface). * Kittens and tomcats are cats. * All animals are described by age, name and sex. * Kittens can be only female and tomcats can be only male. * Each animal produces a specific sound. * 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() { //create different animals Cat cat = new Cat("Catty", 3, Sex.Female); Dog dog = new Dog("Silvestar", 5, Sex.Male); Frog froggy = new Frog("Froggy", 1, Sex.Female); Kitten kitten = new Kitten("Kitty", 5); Kitten kittenCat = new Kitten("Kety", 3); Tomcat tomcat = new Tomcat("Tom", 6); //produces a specific sound Console.Write("Cat make sound: "); cat.MakeSound(); Console.Write("Dog make sound: "); dog.MakeSound(); Console.Write("Frog make sound: "); froggy.MakeSound(); Console.WriteLine(); //Create arrays of different kinds of animals Animal[] animals = { cat, dog, froggy, kitten, tomcat, kittenCat}; foreach (var ani in animals) { Console.WriteLine(ani.ToString()); } //calculate the average age of all animals double agerageAgeOfAnimals = Animal.CalculateAverageAgeOfAnimals(animals); Console.WriteLine(); Console.WriteLine("Average age of all animals is {0:F3}", agerageAgeOfAnimals); //calculate the average age of each kind of animal Console.WriteLine("Average of each kind is: "); Animal.CalculateAverageAgeOfKind(animals); }
static void Main() { Kitten maca = new Kitten("Maca", 1); Cat tomas = new Cat("Tomas", 5, Gender.male); Dog sharo = new Dog("Sharo", 5, Gender.male); Frog kroko = new Frog("Kroko", 3, Gender.male); Tomcat herkules = new Tomcat("Herkules", 20); List<Kitten> kittens = new List<Kitten>() { maca, new Kitten("Pisi", 2), new Kitten("Lapa", 1) }; Cat[] cats = new Cat[] { tomas, new Cat("Gosho", 4, Gender.male), new Cat("Sara", 8, Gender.female), new Cat("Pesho", 11, Gender.male) }; Dog[] dogs = new Dog[] { sharo, new Dog("Chocho", 16, Gender.male), new Dog("Lucky", 8, Gender.female), new Dog("Sara", 10, Gender.female) }; List<Frog> frogs = new List<Frog>() { kroko, new Frog("Kroki", 2, Gender.female) }; Tomcat[] tomcats = new Tomcat[] { herkules, new Tomcat("Paul", 11), new Tomcat("Richard", 9) }; Console.WriteLine("Average ages of animals in collections:"); Console.WriteLine(string.Format("Tomcats: {0:F2}",Animal.AvgAge(tomcats))); Console.WriteLine(string.Format("Kittens: {0:F2}", Animal.AvgAge(kittens))); Console.WriteLine(string.Format("Cats: {0:F2}",Animal.AvgAge(cats))); Console.WriteLine(string.Format("Dogs: {0:F2}", Animal.AvgAge(dogs))); Console.WriteLine(string.Format("Frogs: {0:F2}", Animal.AvgAge(frogs))); Console.WriteLine("\nSounds the animals make:"); Console.Write("Kitten: "); maca.MakeSound(); Console.Write("Tomcat: "); herkules.MakeSound(); Console.Write("Cat: "); tomas.MakeSound(); Console.Write("Dog: "); sharo.MakeSound(); Console.Write("Frog: "); kroko.MakeSound(); }