コード例 #1
0
ファイル: Animal.cs プロジェクト: BlueForeverI/OOPHomework
        /// <summary>
        /// Calculates the verage age of all animal in an array
        /// </summary>
        /// <param name="animals">The animals</param>
        /// <returns>The average age</returns>
        public static double CalculateAverageAge(Animal[] animals)
        {
            double sum = 0;

            for(int i = 0; i < animals.Length; i ++)
            {
                sum += animals[i].Age;
            }

            return sum / animals.Length;
        }
コード例 #2
0
        static void GetAverageAge(Animal[] animals)
        {
            var speciesByAverageAge =
                from animal in animals
                group animal by animal.GetType() into a
                select new { Species = a.Key.Name, AverageAge = a.Average(animal => animal.Age) };

            foreach (var species in speciesByAverageAge)
            {
                Console.WriteLine("{0} Average age: {1}",species.Species,species.AverageAge);
            }
        }
コード例 #3
0
 static void Main(string[] args)
 {
     Animal[] animals = new Animal[]
     {
         new Frog(10,"John",Sex.male),
         new Kitten(5,"Pussy"),
         new Tomcat(6,"Kasanova"),
         new Dog(4,"Balkan",Sex.male),
         new Dog(5,"Pencho",Sex.male),
         new Kitten(2,"Penka"),
         new Kitten(3,"Virginia"),
         new Tomcat(8,"Kotkar"),
         new Tomcat(3,"Garfield")
     };
     GetAverageAge(animals);
 }
コード例 #4
0
ファイル: TestAnimals.cs プロジェクト: quela/myprojects
        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();
        }
コード例 #5
0
ファイル: Animal.cs プロジェクト: androidejl/Telerik
 public static double CalculateAverage(Animal[] animals)
 {
     double sum = 0d;
     foreach(Animal animal in animals)
     {
         sum += animal.age;
     }
     return sum / (double)animals.Length;
 }