Esempio n. 1
0
 private static void print(Animals zoo)
 {
     foreach (Animal animal in zoo)
     {
         Console.WriteLine("{0} {1}", animal.Genus, animal.Weight);
     }
 }
Esempio n. 2
0
        static void Main(string[] args)
        {
            // 10) Create an arary of Animal objects and object of Animals
            // print animals with foreach operator for object of Animals
            Animal[] animal = new Animal[5];
            animal[0] = new Animal("Dog", 30);
            animal[1] = new Animal("Cat", 7);
            animal[2] = new Animal("Cow", 1200);
            animal[3] = new Animal("Tiger", 500);
            animal[4] = new Animal("Lion", 600);

            Animals zoo = new Animals(animal);
            Console.WriteLine("Animals:");
            print(zoo);

            // 11) Invoke 3 types of sorting
            // and print results with foreach operator for array of Animal objects
            Console.WriteLine(new string('*', 30));
            Console.WriteLine("Sorting by genus:" );
            Array.Sort(animal);
            print(zoo);

            Console.WriteLine(new string('*', 30));
            Console.WriteLine("Sorting by weigh ascending:");
            Array.Sort(animal, Animal.SortWeightAscending);
            print(zoo);

            Console.WriteLine(new string('*', 30));
            Console.WriteLine("Sorting by genus descending:");
            Array.Sort(animal, Animal.SortGenusDescending);
            print(zoo);

            Console.ReadLine();
        }
Esempio n. 3
0
        static void Main(string[] args)
        {
            // 10) Create an array of Animal objects and object of Animals
            // print animals with foreach operator for object of Animals

            Animal[] animals    = { new Animal("Tiger", 180), new Animal("Lioin", 30), new Animal("Zebra", 120) };
            Animals  animalsObj = new Animals(animals);

            Console.WriteLine("Objext od Animals: ");
            foreach (Animal item in animalsObj)
            {
                Console.WriteLine($"Animal: {item.Genus} -- Weight: {item.Weight}");
            }
            Console.WriteLine(new string('-', 30));

            // 11) Invoke 3 types of sorting
            // and print results with foreach operator for array of Animal objects

            Array.Sort(animals, Animal.SortGenusDescending());
            foreach (var item in animals)
            {
                Console.WriteLine($"Animal: {item.Genus} -- Weight: {item.Weight}");
            }
            Console.WriteLine(new string('-', 30));


            Array.Sort(animals, Animal.SortWeightAccending());
            foreach (var item in animals)
            {
                Console.WriteLine($"Animal: {item.Genus} -- Weight: {item.Weight}");
            }
            Console.WriteLine(new string('-', 30));

            Array.Sort(animals);
            foreach (var item in animals)
            {
                Console.WriteLine($"Animal: {item.Genus} -- Weight: {item.Weight}");
            }
            Console.WriteLine(new string('-', 30));

            Console.ReadLine();
        }
Esempio n. 4
0
        static void Main(string[] args)
        {
            // 10) Create an arary of Animal objects and object of Animals
            // print animals with foreach operator for object of Animals
            Animal[] animal = new Animal[4];
            animal[0] = new Animal("Pig", 50);
            animal[1] = new Animal("Goat", 40);
            animal[2] = new Animal("Chicken", 25);
            animal[3] = new Animal("Elephant", 2000);
            Animals animals = new Animals(animal);

            Console.WriteLine("Animals:");
            foreach (Animal an in animal)
            {
                Console.WriteLine("{0},{1}", an.name, an.weight);
            }
            // 11) Invoke 3 types of sorting
            // and print results with foreach operator for array of Animal objects
            Console.WriteLine("Sorted by genus Ascending:");
            Array.Sort(animal);
            foreach (Animal an in animal)
            {
                Console.WriteLine("{0},{1}", an.name, an.weight);
            }
            Array.Sort(animal, Animal.SortGenusDescending);
            Console.WriteLine("Sorted by genus Descending:");
            foreach (Animal an in animal)
            {
                Console.WriteLine("{0},{1}", an.name, an.weight);
            }
            Array.Sort(animal, Animal.SortWeightAscending);
            Console.WriteLine("Sorted by weight:");
            foreach (Animal an in animal)
            {
                Console.WriteLine("{0},{1}", an.name, an.weight);
            }
            Console.ReadKey();
        }
Esempio n. 5
0
 static void Main(string[] args)
 {
     // 10) Create an arary of Animal objects and object of Animals    
     // print animals with foreach operator for object of Animals               
     Animal[] animal = new Animal[4];
     animal[0] = new Animal("Pig", 50);
     animal[1] = new Animal("Goat", 40);
     animal[2] = new Animal("Chicken", 25);
     animal[3] = new Animal("Elephant", 2000);
     Animals animals = new Animals(animal);
     Console.WriteLine("Animals:");
     foreach (Animal an in animal)
     {
         Console.WriteLine("{0},{1}",an.name,an.weight);
     }
     // 11) Invoke 3 types of sorting 
     // and print results with foreach operator for array of Animal objects         
     Console.WriteLine("Sorted by genus Ascending:");
     Array.Sort(animal);
     foreach (Animal an in animal)
     {
         Console.WriteLine("{0},{1}",an.name, an.weight);
     }
     Array.Sort(animal, Animal.SortGenusDescending);
     Console.WriteLine("Sorted by genus Descending:");
     foreach (Animal an in animal)
     {
         Console.WriteLine("{0},{1}", an.name, an.weight);
     }
     Array.Sort(animal, Animal.SortWeightAscending);
     Console.WriteLine("Sorted by weight:");
     foreach (Animal an in animal)
     {
         Console.WriteLine("{0},{1}", an.name, an.weight);
     }
     Console.ReadKey();
 }