コード例 #1
0
ファイル: Test.cs プロジェクト: iliantova/Telerik-Homework
        static void Main()
        {
            Kitten[] newKitten = new Kitten[5];
            newKitten[0] = new Kitten("Lapichka", 5);
            newKitten[1] = new Kitten("Timiti", 1);
            newKitten[2] = new Kitten("Glezla", 3);
            newKitten[3] = new Kitten("Kotka", 2);
            newKitten[4] = new Kitten("Kitty", 10);

            Frog[] FrogsList = new Frog[3];
            FrogsList[0] = new Frog("Kvak", 1, false);
            FrogsList[1] = new Frog("Skoklio", 1, true);
            FrogsList[2] = new Frog("Krasaveca", 3, false);

            Dog[] newDog = new Dog[4];
            newDog[0] = new Dog("Rexy", 3, false);
            newDog[1] = new Dog("Tot", 4, false);
            newDog[2] = new Dog("Geya", 7, true);
            newDog[3] = new Dog("Ayay", 2, true);

            Console.WriteLine("Average age using standart calc: {0}", Animals.CalculateAverageLINQ(newKitten));
            Console.WriteLine("Average age using LINQ calc: {0}", Animals.CalculateAverageLINQ(newDog));

            Dog Sharo = new Dog("Sharo", 5, false);
            Console.WriteLine( "Sharo sais: " );
            Sharo.MakeSound();
        }
コード例 #2
0
        static void Main()
        {
            Dog[] dogs = new Dog[]
             {
                 new Dog("Jaro", Animal.GenderValue.male,7),
                 new Dog("Sharo", Animal.GenderValue.female, 3),
                 new Dog("Doge", Animal.GenderValue.female,5),
                 new Dog("Estel",Animal.GenderValue.male, 10)
             };

             Frog[] frogs = new Frog[]
             {
                 new Frog("Kikirica",Animal.GenderValue.male, 13),
                 new Frog("Jaba", Animal.GenderValue.female,15),
                 new Frog("Froggy",Animal.GenderValue.male, 5),
                 new Frog("Nikoleta Lozanova",Animal.GenderValue.female, 10)
             };

             Cat[] cats = new Cat[]
             {
                 new Cat("Street Excellent",Animal.GenderValue.female, 3),
                 new Cat("Home Excellent", Animal.GenderValue.female,5),
                 new Cat("Persiiko", Animal.GenderValue.female,1),
                 new Cat("Garfield",Animal.GenderValue.female,7)
             };

             Kitten[] kittens = new Kitten[]
             {
                 new Kitten("Malcho", 1),
                 new Kitten("Palcho", 2),
                 new Kitten("Shalco", 1)

             };

             TomCat[] tomcats = new TomCat[]
             {
                 new TomCat("Kotio", 5),
                 new TomCat("Gosho", 4),
                 new TomCat("Pesho", 8)
             };

             double dogsAverageAge = Animal.AverageAge(dogs);
             double frogsAverageAge = Animal.AverageAge(frogs);
             double catsAverageAge = Animal.AverageAge(cats);
             double kittensAverageAge = Animal.AverageAge(kittens);
             double tomcatsAverageAge = Animal.AverageAge(tomcats);

             Console.WriteLine("Average age of the dogs: {0:F2}", dogsAverageAge);
             Console.WriteLine("Average age of the frogs: {0:F2}", frogsAverageAge);
             Console.WriteLine("Average age of the cats: {0:F2}", catsAverageAge);
             Console.WriteLine("Average age of the kittens: {0:F2}", kittensAverageAge);
             Console.WriteLine("Average age of the tomcats: {0:F2}", tomcatsAverageAge);

             Console.WriteLine(new string('-', 10));

             Console.WriteLine("Sounds: ");
             cats[0].MakeSound();
             dogs[1].MakeSound();
             frogs[2].MakeSound();
        }
コード例 #3
0
        static void Main()
        {
            //Create arrays of different kinds of animals
            Animal[] animals = new Animal[5];
            animals[0] = new Kitten("Kitty", 1);
            animals[1] = new Tomcat("Tom", 5);
            animals[2] = new Frog("Froggy");
            animals[3] = new Frog("granny Frog", 6, Sex.Female);
            animals[4] = new Dog("Balkan", 4, Sex.Male);

            //test sound
            Console.WriteLine("Test sounds:");
            Console.WriteLine();

            foreach (var item in animals)
            {
                Console.WriteLine(item);
                Console.WriteLine(item.ProduceSound());
            }
            Console.WriteLine();

            //and calculate the average age of each kind of animal using a static method (you may use LINQ).

            var animalsAverageAgeByType = animals.GroupBy(a => a.GetType().Name)
                .Select(g => new { Kind = g.Key, AverageAge = g.Average(item => item.Age) });

            Console.WriteLine("Average age of each kind of animal using a static method");

            foreach (var item in animalsAverageAgeByType)
            {
                Console.WriteLine(item);
            }

            Console.WriteLine();

            var animalsAverageAgeByTypeLINQ =
                from a in animals
                group a by a.GetType().Name into k
                select new { Kind = k.Key, AverageAge = k.Average(a => a.Age) };

            Console.WriteLine("Average age of each kind of animal using LINQ");

            foreach (var item in animalsAverageAgeByTypeLINQ)
            {
                Console.WriteLine(item);
            }

            Console.WriteLine();
        }
コード例 #4
0
        static void Main()
        {
            Kitten penka = new Kitten("Penka", 1);
            Tomcat gosho = new Tomcat("Gosho", 5);
            Frog stamat = new Frog("Stamat", 1, AnimalGender.Male);
            Dog sharo = new Dog("Sharo", 15, AnimalGender.Male);
            Cat pisana = new Cat("Pisana", 3, AnimalGender.Female);

            List<Animal> animals = new List<Animal>() { penka, gosho, stamat, sharo, pisana };

            foreach (var animal in animals)
            {
                Console.WriteLine(animal.ToString());
            }
            Console.WriteLine(Animal.CalculateAverageAge(animals));
        }
コード例 #5
0
        static void Main(string[] args)
        {
            Dog[] dogs = new Dog[]
            {
                new Dog("Sharo",5,'F'),
                new Dog("Mimi",3,'M'),
                new Dog("Becks",3.4,'F')
            };
            Frog[] frogs = new Frog[]
            {
                new Frog("BiBI", 13, 'F'),
                new Frog("Katya", 15, 'M'),
                new Frog("Kolio", 5, 'M'),
                new Frog("Niki", 10, 'M')
            };

            Cat[] cats = new Cat[]
            {
                new Cat("Ass", 3, 'F'),
                new Cat("Hand", 5, 'F'),
                new Cat("Head", 1, 'F'),
                new Cat("Leg", 7, 'M')
            };

            Kitten[] kittens = new Kitten[]
            {
                new Kitten("Malcho", 1),
                new Kitten("Palcho", 2),
                new Kitten("Shalco", 1)

            };

            Tomcat[] tomcats = new Tomcat[]
            {
                new Tomcat("Gosho", 5),
                new Tomcat("Mitko", 4),
                new Tomcat("Pesho", 8)
            };
            Console.WriteLine("Average age of the dogs: {0:F2}", Animal.AverageAge(dogs));
            Console.WriteLine("Average age of the frogs: {0:F2}",Animal.AverageAge(frogs));
            Console.WriteLine("Average age of the cats: {0:F2}",Animal.AverageAge(cats));
            Console.WriteLine("Average age of the kittens: {0:F2}", Animal.AverageAge(kittens));
            Console.WriteLine("Average age of the tomcats: {0:F2}",Animal.AverageAge(tomcats));
        }
コード例 #6
0
        static void Main()
        {
            Animal[] animals = new Animal[]
            {
                new Kitten(GetRandomName(), GetRandomAge()),
                new Tomcat(GetRandomName(), GetRandomAge()),
                new Dog(GetRandomName(), GetRandomAge(), Gender.Male),
                new Cat(GetRandomName(), GetRandomAge(), Gender.Male),
                new Frog(GetRandomName(), GetRandomAge(), Gender.Female),
                new Frog(GetRandomName(), GetRandomAge(), Gender.Male)
            };

            Console.WriteLine("Some animals:");
            foreach (Animal animal in animals)
            {
                Console.WriteLine(animal);
            }
            Console.WriteLine();

            var kittens = new List<Kitten>();
            for (int i = 0; i < 5; i++)
            {
                Kitten currerntKitten
                    = new Kitten(GetRandomName(), GetRandomAge());
                kittens.Add(currerntKitten);
            }

            double avrKitten = Kitten.CalculateAverageAge(kittens);
            Console.WriteLine("Kitten average age: " + avrKitten);

            var dogs = new List<Dog>();
            for (int i = 0; i < 5; i++)
            {
                Dog currerntDog
                    = new Dog(GetRandomName(), GetRandomAge(), Gender.Male);
                dogs.Add(currerntDog);
            }

            double avrDog = Dog.CalculateAverageAge(dogs);
            Console.WriteLine("Dog average age: " + avrDog);
        }
コード例 #7
0
        static void Main(string[] args)
        {
            Kitten kit = new Kitten("Kitty", 2);
            Tomcat tom = new Tomcat("Tom", 3);
            Frog leggy = new Frog("Leggy", 6, Gender.Male);
            Dog rex = new Dog("Rex", 4, Gender.Male);
            Cat snow = new Cat("Snow", 5, Gender.Female);

            List<Animal> animals = new List<Animal>() { kit, tom, leggy, rex, snow };
            IntroduceAnimal(animals);

            Kitten[] kittens = new Kitten[] { kit, new Kitten("Lilly", 1), new Kitten("Smurf", 2) };
            Tomcat[] tomcats = new Tomcat[] { tom, new Tomcat("Joefrrey", 3), new Tomcat("Steve", 2), new Tomcat("Paul", 1) };
            Frog[] frogs = new Frog[] { leggy, new Frog("Squirrle", 12, Gender.Female), new Frog("Kurt", 1, Gender.Male) };
            Dog[] dogs = new Dog[] { rex, new Dog("Fluffy", 4, Gender.Female), new Dog("Olde Doge", 12, Gender.Male) };
            Cat[] cats = new Cat[] { snow };

            Console.WriteLine("Kittens: {0:F2}", Animal.AverageAge(kittens));
            Console.WriteLine("Tomcats: {0:F2}", Animal.AverageAge(tomcats));
            Console.WriteLine("Frogs: {0:F2}", Animal.AverageAge(frogs));
            Console.WriteLine("Dogs: {0:F2}", Animal.AverageAge(dogs));
            Console.WriteLine("Cats: {0:F2}", Animal.AverageAge(cats));
        }
コード例 #8
0
        /*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);
        }
コード例 #9
0
        public static void Main()
        {
            Cat[] cats = new Cat[]
            {
                new Cat("Jonny", 1, Sex.male),
                new Cat("Hue", 2, Sex.male),
                new Cat("Maia", 2, Sex.female),
            };

            Dog[] dogs = new Dog[]
            {
                new Dog("Ivo", 5, Sex.male),
                new Dog("Pesho", 4, Sex.male),
                new Dog("Maria", 8, Sex.female),
            };

            Frog[] frogs = new Frog[]
            {
                new Frog("Gosho", 4, Sex.male),
                new Frog("Ivan", 5, Sex.female),
                new Frog("Zahari", 3, Sex.male)
            };

            Kitten[] kittens = new Kitten[]
            {
                new Kitten("Mihaela", 5),
                new Kitten("Ivona", 3),
                new Kitten("Petra", 2)
            };

            Tomcat[] tomcats = new Tomcat[]
            {
                new Tomcat("Rado", 3),
                new Tomcat("Radoi", 3),
                new Tomcat("Frodo", 2)
            };

            Console.WriteLine("Cats average age is: ");
            Console.WriteLine("{0:F2}", Cat.AverageAge(cats));
            Console.WriteLine("The cat say: {0}", cats[0].Sound());
            Console.WriteLine();

            Console.WriteLine("Cats average age is: ");
            Console.WriteLine("{0:F2}", Dog.AverageAge(dogs));
            Console.WriteLine("The cat say: {0}", dogs[0].Sound());
            Console.WriteLine();

            Console.WriteLine("Cats average age is: ");
            Console.WriteLine("{0:F2}", Frog.AverageAge(frogs));
            Console.WriteLine("The cat say: {0}", frogs[0].Sound());
            Console.WriteLine();

            Console.WriteLine("Cats average age is: ");
            Console.WriteLine("{0:F2}", Kitten.AverageAge(kittens));
            Console.WriteLine("The cat say: {0}", kittens[0].Sound());
            Console.WriteLine();

            Console.WriteLine("Cats average age is: ");
            Console.WriteLine("{0:F2}", Tomcat.AverageAge(tomcats));
            Console.WriteLine("The cat say: {0}", tomcats[0].Sound());
        }
コード例 #10
0
ファイル: Zoo.cs プロジェクト: g-yonchev/Telerik-Academy
 public static void Main()
 {
     Dog firstDog = new Dog();
     string isSleeping = firstDog.Sleep(true);
     Console.WriteLine("The dog is sleeping: {0}", isSleeping);
 }
コード例 #11
0
        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();
        }
コード例 #12
0
        public static void Main()
        {
            Cat[] cats = new Cat[]
            {
                new Cat("Jonny", 1, Sex.male),
                new Cat("Hue", 2, Sex.male),
                new Cat("Maia", 2, Sex.female),
            };

            Dog[] dogs = new Dog[]
            {
                new Dog("Ivo", 5, Sex.male),
                new Dog("Pesho", 4, Sex.male),
                new Dog("Maria", 8, Sex.female),
            };

            Frog[] frogs = new Frog[]
            {
                new Frog("Gosho", 4, Sex.male),
                new Frog("Ivan", 5, Sex.female),
                new Frog("Zahari", 3, Sex.male)
            };

            Kitten[] kittens = new Kitten[]
            {
                new Kitten("Mihaela", 5),
                new Kitten("Ivona", 3),
                new Kitten("Petra", 2)
            };

            Tomcat[] tomcats = new Tomcat[]
            {
                new Tomcat("Rado", 3),
                new Tomcat("Radoi", 3),
                new Tomcat("Frodo", 2)
            };

            Console.WriteLine("Cats average age is: ");
            Console.WriteLine("{0:F2}", Cat.AverageAge(cats));
            Console.WriteLine("The cat say: {0}", cats[0].Sound());
            Console.WriteLine();

            Console.WriteLine("Cats average age is: ");
            Console.WriteLine("{0:F2}", Dog.AverageAge(dogs));
            Console.WriteLine("The cat say: {0}", dogs[0].Sound());
            Console.WriteLine();

            Console.WriteLine("Cats average age is: ");
            Console.WriteLine("{0:F2}", Frog.AverageAge(frogs));
            Console.WriteLine("The cat say: {0}", frogs[0].Sound());
            Console.WriteLine();

            Console.WriteLine("Cats average age is: ");
            Console.WriteLine("{0:F2}", Kitten.AverageAge(kittens));
            Console.WriteLine("The cat say: {0}", kittens[0].Sound());
            Console.WriteLine();

            Console.WriteLine("Cats average age is: ");
            Console.WriteLine("{0:F2}", Tomcat.AverageAge(tomcats));
            Console.WriteLine("The cat say: {0}", tomcats[0].Sound());
        }
コード例 #13
0
        /*
         * 3. Animal hierarchy
         *
         *  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()
        {
            var arrWithCats = new Cat[]
            {
                new Cat(14, "Gosho", Sex.Male),
                new Cat(6, "Mialcho", Sex.Male),
                new Cat(10, "Misha", Sex.Female),
                new Cat(9, "Joystick", Sex.Male),
                new Cat(14, "Macka", Sex.Female)
            };

            Console.ForegroundColor = ConsoleColor.Magenta;
            foreach (var item in arrWithCats)
            {
                Console.WriteLine(item.ProduceSount());
            }
            Console.WriteLine("Average ange of all cats : " + Animal.AverageAge(arrWithCats) + "\n");

            var arrWithKittens = new Kitten[]
            {
                new Kitten(2, "Peshka"),
                new Kitten(1, "Mimka"),
                new Kitten(3, "Dimitrinka"),
                new Kitten(1, "Skilitka")
            };

            Console.ForegroundColor = ConsoleColor.Cyan;
            foreach (var item in arrWithKittens)
            {
                Console.WriteLine(item.ProduceSount());
            }
            Console.WriteLine("Average ange of all kittens : " + Animal.AverageAge(arrWithKittens) + "\n");

            var arrWithTomcats = new Tomcat[]
            {
                new Tomcat(1, "Stamatcho"),
                new Tomcat(2, "Metadoncho"),
                new Tomcat(1, "Ivelincho"),
            };

            Console.ForegroundColor = ConsoleColor.Blue;
            foreach (var item in arrWithTomcats)
            {
                Console.WriteLine(item.ProduceSount());
            }
            Console.WriteLine("Average ange of all tomcats : " + Animal.AverageAge(arrWithTomcats) + "\n");

            var arrWithDogs = new Dog[]
            {
                new Dog(1, "Sharo", Sex.Male),
                new Dog(2, "Pincho", Sex.Male),
                new Dog(3, "Kalina", Sex.Female),
                new Dog(4, "Steven", Sex.Male),
                new Dog(5, "MIla", Sex.Female),
            };

            Console.ForegroundColor = ConsoleColor.Yellow;
            foreach (var item in arrWithDogs)
            {
                Console.WriteLine(item.ProduceSount());
            }
            Console.WriteLine("Average ange of all dogs : " + Animal.AverageAge(arrWithDogs) + "\n");

            var arrWithFrogs = new Frog[]
            {
                new Frog(1, "Japcho", Sex.Male),
                new Frog(2, "Kwa", Sex.Male),
                new Frog(3, "Japka", Sex.Female),
                new Frog(4, "Jaburan", Sex.Male),
                new Frog(5, "Roberta", Sex.Female),
            };

            Console.ForegroundColor = ConsoleColor.Green;
            foreach (var item in arrWithFrogs)
            {
                Console.WriteLine(item.ProduceSount());
            }
            Console.WriteLine("Average ange of all frogs : " + Animal.AverageAge(arrWithFrogs) + "\n");
        }
コード例 #14
0
 static void Main()
 {
     // Testing all classes and printing the average age of each group of animals
     Console.WriteLine("TESTING ALL CLASSES AND THE AVERAGE AGE METHOD ON THEM:");
     Cat[] cats = new Cat[] { new Cat("Pissy", 3, "Female"),
                              new Cat("Missy", 7, "Female"),
                              new Cat("Tommy", 11, "Male") };
     Console.WriteLine("The average age of the cats is: {0:F2} years.", AverageAge(cats));
     Dog[] dogs = new Dog[] { new Dog("Timmy", 8, "Male"),
                              new Dog("Gabby", 4, "Female"),
                              new Dog("Jake", 13, "Male") };
     Console.WriteLine("The average age of the dogs is: {0:F2} years.", AverageAge(dogs));
     Frog[] frogs = new Frog[] { new Frog("Josh", 1, "Male", "Blue"),
                                 new Frog("Patricia", 7, "Female", "Green"),
                                 new Frog("Helen", 6, "Female", "Black") };
     Console.WriteLine("The average age of the frogs is: {0:F2} years.", AverageAge(frogs));
     Tomcat[] tomcats = new Tomcat[] { new Tomcat("Billy", 5),
                                       new Tomcat("Oliver", 3),
                                       new Tomcat("Sully", 2),
                                       new Tomcat("Lefty", 1) };
     Console.WriteLine("The average age of the tomcats is: {0:F2} years.", AverageAge(tomcats));
     Kitten[] kittens = new Kitten[] { new Kitten("Mandy", 3),
                                       new Kitten("Suzy", 6),
                                       new Kitten("Mary", 2),
                                       new Kitten("Dolly", 5) };
     Console.WriteLine("The average age of the kittens is: {0:F2} years.", AverageAge(kittens));
     Console.WriteLine();
     // Testing the Breed property and the Gender property
     Console.WriteLine("TESTING THE BREED PROPERTY AND THE GENDER PROPERTY:");
     dogs[2].Breed = "Golden retriever";
     Console.WriteLine("{0} is a dog. It's breed is {1} and it's gender is {2}.", dogs[2].Name, dogs[2].Breed, dogs[2].Gender);
     cats[1].Breed = "Persian";
     Console.WriteLine("{0} is a cat. It's breed is {1} and it's gender is {2}.", cats[1].Name, cats[1].Breed, cats[1].Gender);
     tomcats[1].Breed = "Siberian";
     Console.WriteLine("{0} is a tomcat. It's breed is {1} and it's gender is {2}.", tomcats[1].Name, tomcats[1].Breed, tomcats[1].Gender);
     kittens[0].Breed = "Siamese";
     Console.WriteLine("{0} is a kitten. It's breed is {1} and it's gender is {2}.", kittens[0].Name, kittens[0].Breed, kittens[0].Gender);
     Console.WriteLine();
     // Testing the optional methods, that I added
     Console.WriteLine("TESTING THE OPTIONAL METHODS:");
     Console.WriteLine(cats[0].Eat());
     Console.WriteLine(dogs[0].Fetch());
     Console.WriteLine(frogs[0].Hop());
     Console.WriteLine(tomcats[0].Hunt());
     Console.WriteLine(kittens[0].Sleep());
     Console.WriteLine();
     // Testing class Animal and calculating the average age of the different animals in the array
     Animal[] animals = new Animal[5];
     animals[0] = cats[0];
     animals[1] = dogs[1];
     animals[2] = frogs[2];
     animals[3] = tomcats[3];
     animals[4] = kittens[3];
     Console.WriteLine("TESTING THE AVERAGE AGE METHOD ON AN ARRAY OF DIFFERENT ANIMALS:");
     Console.WriteLine("The average age of the animals is: {0:F2} years.", AverageAge(animals));
     Console.WriteLine();
     // Testing the MakeSound method for every animal
     Console.WriteLine("TESTING THE MAKESOUND METHOD ON EVERY CLASS:");
     foreach (var animal in animals)
     {
         Console.WriteLine("The {0} makes that sound \"{1}\"", animal.ToString().Substring(16).ToLower(), animal.MakeSound());
     }
     Console.WriteLine();
 }
コード例 #15
0
        /* 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).
         */
        public static void Main()
        {
            Dog[] dogs = new Dog[]
            {
                new Dog("Jaro", 7, true, "Golden Retriever"),
                new Dog("Sharo", 3, true, "German Sheperd"),
                new Dog("Doge", 5, true, "Labrador Retriever"),
                new Dog("Estel", 10, false, "Pincher")
            };

            Frog[] frogs = new Frog[]
            {
                new Frog("Kikirica", 13, false),
                new Frog("Jaba", 15, false),
                new Frog("Froggy", 5, true),
                new Frog("Nikoleta Lozanova", 10, false)
            };

            Cat[] cats = new Cat[]
            {
                new Cat("Street Excellent", 3, false),
                new Cat("Home Excellent", 5, false),
                new Cat("Persiiko", 1, true),
                new Cat("Garfield", 7, true)
            };

            Kitten[] kittens = new Kitten[]
            {
                new Kitten("Malcho", 1),
                new Kitten("Palcho", 2),
                new Kitten("Shalco", 1)
            };

            Tomcat[] tomcats = new Tomcat[]
            {
                new Tomcat("Kotio", 5),
                new Tomcat("Gosho", 4),
                new Tomcat("Pesho", 8)
            };

            double dogsAverageAge = Animal.AverageAge(dogs);
            double frogsAverageAge = Animal.AverageAge(frogs);
            double catsAverageAge = Animal.AverageAge(cats);
            double kittensAverageAge = Animal.AverageAge(kittens);
            double tomcatsAverageAge = Animal.AverageAge(tomcats);
            Console.WriteLine("Average age of the dogs: {0:F2}", dogsAverageAge);
            Console.WriteLine("Average age of the frogs: {0:F2}", frogsAverageAge);
            Console.WriteLine("Average age of the cats: {0:F2}", catsAverageAge);
            Console.WriteLine("Average age of the kittens: {0:F2}", kittensAverageAge);
            Console.WriteLine("Average age of the tomcats: {0:F2}", tomcatsAverageAge);
            Console.WriteLine(new string('-', 10));
            Console.WriteLine("Actions: ");
            Console.WriteLine(tomcats[0].Hunt());
            Console.WriteLine((cats[1].BeGracious()));
            Console.WriteLine(dogs[2].Fetch());
            Console.WriteLine(frogs[1].JumpAround());
            Console.WriteLine(new string('-', 10));
            Console.WriteLine("Sounds: ");
            cats[0].MakeSound();
            dogs[1].MakeSound();
            frogs[2].MakeSound();
        }