public static Animal Create(string animalType, string[] animalInfo)
        {
            Validator.AnimalType(animalType);
            Validator.AnimalInfo(animalInfo);

            string name   = animalInfo[0];
            int    age    = int.Parse(animalInfo[1]);
            string gender = animalInfo[2];

            switch (animalType)
            {
            case "Dog":
                var newDog = new Dog(name, age, gender);
                return(newDog);

            case "Cat":
                var newCat = new Cat(name, age, gender);
                return(newCat);

            case "Frog":
                var newFrog = new Frog(name, age, gender);
                return(newFrog);

            case "Kitten":
                var kitten = new Kitten(name, age, gender);
                return(kitten);

            case "Tomcat":
                var tomcat = new Tomcat(name, age, gender);
                return(tomcat);
            }

            return(null);
        }
    public static void TestCreatures()
    {
        Dog charlie = new Dog("Charlie", 4, true);

        Console.WriteLine(charlie);
        charlie.ProduceSound();

        Console.WriteLine();

        Frog quackster = new Frog("Rab", 1, false);

        Console.WriteLine(quackster);
        quackster.ProduceSound();

        Console.WriteLine();

        Cat miew = new Cat("Dangleton", 3, false);

        Console.WriteLine(miew);
        miew.ProduceSound();

        Console.WriteLine();

        Kitten kitty = new Kitten("KittyCat", 3);

        Console.WriteLine(kitty);
        kitty.ProduceSound();

        Console.WriteLine();

        Tomcat tom = new Tomcat("Tom", 2);

        Console.WriteLine(tom);
        tom.ProduceSound();
    }
示例#3
0
    static Animal CreateAnimal(string type, string name, int age, string gender)
    {
        Animal currentAnimal = null;

        switch (type.ToLower())
        {
        case "cat":
            currentAnimal = new Cat(name, age, gender);
            break;

        case "tomcat":
            currentAnimal = new Tomcat(name, age);
            break;

        case "kitten":
            currentAnimal = new Kitten(name, age);
            break;

        case "dog":
            currentAnimal = new Dog(name, age, gender);
            break;

        case "frog":
            currentAnimal = new Frog(name, age, gender);
            break;

        default:
            throw new ArgumentException("Invalid input!");
            break;
        }

        return(currentAnimal);
    }
示例#4
0
    private static void ParseAnimal(List <Animal> animals, string animalType, string[] animalParts)
    {
        switch (animalType.ToLower())
        {
        case "dog":
            Dog dog = new Dog(animalParts[0], int.Parse(animalParts[1]), animalParts[2]);
            animals.Add(dog);
            break;

        case "cat":
            Cat cat = new Cat(animalParts[0], int.Parse(animalParts[1]), animalParts[2]);
            animals.Add(cat);
            break;

        case "frog":
            Frog frog = new Frog(animalParts[0], int.Parse(animalParts[1]), animalParts[2]);
            animals.Add(frog);
            break;

        case "kitten":
            Kitten kitten = new Kitten(animalParts[0], int.Parse(animalParts[1]));
            animals.Add(kitten);
            break;

        case "tomcat":
            Tomcat tomcat = new Tomcat(animalParts[0], int.Parse(animalParts[1]));
            animals.Add(tomcat);
            break;

        default:
            throw new ArgumentException("Invalid input!");
        }
    }
示例#5
0
    static void Main()
    {
        Dog dogOne = new Dog("Sharo", "male", 7);
        Dog dogTwo = new Dog("Sara", "female", 17);
        Dog dogThree = new Dog("Tara", "female", 1);
        Dog[] dogArr = new Dog[] { dogOne, dogTwo, dogThree };
        Console.WriteLine("The average age of dogs is {0:F2} years old.", Animal.CalculateAverage(dogArr));
        Console.WriteLine("Dog {0} says {1}.", dogThree.Name, dogThree.PrintSound());

        Frog frogOne = new Frog("Skokla", "female", 23);
        Frog frogTwo = new Frog("Handsome", "male", 9);
        Frog frogThree = new Frog("Skokancho", "male", 2);
        Frog[] frogArr = new Frog[] { frogOne, frogTwo, frogThree };
        Console.WriteLine("The average age of frogs is {0:F2} years old.", Animal.CalculateAverage(frogArr));
        Console.WriteLine("Frog {0} says {1}.", frogThree.Name, frogThree.PrintSound());

        Kitten kittenOne = new Kitten("Hihi", 1);
        Kitten kittenTwo = new Kitten("Kiki", 1);
        Kitten kittenThree = new Kitten("Kenny", 2);
        Kitten[] kittenArr = new Kitten[] { kittenOne, kittenTwo, kittenThree };
        Console.WriteLine("The average age of kittens is {0:F2} years old.", Animal.CalculateAverage(kittenArr));
        Console.WriteLine("Kitten {0} says {1}.", kittenThree.Name, kittenThree.PrintSound());

        Tomcat tomcatOne = new Tomcat("Pancho", 4);
        Tomcat tomcatTwo = new Tomcat("Robby", 3);
        Tomcat tomcatThree = new Tomcat("Vasko", 8);
        Tomcat[] tomcatArr = new Tomcat[] { tomcatOne, tomcatTwo, tomcatThree };
        Console.WriteLine("The average age of tomcats is {0:F2} years old.", Animal.CalculateAverage(tomcatArr));
        Console.WriteLine("Tomcat {0} says {1}.", tomcatThree.Name, tomcatThree.PrintSound());
    }
示例#6
0
        public static void Main()
        {
            Frog[] frogs = new Frog[5];
            frogs[0] = new Frog("Kermit", 5, 'M');
            frogs[1] = new Frog("Piggy", 8, 'F');
            frogs[2] = new Frog("Cookie Monster", 3, 'F');
            frogs[3] = new Frog("Oscar the grouch", 15, 'M');
            frogs[4] = new Frog("Kermit's double", 5, 'M');

            Dog[] dogs = new Dog[5];
            dogs[0] = new Dog("Rex", 1, 'M');
            dogs[1] = new Dog("Sergeant Dirty", 6, 'F');
            dogs[2] = new Dog("Lucky", 7, 'F');
            dogs[3] = new Dog("Unlucky", 2, 'M');
            dogs[4] = new Dog("Tramp", 4, 'M');

            Cat[] cats = new Cat[5];
            cats[0] = new Cat("Tom", 6, 'M');
            cats[1] = new Kitten("Princess", 1);
            cats[2] = new Cat("Jerry", 2, 'F');
            cats[3] = new Tomcat("Behemoth", 1);
            cats[4] = new Cat("Pluto", 7, 'M');

            Console.WriteLine("The average age of the frogs is " + Animal.AverageAge(frogs));
            Console.WriteLine("The average age of the dogs is " + Animal.AverageAge(dogs));
            Console.WriteLine("The average age of the cats is " + Animal.AverageAge(cats));
        }
示例#7
0
        public Animal CreateAnimal(string type, string name, int age, string gender)
        {
            Animal animal;

            if (type == "Cat")
            {
                animal = new Cat(name, age, gender);
            }
            else if (type == "Tomcat")
            {
                animal = new Tomcat(name, age);
            }
            else if (type == "Kitten")
            {
                animal = new Kitten(name, age);
            }
            else if (type == "Dog")
            {
                animal = new Dog(name, age, gender);
            }
            else if (type == "Frog")
            {
                animal = new Frog(name, age, gender);
            }
            else
            {
                throw new ArgumentException("Invalid input!");
            }

            return(animal);
        }
示例#8
0
    private static void AddAnimal(List <Animal> animals, string animalType, string name, int age, string gender)
    {
        switch (animalType)
        {
        case "Dog":
            var dog = new Dog(name, age, gender);
            animals.Add(dog);
            break;

        case "Cat":
            var cat = new Cat(name, age, gender);
            animals.Add(cat);
            break;

        case "Frog":
            var frog = new Frog(name, age, gender);
            animals.Add(frog);
            break;

        //It may be kittenS
        case "Kitten":
            var kitten = new Kitten(name, age);
            animals.Add(kitten);
            break;

        case "Tomcat":
            var tomcat = new Tomcat(name, age);
            animals.Add(tomcat);
            break;

        default:
            throw new ArgumentException("Invalid input!");
        }
    }
示例#9
0
        static void Main(string[] args)
        {
            List <Animal> Animals = new List <Animal>();

            List <Dog>  Dogs  = new List <Dog>();
            List <Cat>  Cats  = new List <Cat>();
            List <Frog> Frogs = new List <Frog>();

            Dog dog1 = new Dog("Sparky", 2, "male"); Animals.Add(dog1);
            Dog dog2 = new Dog("Hugo", 3, "male"); Animals.Add(dog2);
            Dog dog3 = new Dog("Luna", 4, "female"); Animals.Add(dog3);

            Cat    cat1 = new Cat("Calypso", 1, "female"); Animals.Add(cat1);
            Kitten cat2 = new Kitten("Maya", 2); Animals.Add(cat2);
            Tomcat cat3 = new Tomcat("Thomas", 3); Animals.Add(cat3);

            Frog frog1 = new Frog("Kenen", 5, "male"); Animals.Add(frog1);
            Frog frog2 = new Frog("Giraya", 4, "male"); Animals.Add(frog2);


            foreach (Animal animal in Animals)
            {
                Console.WriteLine($"Name: {animal.Name}; Age: {animal.Age}; Gender: {animal.Gender}; Sound: {animal.getSound()}");
            }

            Console.WriteLine();


            Console.WriteLine($"The average age for animals is: {AverageAge(Animals)}");
            Console.WriteLine($"The average age for dogs is: {AverageAgeDogs(Animals)}");
            Console.WriteLine($"The average age for cats is: {AverageAgeCats(Animals)}");
            Console.WriteLine($"The average age for frogs is: {AverageAgeFrogs(Animals)}");

            Console.ReadKey();
        }
    public static void Main()
    {
        Turtle turtle = new Turtle();
        Console.WriteLine(turtle);
        Console.WriteLine("The {0} can go {1} km/h ", turtle.GetName(), turtle.Speed);

        Console.WriteLine();

        Cheetah cheetah = new Cheetah();
        Console.WriteLine(cheetah);
        Console.WriteLine("The {0} can go {1} km/h ", cheetah.GetName(), cheetah.Speed);

        Console.WriteLine();

        Tomcat tomcat = new Tomcat();
        Console.WriteLine(tomcat);
        Console.WriteLine("The {0} can go {1} km/h ", tomcat.GetName(), tomcat.Speed);
        tomcat.SayMyaau();

        Console.WriteLine();

        Kitten kitten = new Kitten();
        Console.WriteLine(kitten);
        Console.WriteLine("The {0} can go {1} km/h ", kitten.GetName(), kitten.Speed);
        kitten.SayMyaau();

        // This will not compile (Cat is abstract -> cannot be instantiated)
        //Cat cat = new Cat();
    }
    private static void ReadAndCreateAnimal(List <Animal> animals, string input)
    {
        string[] animalData = Console.ReadLine().Split();
        switch (input)
        {
        case "Dog":
            Dog dog = new Dog(animalData[0], int.Parse(animalData[1]), animalData[2]);
            animals.Add(dog);
            break;

        case "Cat":
            Cat cat = new Cat(animalData[0], int.Parse(animalData[1]), animalData[2]);
            animals.Add(cat);
            break;

        case "Frog":
            Frog frog = new Frog(animalData[0], int.Parse(animalData[1]), animalData[2]);
            animals.Add(frog);
            break;

        case "Tomcat":
            Tomcat tomcat = new Tomcat(animalData[0], int.Parse(animalData[1]));
            animals.Add(tomcat);
            break;

        case "Kitten":
            Kitten kitten = new Kitten(animalData[0], int.Parse(animalData[1]));
            animals.Add(kitten);
            break;

        default: throw new ArgumentException("Invalid input!");
        }
    }
示例#12
0
    public static void Main()
    {
        string command;

        while ((command = Console.ReadLine()) != "Beast!")
        {
            var animalInfo = Console.ReadLine().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

            try
            {
                switch (command)
                {
                case "Dog":
                    var    dogName   = animalInfo[0];
                    var    dogAge    = int.Parse(animalInfo[1]);
                    var    dogGender = animalInfo[2];
                    Animal dog       = new Dog(dogName, dogAge, dogGender);
                    Console.WriteLine(dog);
                    break;

                case "Cat":
                    var    catName   = animalInfo[0];
                    var    catAge    = int.Parse(animalInfo[1]);
                    var    catGender = animalInfo[2];
                    Animal cat       = new Cat(catName, catAge, catGender);
                    Console.WriteLine(cat);
                    break;

                case "Frog":
                    var    frogName   = animalInfo[0];
                    var    frogAge    = int.Parse(animalInfo[1]);
                    var    frogGender = animalInfo[2];
                    Animal frog       = new Frog(frogName, frogAge, frogGender);
                    Console.WriteLine(frog);
                    break;

                case "Kitten":
                    var    kittenName = animalInfo[0];
                    var    kittenAge  = int.Parse(animalInfo[1]);
                    Animal kitten     = new Kitten(kittenName, kittenAge, "Female");
                    Console.WriteLine(kitten);
                    break;

                case "Tomcat":
                    var    tomcatName = animalInfo[0];
                    var    tomcatAge  = int.Parse(animalInfo[1]);
                    Animal tomcat     = new Tomcat(tomcatName, tomcatAge, "Male");
                    Console.WriteLine(tomcat);
                    break;

                default:
                    throw new ArgumentException("Invalid input!");
                }
            }
            catch (ArgumentException argEx)
            {
                Console.WriteLine(argEx.Message);
            }
        }
    }
    public static void Main()
    {
        Turtle turtle = new Turtle();

        Console.WriteLine(turtle);
        Console.WriteLine("The {0} can go {1} km/h ", turtle.GetName(), turtle.Speed);

        Console.WriteLine();

        Cheetah cheetah = new Cheetah();

        Console.WriteLine(cheetah);
        Console.WriteLine("The {0} can go {1} km/h ", cheetah.GetName(), cheetah.Speed);

        Console.WriteLine();

        Tomcat tomcat = new Tomcat();

        Console.WriteLine(tomcat);
        Console.WriteLine("The {0} can go {1} km/h ", tomcat.GetName(), tomcat.Speed);
        tomcat.SayMyaau();

        Console.WriteLine();

        Kitten kitten = new Kitten();

        Console.WriteLine(kitten);
        Console.WriteLine("The {0} can go {1} km/h ", kitten.GetName(), kitten.Speed);
        kitten.Breed = "siamska";//Ot dopalnitelnoto Property ot Cat
        Console.WriteLine("The breed of the {0} is {1}", kitten.GetName(), kitten.Breed);
        kitten.SayMyaau();

        // This will not compile (Cat is abstract -> cannot be instantiated)
        //Cat cat = new Cat();
    }
示例#14
0
        /// <exception cref="System.Exception"/>
        protected internal virtual void StartTomcat()
        {
            tomcat = new Tomcat();
            FilePath  @base = new FilePath(Runtime.GetProperty("java.io.tmpdir"));
            Context   ctx   = tomcat.AddContext("/foo", @base.GetAbsolutePath());
            FilterDef fd    = new FilterDef();

            fd.SetFilterClass(typeof(AuthenticatorTestCase.TestFilter).FullName);
            fd.SetFilterName("TestFilter");
            FilterMap fm = new FilterMap();

            fm.SetFilterName("TestFilter");
            fm.AddURLPattern("/*");
            fm.AddServletName("/bar");
            ctx.AddFilterDef(fd);
            ctx.AddFilterMap(fm);
            Tomcat.AddServlet(ctx, "/bar", typeof(AuthenticatorTestCase.TestServlet).FullName
                              );
            ctx.AddServletMapping("/bar", "/bar");
            host = "localhost";
            port = GetLocalPort();
            tomcat.SetHostname(host);
            tomcat.SetPort(port);
            tomcat.Start();
        }
示例#15
0
 static void Main()
 {
     //
     Cat somecat = new Cat("Monika_Cat",4,"female");
     somecat.ProduceSound();
     Kitten kitten = new Kitten("Vesi_Kitten", 5);
     kitten.ProduceSound();
     Console.WriteLine(kitten.Sex);
     Tomcat tomCat = new Tomcat("Tomas_TomCat", 6);
     tomCat.ProduceSound();
     Console.WriteLine(tomCat.Sex);
     Frog kyrmit = new Frog("Kyrmit_Jabok", 2, "male");
     Dog rex = new Dog("Rex_Dog", 7, "male");
     Dog hera = new Dog("Hera_Dog", 8, "female");
     List<Animal> animalsList = new List<Animal>();
     Cat lora = new Cat("Lora_Cat", 3, "female");
     animalsList.Add(somecat);
     animalsList.Add(kitten);
     animalsList.Add(tomCat);
     animalsList.Add(kyrmit);
     animalsList.Add(rex);
     animalsList.Add(hera);
     animalsList.Add(lora);
     //Average groups age
     var animalGroups =
         from animal in animalsList
         group animal by animal.GetType();
     foreach (var animals in animalGroups)
     {
         Console.WriteLine(animals.myAverage());
     }
 }
示例#16
0
        public Animal CreateAnimal(string animalType, string name, int age, string gender)
        {
            string type = animalType.ToLower();

            switch (type)
            {
            case "dog":
                Animal dog = new Dog(name, age, gender);
                return(dog);

            case "frog":
                Animal frog = new Frog(name, age, gender);
                return(frog);

            case "cat":
                Animal cat = new Cat(name, age, gender);
                return(cat);

            case "tomcat":
                Animal tomcat = new Tomcat(name, age);
                return(tomcat);

            case "kitten":
                Animal kitten = new Kitten(name, age);
                return(kitten);

            default: throw new ArgumentException("Invalid input!");
            }
        }
示例#17
0
        public override void stop()
        {
            try
            {
                try
                {
                    tomcat.stop();
                }
                catch (Exception e)
                {
//JAVA TO C# CONVERTER WARNING: The .NET Type.FullName property will not always yield results identical to the Java Class.getName method:
                    Logger.getLogger(this.GetType().FullName).log(Level.WARNING, "Failed to stop tomcat instance", e);
                }

                try
                {
                    tomcat.destroy();
                }
                catch (Exception e)
                {
//JAVA TO C# CONVERTER WARNING: The .NET Type.FullName property will not always yield results identical to the Java Class.getName method:
                    Logger.getLogger(this.GetType().FullName).log(Level.WARNING, "Failed to destroy instance", e);
                }

                tomcat = null;
            }
            catch (Exception e)
            {
                throw new Exception(e);
            }
        }
示例#18
0
    static void Main()
    {
        Tomcat tom    = new Tomcat("tom", 12);
        Kitten kitty  = new Kitten("kitty", 3);
        Cat    matsa  = new Cat("matsa", 8, Sex.Female);
        Frog   jabcho = new Frog("jabcho", 5, Sex.Male);
        Dog    sharo  = new Dog("sharo", 14, Sex.Male);

        List <Animal> animals = new List <Animal>();

        animals.Add(tom);
        animals.Add(kitty);
        animals.Add(matsa);
        animals.Add(jabcho);
        animals.Add(sharo);

        foreach (var animal in animals)
        {
            animal.MakeSound();
        }

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

        var averageAge = animals.Average(anim => anim.Age);

        Console.WriteLine("\nAverage age of all animals is: {0} years", averageAge);
    }
示例#19
0
    static void Main(string[] args)
    {
        string input = Console.ReadLine().Trim();

        while (input != "Beast!")
        {
            try
            {
                string[] data = Console.ReadLine().Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).ToArray();
                string   name = data[0];
                int      age;
                if (!int.TryParse(data[1], out age))
                {
                    throw new ArgumentException("Invalid input!");
                }

                string gender = data[2];

                switch (input)
                {
                case "Dog":
                    Dog dog = new Dog(name, age, gender);
                    Console.WriteLine(dog);
                    break;

                case "Cat":
                    Cat cat = new Cat(name, age, gender);
                    Console.WriteLine(cat);
                    break;

                case "Frog":
                    Frog frog = new Frog(name, age, gender);
                    Console.WriteLine(frog);
                    break;

                case "Kitten":
                    Kitten kitten = new Kitten(name, age, gender);
                    Console.WriteLine(kitten);
                    break;

                case "Tomcat":
                    Tomcat tomcat = new Tomcat(name, age, gender);
                    Console.WriteLine(tomcat);
                    break;

                default:
                    throw new ArgumentException("Invalid input!");
                }
            }
            catch (ArgumentException ae)
            {
                Console.WriteLine(ae.Message);
            }
            catch (Exception)
            {
                Console.WriteLine("Invalid input!");
            }
            input = Console.ReadLine().Trim();
        }
    }
示例#20
0
    static void Main()
    {
        Turtle turtle = new Turtle();

        Console.WriteLine(turtle);
        Console.WriteLine("The {0} can go {1} km/h ", turtle.GetName(), turtle.Speed);

        Console.WriteLine();

        Cheetah cheetah = new Cheetah();

        Console.WriteLine(cheetah);
        Console.WriteLine("The {0} can go {1} km/h ", cheetah.GetName(), cheetah.Speed);

        Console.WriteLine();

        Tomcat tomcat = new Tomcat();

        Console.WriteLine(tomcat);
        Console.WriteLine("The {0} can go {1} km/h ", tomcat.GetName(), tomcat.Speed);
        tomcat.SayMyaau();

        Console.WriteLine();

        Kitten kitten = new Kitten();

        Console.WriteLine(kitten);
        Console.WriteLine("The {0} can go {1} km/h ", kitten.GetName(), kitten.Speed);
        kitten.SayMyaau();

        // This wil not compile (Cat is abstract -> cannot be instantiated)
        //Cat cat = new Cat();
    }
示例#21
0
文件: Program.cs 项目: nikoyal/C-OOP
    static void Main(string[] args)
    {
        string type = Console.ReadLine();

        while (type != "Beast!")
        {
            try
            {
                string[] animalInfo   = Console.ReadLine().Split();
                string   animalName   = animalInfo[0];
                int      animalAge    = int.Parse(animalInfo[1]);
                string   animalGender = animalInfo[2];
                switch (type.ToLower())
                {
                case "cat":
                    Cat cat = new Cat(animalName, animalAge, animalGender);
                    Console.WriteLine(type);
                    Console.WriteLine(cat);
                    break;

                case "dog":
                    Dog dog = new Dog(animalName, animalAge, animalGender);
                    Console.WriteLine(type);
                    Console.WriteLine(dog);
                    break;

                case "frog":
                    Frog frog = new Frog(animalName, animalAge, animalGender);
                    Console.WriteLine(type);
                    Console.WriteLine(frog);
                    break;

                case "kitten":
                    Kitten kitten = new Kitten(animalName, animalAge);
                    Console.WriteLine(type);
                    Console.WriteLine(kitten);
                    break;

                case "tomcat":
                    Tomcat tomcat = new Tomcat(animalName, animalAge);
                    Console.WriteLine(type);
                    Console.WriteLine(tomcat);
                    break;

                default:
                    Animal animal = new Animal(animalName, animalAge, animalGender);
                    Console.WriteLine(type);
                    Console.WriteLine(animal);
                    break;
                }
            }
            catch (ArgumentException ae)
            {
                Console.WriteLine(ae.Message);
            }

            type = Console.ReadLine();
        }
    }
    static void Main()
    {
        List <Animal> animals = new List <Animal>();
        string        command;

        while ((command = Console.ReadLine()) != "Beast!")
        {
            string   animalType = command;
            string[] input      = Console.ReadLine().Split().ToArray();

            string name   = input[0];
            int    age    = int.Parse(input[1]);
            string gender = input[2];
            try
            {
                switch (animalType.ToLower())
                {
                case "dog":
                    var dog = new Dog(name, age, gender);
                    animals.Add(dog);
                    break;

                case "frog":
                    var frog = new Frog(name, age, gender);
                    animals.Add(frog);
                    break;

                case "cat":
                    var cat = new Cat(name, age, gender);
                    animals.Add(cat);
                    break;

                case "kitten":
                    var kitten = new Kitten(name, age, gender);
                    animals.Add(kitten);
                    break;

                case "tomcat":
                    var tomcat = new Tomcat(name, age, gender);
                    animals.Add(tomcat);
                    break;

                default:
                    throw new ArgumentException("Invalid input!");
                }
            }


            catch (ArgumentException e)
            {
                Console.WriteLine(e.Message);
            }
        }
        foreach (var a in animals)
        {
            Console.WriteLine(a);
        }
    }
示例#23
0
    static void Main()
    {
        List <Animal> animals = new List <Animal>();
        string        animalType;

        while ((animalType = Console.ReadLine()) != "Beast!")
        {
            try
            {
                string[] animalInfo = Console.ReadLine().Split();
                string   name       = animalInfo[0];
                int      age        = int.Parse(animalInfo[1]);
                string   gender     = String.Empty;
                if (animalInfo.Length == 3)
                {
                    gender = animalInfo[2];
                }
                switch (animalType)
                {
                case "Cat":
                    Cat cat = new Cat(name, age, gender);
                    animals.Add(cat);
                    break;

                case "Dog":
                    Dog dog = new Dog(name, age, gender);
                    animals.Add(dog);
                    break;

                case "Frog":
                    Frog frog = new Frog(name, age, gender);
                    animals.Add(frog);
                    break;

                case "Kitten":
                    Kitten kitten = new Kitten(name, age);
                    animals.Add(kitten);
                    break;

                case "Tomcat":
                    Tomcat tomcat = new Tomcat(name, age);
                    animals.Add(tomcat);
                    break;

                default:
                    throw new ArgumentException("Invalid input!");
                }
            }
            catch (ArgumentException exception)
            {
                Console.WriteLine(exception.Message);
            }
        }
        foreach (Animal animal in animals)
        {
            Console.WriteLine(animal);
        }
    }
示例#24
0
    private static void ReadAnimals(List <Animal> animals, string command)
    {
        Animal animal     = new Animal();
        string animalType = "";

        for (int i = 0; i < 2; i++)
        {
            if (i == 1)
            {
                command = Console.ReadLine();
            }
            var input = command.Split(new[] { ' ' }, StringSplitOptions.None);
            if (input.Length == 1)
            {
                animalType = input[0];
            }
            else if (input.Length == 3)
            {
                var name   = input[0];
                var age    = int.Parse(input[1]);
                var gender = "";
                if (input.Length == 3)
                {
                    gender = input[2];
                }
                switch (animalType)
                {
                case "Cat":
                    animal = new Cat(name, age, gender);
                    break;

                case "Dog":
                    animal = new Dog(name, age, gender);
                    break;

                case "Frog":
                    animal = new Frog(name, age, gender);
                    break;

                case "Tomcat":
                    animal = new Tomcat(name, age);
                    break;

                case "Kitten":
                    animal = new Kitten(name, age);
                    break;

                default: throw new ArgumentException($"Invalid input!");
                }
                animals.Add(animal);
            }
            else
            {
                throw new ArgumentException($"Invalid input!");
            }
        }
    }
示例#25
0
        public static void Main()
        {
            Kitten kat = new Kitten("Sunny", 9);

            Console.WriteLine(kat);

            Tomcat grumpyCat = new Tomcat("Grumpy Cat", 5);

            Console.WriteLine(grumpyCat);

            kat.MakeSound();

            List <Dog> dogList = new List <Dog>()
            {
                new Dog("Rex", 6, Gender.Male),
                new Dog("Roxana", 4, Gender.Female),
                new Dog("Doge", 7, Gender.Male)
            };

            Console.WriteLine("\nAverage dog age: {0}\n", (int)dogList.Average(x => x.Age));

            List <Frog> forgList = new List <Frog>()
            {
                new Frog("Joro", 3, Gender.Female),
                new Frog("Queen Victoria", 2, Gender.Male),
                new Frog("Badass", 5, Gender.Male)
            };

            Console.WriteLine("Average frog age: {0}\n", (int)forgList.Average(x => x.Age));

            List <Cat> catList = new List <Cat>()
            {
                new Cat("Felicity", 11, Gender.Female),
                new Cat("Gesha", 4, Gender.Male),
                new Cat("Lorenzo", 2, Gender.Male)
            };

            Console.WriteLine("Average cat age: {0}\n", (int)catList.Average(x => x.Age));

            List <Kitten> kittenList = new List <Kitten>()
            {
                new Kitten("Shmatio", 6),
                new Kitten("Kitten", 2),
                kat
            };

            Console.WriteLine("Average kitten age: {0}\n", (int)kittenList.Average(x => x.Age));

            List <Tomcat> tomcatList = new List <Tomcat>()
            {
                new Tomcat("Gribbo", 13),
                new Tomcat("Batalen", 7),
                grumpyCat
            };

            Console.WriteLine("Average tomcat age: {0}\n", (int)tomcatList.Average(x => x.Age));
        }
示例#26
0
 public static Tomcat[] FillTomArr()
 {
     var tomArr = new Tomcat[rnd.Next(5, 21)];
     for (int i = 0; i < tomArr.Length; i++)
     {
         tomArr[i] = new Tomcat(GetRandomName(), rnd.Next(1, 16));
     }
     return tomArr;
 }
示例#27
0
        public void Run()
        {
            while (true)
            {
                var animalType = Console.ReadLine();

                if (animalType == "Beast!")
                {
                    break;
                }

                try
                {
                    string[] animalArgs = Console.ReadLine().Split(" ");
                    string   name       = animalArgs[0];
                    int      age        = int.Parse(animalArgs[1]);
                    string   gender     = animalArgs[2];

                    switch (animalType)
                    {
                    case "Dog":
                        Dog dog = new Dog(name, age, gender);
                        this.animals.Add(dog);
                        break;

                    case "Cat":
                        Cat cat = new Cat(name, age, gender);
                        this.animals.Add(cat);
                        break;

                    case "Frog":
                        Frog frog = new Frog(name, age, gender);
                        this.animals.Add(frog);
                        break;

                    case "Kitten":
                        Kitten kitten = new Kitten(name, age);
                        this.animals.Add(kitten);
                        break;

                    case "Tomcat":
                        Tomcat tomcat = new Tomcat(name, age);
                        this.animals.Add(tomcat);
                        break;

                    default:
                        throw new InvalidInputException();
                    }
                }
                catch (InvalidInputException exception)
                {
                    Console.WriteLine(exception.Message);
                }
            }

            PrintAnimals();
        }
示例#28
0
    public static void Main()
    {
        var animals = new List <Animal>();

        string kindOfAnimal;

        while ((kindOfAnimal = Console.ReadLine()) != "Beast!")
        {
            var animalTokens = Console.ReadLine().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

            try
            {
                switch (kindOfAnimal.ToLower().Trim())
                {
                case "cat":
                    Animal cat = new Cat(animalTokens[0], int.Parse(animalTokens[1]), animalTokens[2]);
                    animals.Add(cat);
                    break;

                case "dog":
                    Animal dog = new Dog(animalTokens[0], int.Parse(animalTokens[1]), animalTokens[2]);
                    animals.Add(dog);
                    break;

                case "frog":
                    Animal frog = new Frog(animalTokens[0], int.Parse(animalTokens[1]), animalTokens[2]);
                    animals.Add(frog);
                    break;

                case "kitten":
                    Animal kitten = new Kitten(animalTokens[0], int.Parse(animalTokens[1]), "Female");
                    animals.Add(kitten);
                    break;

                case "tomcat":
                    Animal tomcat = new Tomcat(animalTokens[0], int.Parse(animalTokens[1]), "Male");
                    animals.Add(tomcat);
                    break;

                default:
                    Console.WriteLine("Invalid input!");
                    break;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);;
            }
        }

        foreach (var an in animals)
        {
            Console.WriteLine(an.IntroduceAnimal());
            Console.WriteLine(an.ProduceSound());
        }
    }
示例#29
0
    static void Main(string[] args)
    {
        List <Animal> animals = new List <Animal>();

        string input;

        while ((input = Console.ReadLine()) != "Beast!")
        {
            try
            {
                var tokens = Console.ReadLine().Split();

                switch (input)
                {
                case "Cat":
                    Cat cat = new Cat(tokens[0], int.Parse(tokens[1]), tokens[2], "Cat");
                    animals.Add(cat);
                    break;

                case "Dog":
                    Dog dog = new Dog(tokens[0], int.Parse(tokens[1]), tokens[2], "Dog");
                    animals.Add(dog);
                    break;

                case "Frog":
                    Frog frog = new Frog(tokens[0], int.Parse(tokens[1]), tokens[2], "Frog");
                    animals.Add(frog);
                    break;

                case "Kitten":
                    Kitten kitten = new Kitten(tokens[0], int.Parse(tokens[1]), tokens[2], "Kitten");
                    animals.Add(kitten);
                    break;

                case "Tomcat":
                    Tomcat tomcat = new Tomcat(tokens[0], int.Parse(tokens[1]), tokens[2], "Tomcat");
                    animals.Add(tomcat);
                    break;

                default:
                    throw new ArgumentException("Invalid input!");
                }
            }
            catch (ArgumentException ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        foreach (var animal in animals)
        {
            Console.WriteLine(animal.Type);
            Console.WriteLine($"{animal.Name} {animal.Age} {animal.Gender}");
            Console.WriteLine(animal.ProduceSound());
        }
    }
示例#30
0
    static void Main(string[] args)
    {
        var animals = new List <Animal>();

        string input;

        while ((input = Console.ReadLine()) != "Beast!")
        {
            string[] animalArg = Console.ReadLine().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            string   name      = animalArg[0];
            int      age       = int.Parse(animalArg[1]);
            string   gender    = animalArg[2];
            try
            {
                Animal animal;
                switch (input)
                {
                case "Dog":
                    animal = new Dog(name, age, gender);
                    animals.Add(animal);
                    break;

                case "Cat":
                    animal = new Cat(name, age, gender);
                    animals.Add(animal);
                    break;

                case "Frog":
                    animal = new Frog(name, age, gender);
                    animals.Add(animal);
                    break;

                case "Kitten":
                    animal = new Kitten(name, age, gender);
                    animals.Add(animal);
                    break;

                case "Tomcat":
                    animal = new Tomcat(name, age, gender);
                    animals.Add(animal);
                    break;

                default:
                    throw new Exception();
                }
            }
            catch (Exception)
            {
                Console.WriteLine("Invalid input!");
            }
        }
        foreach (var item in animals)
        {
            Console.WriteLine(item);
        }
    }
示例#31
0
    public static void Main()
    {
        string animalName = Console.ReadLine();
        string line       = Console.ReadLine();

        while (animalName != "Beast!" && line != null)
        {
            string[] animalInfo = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

            var animal = new Animal();

            try
            {
                switch (animalName.ToLower())
                {
                case "cat":
                    animal = new Cat(animalInfo[0], animalInfo[1], animalInfo[2]);
                    Console.WriteLine(animalName);
                    Console.WriteLine(animal.ToString());
                    break;

                case "dog":
                    animal = new Dog(animalInfo[0], animalInfo[1], animalInfo[2]);
                    Console.WriteLine(animalName);
                    Console.WriteLine(animal.ToString());
                    break;

                case "frog":
                    animal = new Frog(animalInfo[0], animalInfo[1], animalInfo[2]);
                    Console.WriteLine(animalName);
                    Console.WriteLine(animal.ToString());
                    break;

                case "kitten":
                    animal = new Kitten(animalInfo[0], animalInfo[1], "Female");
                    Console.WriteLine(animalName);
                    Console.WriteLine(animal.ToString());
                    break;

                case "tomcat":
                    animal = new Tomcat(animalInfo[0], animalInfo[1], "Male");
                    Console.WriteLine(animalName);
                    Console.WriteLine(animal.ToString());
                    break;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return;
            }

            animalName = Console.ReadLine();
            line       = Console.ReadLine();
        }
    }
示例#32
0
    public static void Main()
    {
        List <Animal> animals = new List <Animal>();

        while (true)
        {
            string type = Console.ReadLine();

            if (type == "Beast!")
            {
                break;
            }

            string[] info = Console.ReadLine().Split();

            try
            {
                type = type.ToLower();

                if (type == "cat")
                {
                    var cat = new Cat(info[0], int.Parse(info[1]), info[2]);
                    animals.Add(cat);
                }
                else if (type == "dog")
                {
                    var dog = new Dog(info[0], int.Parse(info[1]), info[2]);
                    animals.Add(dog);
                }
                else if (type == "frog")
                {
                    var frog = new Frog(info[0], int.Parse(info[1]), info[2]);
                    animals.Add(frog);
                }
                else if (type == "tomcat")
                {
                    var tomcat = new Tomcat(info[0], int.Parse(info[1]));
                    animals.Add(tomcat);
                }
                else if (type == "kitten")
                {
                    var kitten = new Kittens(info[0], int.Parse(info[1]));
                    animals.Add(kitten);
                }
            }
            catch (Exception ae)
            {
                Console.WriteLine(ae.Message);
            }
        }

        foreach (var animal in animals)
        {
            Console.WriteLine(animal);
        }
    }
示例#33
0
    public static Tomcat[] FillTomArr()
    {
        var tomArr = new Tomcat[rnd.Next(5, 21)];

        for (int i = 0; i < tomArr.Length; i++)
        {
            tomArr[i] = new Tomcat(GetRandomName(), rnd.Next(1, 16));
        }
        return(tomArr);
    }
示例#34
0
    public static void Main()
    {
        string type = Console.ReadLine();

        while (type != "Beast!")
        {
            string[] animalArgs = Console.ReadLine().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            Animal   animal;

            try
            {
                string name   = animalArgs[0];
                string age    = animalArgs[1];
                string gender = string.Empty;

                switch (type)
                {
                case "Dog":
                    gender = animalArgs[2];
                    animal = new Dog(name, age, gender);
                    break;

                case "Cat":
                    gender = animalArgs[2];
                    animal = new Cat(name, age, gender);
                    break;

                case "Frog":
                    gender = animalArgs[2];
                    animal = new Frog(name, age, gender);
                    break;

                case "Kitten":
                    animal = new Kitten(name, age);
                    break;

                case "Tomcat":
                    animal = new Tomcat(name, age);
                    break;

                default:
                    animal = new Cat(null, null, null);
                    break;
                }

                PrintAnimal(animal);
            }
            catch (Exception)
            {
                Console.WriteLine("Invalid input!");
            }

            type = Console.ReadLine();
        }
    }
示例#35
0
    static void Main()
    {
        // Animal an00 = new Animal("animal", 99, Sex.Male); //- it is not possible to initialize Animal - the class is abstract

        Frog an01 = new Frog("Jabka", 1, Sex.Female);
        Console.Write("sound of a frog is:");
        an01.Makesound();
        Dog an02 = new Dog("Balkan", 5, Sex.Male);
        Console.Write("sound of a dog is:");
        an02.Makesound();
        Cat an03 = new Cat("Muro", 2, Sex.Male); // Cat could be male or female
        Cat an04 = new Cat("Izabela", 4, Sex.Female); // Cat could be male or female
        Console.Write("sound of a cat is:");
        an04.Makesound();
        Kitten an05 = new Kitten("Chochi", 2);  //Kitten is alwais female
        Console.WriteLine("Kitten's sex is {0}", an05.Sex);
        Console.Write("sound of a kitten is:");
        an05.Makesound();
           // an05.Sex = Sex.Male; //It is not possible to access/change the sex of an Animal
        Tomcat an06 = new Tomcat("Gosho", 5);
        Console.WriteLine("Tomcat's sex is {0}", an06.Sex);
        Console.Write("sound of a tomcat is:");
        an06.Makesound();

        Dog an07 = new Dog("Lara", 8, Sex.Female);
        Dog an08 = new Dog("Sharo", 3, Sex.Male);
        Frog an09 = new Frog("Cermit", 2, Sex.Male);
        Kitten an10 = new Kitten("Suzi", 5);
        Tomcat an11 = new Tomcat("Tom", 2);

        Frog[] frogs = { an01, an09 };
        Console.WriteLine("The average age of frogs is {0:f2}", Animal.AverageAge(frogs)); //Static method in class Animal
        Console.WriteLine("The average age of frogs calculated by lambda is {0:f2}",frogs.Average(x=>x.Age));

        Dog[] dogs = { an02, an07, an08};
        Console.WriteLine("The average age of dogs is {0:f2}", Animal.AverageAge(dogs));
        Console.WriteLine("The average age of dogs calculated by lambda is {0:f2}", dogs.Average(x => x.Age));

        Cat[] allKindOfCats = { an03, an04, an05, an06, an10, an11};
        Console.WriteLine("The average age of all kind of cats is {0:f2}", Animal.AverageAge(allKindOfCats));
        Console.WriteLine("The average age of all kin of cats calculated by lambda is {0:f2}", allKindOfCats.Average(x => x.Age));

        Console.WriteLine();
        //Mix array
        Animal[] allAnimals = { an01,an02,an03, an04,an05, an06, an07, an08, an09, an10, an11};
        //split mixed aray to groups
        var groups = from kind in allAnimals
                     group kind by kind.GetType() into species
                     select species;
        //calculate and print average age by groups
        foreach (var item in groups)
        {
            Console.WriteLine("Kind ->{0, -10}; Average age ->{1:f2}", item.Key.Name, item.Average(x => x.Age));
        }
    }
    static void Main()
    {
        Dog[] dogs = new Dog[]
        {
            new Dog(5, "Sharo", 'm'),
            new Dog(8, "Johnny", 'm'),
            new Dog(2, "Brian", 'm')
        };

        Frog[] frogs = new Frog[]
        {
            new Frog(1, "Kikirica", 'f'),
            new Frog(2, "Jaborana", 'f')
        };

        Cat[] cats = new Cat[]
        {
            new Cat(5, "Pisana", 'f'),
            new Cat(3, "Stefka", 'f'),
            new Cat(7, "Roshko", 'm'),
            new Cat(4, "Maca", 'f')
        };

        Kitten[] kittens = new Kitten[]
        {
            new Kitten(1, "Pisana"),
            new Kitten(1, "Belosnejka"),
            new Kitten(1, "Cecka")
        };

        Tomcat[] tomcats = new Tomcat[]
        {
            new Tomcat(14, "Tom"),
            new Tomcat(7, "Rich"),
            new Tomcat(3, "Roshko 2")
        };

        Console.WriteLine("Average age of Dogs: {0}", AverageAge(dogs));
        Console.WriteLine("Average age of Frogs: {0}", AverageAge(frogs));
        Console.WriteLine("Average age of Cats: {0}", AverageAge(cats));
        Console.WriteLine("Average age of Kittens: {0}", AverageAge(kittens));
        Console.WriteLine("Average age of Tomcats: {0}", AverageAge(tomcats));

        Console.WriteLine("Sounds:");
        Console.Write("\tThe dog says: ");
        dogs[0].MakeSound();
        Console.Write("\tThe frog says: ");
        frogs[0].MakeSound();
        Console.Write("\tThe cat says: ");
        cats[0].MakeSound();
        Console.Write("\tThe kitten says: ");
        kittens[0].MakeSound();
        Console.Write("\tThe tomcat says: ");
        tomcats[0].MakeSound();
    }
示例#37
0
    static void Main(string[] args)
    {
        string type;

        while (!(type = Console.ReadLine()).Equals("Beast!"))
        {
            var tokens = Console.ReadLine()
                         .Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

            try
            {
                if (tokens.Length != 3)
                {
                    throw new ArgumentException("Invalid input!");
                }

                Animal animal = null;
                switch (type)
                {
                case "Cat":
                    animal = new Cat(tokens[0], int.Parse(tokens[1]), tokens[2]);
                    break;
                    ;

                case "Dog":
                    animal = new Dog(tokens[0], int.Parse(tokens[1]), tokens[2]);
                    break;

                case "Frog":
                    animal = new Frog(tokens[0], int.Parse(tokens[1]), tokens[2]);
                    break;
                    ;

                case "Kitten":
                    animal = new Kitten(tokens[0], int.Parse(tokens[1]), tokens[2]);
                    break;

                case "Tomcat":
                    animal = new Tomcat(tokens[0], int.Parse(tokens[1]), tokens[2]);
                    break;

                default:
                    throw new ArgumentException("Invalid input!");
                }
                Console.WriteLine(animal);
            }
            catch (ArgumentException e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }
示例#38
0
        static void Main()
        {
            Animal[] dogArray = new Animal[5];
            dogArray[0] = new Dog("Sharo", 4, "male");
            dogArray[1] = new Dog("Rex", 1, "male");
            dogArray[2] = new Dog("Lasi", 10, "male");
            dogArray[3] = new Dog("Djinka", 3, "female");
            dogArray[4] = new Dog("Princess", 2, "female");

            foreach (var animal in dogArray)
            {
                Console.WriteLine(animal);
            }

            var dogArrayAvg = dogArray.Average(d => d.Age);
            Console.WriteLine("Average dog age: {0:F2}{1}",dogArrayAvg, Environment.NewLine);
            Console.WriteLine();


            Cat[] catArray = new Cat[5];
            catArray[0] = new Kitten("Mici", 3);
            catArray[1] = new Tomcat("Tom", 5);
            catArray[2] = new Kitten("Grozdanka", 2);
            catArray[3] = new Tomcat("Grozdan", 10);
            catArray[4] = new Kitten("Micana", 7);

            foreach (var cat in catArray)
            {
                Console.WriteLine(cat);
            }

            var catArrayAvg = catArray.Average(c => c.Age);
            Console.WriteLine("Average cat age: {0:F2}{1}",catArrayAvg, Environment.NewLine);
            Console.WriteLine();


            Frog[] frogArray = new Frog[5];
            frogArray[0] = new Frog("Kyrmit", 4, "male");
            frogArray[1] = new Frog("Kyrmita", 2, "female");
            frogArray[2] = new Frog("Kyrmin", 6, "male");
            frogArray[3] = new Frog("Kyrmina", 3, "female");
            frogArray[4] = new Frog("Kyrtin", 8, "male");

            foreach (var frog in frogArray)
            {
                Console.WriteLine(frog);
            }

            var frogArrayAvg = frogArray.Average(f => f.Age);
            Console.WriteLine("Average frog age: {0:F2}{1}", frogArrayAvg, Environment.NewLine);
            Console.WriteLine();
        }
    static void Main()
    {
        Cat[] cats = new Cat[5];
        cats[0] = new Tomcat("Murry", 2);
        cats[1] = new Kitten("Glori", 4, "January");
        cats[2] = new Tomcat("Ico", 3, "Orange", "Mice");
        cats[3] = new Kitten("Katya", 2, "March");
        cats[4] = new Tomcat("Tom", 5);

        Console.WriteLine("The average age of the cats is: " + cats.Average(c => c.Age));

        Dog doggy = new Dog("Persin", 6, "Male", "English Pointer");
        doggy.ProduceSound();
    }
示例#40
0
        static void Main(string[] args)
        {
            _animalsArr = new Animal[5];

            _animalsArr[0] = new Frog("Froggy", 3, 'm');
            _animalsArr[1] = new Dog("Kudgo", 10, 'm');
            _animalsArr[2] = new Kitten("Pussy", 3);
            _animalsArr[3] = new Tomcat("Kotaran", 4);
            _animalsArr[4] = new Dog("Big Bertha", 11, 'f');

            foreach (var a in _animalsArr.GroupBy(x => x.GetType().Name))
            {
                Console.WriteLine("Animal: {0}\nAvarage Age: {1}\n",
                    a.Key, a.Select(x => x.Age).Average());
            }
        }
示例#41
0
    static void Main()
    {
        Dog[] dogArray = new Dog[10];
        for (int i = 0; i < 10; i++)
        {
            dogArray[i] = new Dog(0, "Rover", "Male");
            dogArray[i].SetRandomAge(0, 15);
        }
        Console.WriteLine("Average years of the Dog array: " + Animal.AverageAge(dogArray));
        Console.WriteLine();

        Frog[] frogArray = new Frog[10];
        for (int i = 0; i < 10; i++)
        {
            frogArray[i] = new Frog(0, "Wib", "Male");
            frogArray[i].SetRandomAge(0, 20);
        }
        Console.WriteLine("Average years of the Frog array: " + Animal.AverageAge(frogArray));
        Console.WriteLine();

        Cat[] catArray = new Cat[10];
        for (int i = 0; i < 10; i++)
        {
            catArray[i] = new Cat(0, "Whiskers", "Male");
            catArray[i].SetRandomAge(0, 22);
        }
        Console.WriteLine("Average years of the Cat array: " + Animal.AverageAge(catArray));
        Console.WriteLine();

        Tomcat[] tomcatArray = new Tomcat[10];
        for (int i = 0; i < 10; i++)
        {
            tomcatArray[i] = new Tomcat(0, "Tom");
            tomcatArray[i].SetRandomAge(0, 22);
        }
        Console.WriteLine("Average years of the Tomcat array: " + Animal.AverageAge(tomcatArray));
        Console.WriteLine();

        Kitten[] kittenArray = new Kitten[10];
        for (int i = 0; i < 10; i++)
        {
            kittenArray[i] = new Kitten(0, "Felicity");
            kittenArray[i].SetRandomAge(0, 22);
        }
        Console.WriteLine("Average years of the Kitten array: " + Animal.AverageAge(kittenArray));
        Console.WriteLine();
    }
示例#42
0
        static void Main(string[] args)
        {
            Cat[] cats = new Cat[]
            {
                new Cat("Aira", 2, Sex.female),
                new Cat("Roni", 6, Sex.male),
                new Cat("Ketty", 3, Sex.female),
            };

            Console.WriteLine(Animal.AverageAge(cats));

            Dog[] dogs = new Dog[]
            {
                new Dog("Rijko", 4, Sex.male),
                new Dog("Roni", 5, Sex.male),
                new Dog("Molly", 3, Sex.female),
            };

            Console.WriteLine(Animal.AverageAge(dogs));

            Kitten[] kittens = new Kitten[]
            {
                new Kitten("Kremi", 3),
                new Kitten("Maca", 1),
                new Kitten("Murja", 8),
            };

            Console.WriteLine(Animal.AverageAge(kittens));

            Tomcat[] tomcats = new Tomcat[]
            {
                new Tomcat("Rijka", 2),
                new Tomcat("Bojka", 1),
                new Tomcat("Sara", 10),
            };

            Console.WriteLine(Animal.AverageAge(tomcats));

            Frog[] frogs = new Frog[]
            {
                new Frog("Ash", 1, Sex.female),
                new Frog("Hrk", 1, Sex.female),
                new Frog("JKl", 1, Sex.female),
            };

            Console.WriteLine(Animal.AverageAge(frogs));
        }
示例#43
0
        static void Main(string[] args)
        {
            Dog firstDog = new Dog(2, "Sharo");
            Dog secondDog = new Dog(6, "Djaro");
            Dog thirdDog = new Dog(1, "Tobi");
            Dog fourthDog = new Dog(5, "Roni");

            Dog[] dogs = new Dog[]
            {
                firstDog, secondDog, thirdDog, fourthDog
            };

            Console.WriteLine("The average age of the dogs is: {0}", Animal.GetAverageAge(dogs));
            firstDog.MakeSound();
            Console.WriteLine();

            Cat firstCat = new Kitten(1, "Kitty");
            Cat secondCat = new Tomcat(2, "Bojko");
            Cat thirdCat = new Cat(4, "Myrzel", "male");

            Cat[] cats = new Cat[] 
            {
                firstCat,
                secondCat,
                thirdCat
            };

            Console.WriteLine("The average age of the cats is: {0}", Animal.GetAverageAge(cats));
            Console.WriteLine();
            
            Console.WriteLine("The kitten's name is: {0} ", firstCat.GetName());
            Console.WriteLine("The kitten's age is: {0}", firstCat.GetAge());
            Console.WriteLine("All kittens are {0}", firstCat.Sex);
            firstCat.MakeSound();
            Console.WriteLine();

            Console.WriteLine("The tomcat's name is: {0}", secondCat.GetName());
            Console.WriteLine("All tomcats are {0}", secondCat.Sex);
            secondCat.MakeSound();

            thirdCat.MakeSound();
            Console.WriteLine();

            Frog froggy = new Frog(22, "Prince");
            froggy.MakeSound();

        }
示例#44
0
    private static void Main()
    {
        Animal[] animalArray =
        {
            new Dog("Lucky", 6, "Male"),
            new Dog("Bengy", 8, "Male"),
            new Cat("Leya", 3, "Female"),
            new Tomcat("Tom", 2),
            new Dog("Ronald", 7, "Male"),
            new Frog("Jerry", 2, "Male"),
            new Kitten("Samara", 1),
            new Tomcat("Bobby", 5),
            new Kitten("Yasmin", 3),
            new Frog("Princess", 9, "Female"),
            new Dog("Kosio", 12, "Male"),
            new Frog("Betty", 1, "Female")

        };
        Cat tomcat = new Tomcat("Robbie", 4);
        tomcat.ProduceSound();
        Console.WriteLine();

        List<Animal> animals = new List<Animal>();
        animals.AddRange(animalArray);

        double averageDogAge = animals
            .Where(x => x.GetType().ToString() == "Dog")
            .Select(x => x.Age)
            .Average();

        double averageCatAge = animals
            .Where(x => x.GetType().ToString() == "Cat" ||
                        x.GetType().ToString() == "Tomcat" ||
                        x.GetType().ToString() == "Kitten")
            .Select(x => x.Age)
            .Average();

        double averageFrogAge = animals
            .Where(x => x.GetType().ToString() == "Frog")
            .Select(x => x.Age)
            .Average();

        Console.WriteLine("Average Dog Age = " + averageDogAge);
        Console.WriteLine("Average Cat Age = " + averageCatAge);
        Console.WriteLine("Average Frog Age = " + averageFrogAge);
    }
        static void Main()
        {
            Frog f1 = new Frog("Billy", 0.2, "m");
            Dog d1 = new Dog("Liza", 4, "f");
            Cat c1 = new Cat("Kotaksi", 2, "m");
            Kitten k1 = new Kitten("Raya", 1);
            Tomcat t1 = new Tomcat("Rijo", 2);

            f1.ProduceSound();
            d1.ProduceSound();
            c1.ProduceSound();
            k1.ProduceSound();
            t1.ProduceSound();

            Console.WriteLine();

            Animal[] animals = new Animal[]
            {
                new Frog("Billy", 0.2, "m"),
                new Dog("Liza", 4, "f"),
                new Cat("Kotaksi", 2, "m"),
                new Kitten("Raya", 1),
                new Tomcat("Rijo", 2),
                new Frog("Pesho", 2.1, "f"),
                new Dog("Beti", 5.4, "f"),
                new Cat("Kotka", 2, "f"),
                new Kitten("Spaska", 4),
                new Tomcat("Gosho", 3),
                new Frog("Marmot", 4, "m"),
                new Tomcat("Bizen", 0.5),
                new Dog("India", 2.5, "f"),
            };

            animals.
                GroupBy(animal => animal.GetType().Name).
                Select(group => new
                {
                    name = group.Key,
                    averageYears = group.Average(animal => animal.Age)
                }).
                ToList().
                ForEach(group => Console.WriteLine("Group: {0}, average age: {1:F2} years!", group.name, group.averageYears));
        }
        public static void Main()
        {
            var kittenTea = new Kitten("Tea", 2);
            var tomcatRoko = new Tomcat("Roko", 4);
            var kittenCeca = new Kitten("Ceca", 1);

            var dogBalkan = new Dog("Balkan", 3, Gender.Male);
            var dogSara = new Dog("Sara", 2, Gender.Female);

            var frogKermit = new Frog("Kermit", 1, Gender.Male);
            var frogCarlotta = new Frog("Carlotta", 2, Gender.Female);

            var animals = new Animal[] { kittenTea, tomcatRoko, kittenCeca, dogBalkan, dogSara, frogKermit, frogCarlotta };

            Animal.PrintAverageAge(animals);

            foreach (var animal in animals)
            {
                animal.ProduceSound();
            }
        }
 static void Main()
 {
     Frog kerm=new Frog("Kerm",4,"male");
     kerm.ProduceSound();
     Tomcat tommy=new Tomcat("Tommy",8,"");
     tommy.ProduceSound();
     Animal[] animals=new Animal[]{new Dog("SnoopDog",5,"male"),new Kitten("Mia",2,""),new Tomcat("Tom",5,""),new Frog("Kermit",12,"male"),new Dog("Max",1,"male"),    };
     foreach (var animal in animals)
     {
         Console.WriteLine(animal);
     }
     //AvarageAgeOfAll\\
     double avarageOfAll= animals.Average(animal => animal.Age);
     Console.WriteLine("Avarage: {0}",avarageOfAll);
     //AvarageOfDog\\
     double avarageOfDog = animals.OfType<Dog>().Average(dog => dog.Age);
     Console.WriteLine("Avarage Dog:{0}",avarageOfDog);
     //AvarageOfCat\\
     double avarageOfCat = animals.OfType<Cat>().Average(cat => cat.Age);
     Console.WriteLine("Avarage Cat:{0}",avarageOfCat);
 }
示例#48
0
    static void Main()
    {
        Dog[] dogs = new Dog[]
        {
            new Dog("Sharo", 5, Gender.Male),
            new Dog("Rex", 8, Gender.Male)
        };

        Frog[] frogs = new Frog[]
        {
            new Frog("Grogo", 1, Gender.Male),
            new Frog("Frogina", 3, Gender.Female)
        };

        Kitten[] kittens = new Kitten[]
        {
            new Kitten("Fluffy", 5),
            new Kitten("Patrisha", 2)
        };

        Tomcat[] tomcats = new Tomcat[]
        {
            new Tomcat("Tom", 3),
            new Tomcat("Johnny", 5)
        };

        List<Animal[]> animals = new List<Animal[]>();
        animals.Add(dogs);
        animals.Add(frogs);
        animals.Add(kittens);
        animals.Add(tomcats);

        Console.WriteLine("Average age by animal type:");
        foreach (var animalArray in animals)
        {
            Console.WriteLine("{0}:\t{1};",
                animalArray[0].GetType(), //assumes non-empty arrays
                GetAverageAge(animalArray));
        }
    }
示例#49
0
    static void Main()
    {
        var tom = new Tomcat("Tommas", 3, "m");
        var bruno = new Tomcat("Bruno", 1, "male");
        var spike = new Dog("Spike", 4, "m");
        var dog = new Dog("Бенджи", 4, "male");
        var frog =  new Frog("Veso", 1, "female");
        var anotherFrog = new Frog("Sasho", 2, "m");
        var kitten = new Kitten("Kote", 2, "female");
        var cat = new Cat("Маца", 5, "f");
        var anotherKitty = new Kitten("Penka", 4, "female");

        Animal[] animals = new Animal[]
           {
           tom, dog, frog, kitten, cat, spike, bruno, anotherFrog, anotherKitty
           };

        foreach (var kind in animals.GroupBy(animal => animal.GetType()))
        {
            double averageAge = kind.Average(animal => (double)animal.Age);
            Console.WriteLine("{0} - average age: {1}", kind.Key, averageAge);
        }
    }
    static void Main()
    {
        Dog dog = new Dog("Sparkie", 18, Sex.Male);
        Console.WriteLine(dog.ToString());

        Frog frog = new Frog("Kermit", 15, Sex.Male);
        Console.WriteLine(frog.ToString());

        Cat cat = new Cat("TheBigCat", 6, Sex.Female);
        Console.WriteLine(cat.ToString());

        Kitten kitten = new Kitten("Kittie", 1, Sex.Female);
        //Kitten kitten = new Kitten("Kittie", 1, Sex.Male); //incorrect
        Console.WriteLine(kitten.ToString());

        Tomcat tomcat = new Tomcat("Tom", 5, Sex.Male);
        //Tomcat tomcat = new Tomcat("Tom", 5, Sex.Female); // incorrect
        Console.WriteLine(tomcat.ToString());

        Console.WriteLine();
        dog.ProduceSound();
        frog.ProduceSound();
        cat.ProduceSound();
        kitten.ProduceSound();
        tomcat.ProduceSound();
        Console.WriteLine();

        List<Dog> dogs = new List<Dog>();
        dogs.Add(new Dog("Sharo", 2, Sex.Male));
        dogs.Add(new Dog("Rex", 5, Sex.Male));
        dogs.Add(new Dog("Roshla", 3, Sex.Female));
        dogs.Add(dog);

        double averageAge = dogs.Average(x => x.Age);
        Console.WriteLine("Average age of dogs is {0}.", averageAge);

        List<Frog> frogs = new List<Frog>();
        frogs.Add(new Frog("Toad", 12, Sex.Male));
        frogs.Add(new Frog("Big Toad", 8, Sex.Male));
        frogs.Add(new Frog("Ugly Frog", 3, Sex.Female));
        frogs.Add(frog);

        averageAge = frogs.Average(x => x.Age);
        Console.WriteLine("Average age of frogs is {0}.", averageAge);

        List<Cat> cats = new List<Cat>();
        cats.Add(new Cat("TopCat", 10, Sex.Male));
        cats.Add(new Cat("Black Cat", 7, Sex.Male));
        cats.Add(new Cat("Kittle", 4, Sex.Female));
        cats.Add(cat);

        averageAge = cats.Average(x => x.Age);
        Console.WriteLine("Average age of cats is {0}.", averageAge);

        List<Kitten> kittens = new List<Kitten>();
        kittens.Add(new Kitten("Huggie", 1, Sex.Female));
        kittens.Add(new Kitten("Pussycat", 2.5, Sex.Female));
        kittens.Add(new Kitten("Cuttie", 6, Sex.Female));
        kittens.Add(kitten);

        averageAge = kittens.Average(x => x.Age);
        Console.WriteLine("Average age of kittens is {0}.", averageAge);

        List<Tomcat> tomcats = new List<Tomcat>();
        tomcats.Add(new Tomcat("Dirty Puss", 4, Sex.Male));
        tomcats.Add(new Tomcat("Gray Tomcat", 8.5, Sex.Male));
        tomcats.Add(new Tomcat("Blackie", 12.5, Sex.Male));
        tomcats.Add(tomcat);

        averageAge = tomcats.Average(x => x.Age);
        Console.WriteLine("Average age of tomcats is {0}.", averageAge);

        Console.WriteLine();
    }
 static void Main()
 {
     //creating array with cats
     Console.WriteLine("--------------------------------------------------------");
     Console.WriteLine("Cat");
     Cat cat1 = new Cat("catOne", 13, 'f');
     Cat cat2 = new Cat("Tom", 22, 'm');
     Cat cat3 = new Cat("CatThree", 1, 'm');
     Cat[] arrCat = new Cat[3];
     arrCat[0] = cat1;
     arrCat[1] = cat2;
     arrCat[2] = cat3;
     //avarage age for 3 cats
     double result = Animal.AvarageAge(arrCat);
     Console.WriteLine(result);
     //creating array with dogs
     Console.WriteLine("----------------------------------------------------------");
     Console.WriteLine("Dogs");
     Dog dog1 = new Dog("dogOne", 13, 'f');
     Dog dog2 = new Dog("dogTom", 9, 'm');
     Dog dog3 = new Dog("Dog3", 1, 'm');
     Dog[] arrDog = new Dog[3];
     arrDog[0] = dog1;
     arrDog[1] = dog2;
     arrDog[2] = dog3;
     //avarage age for 3 cats
     double result2 = Animal.AvarageAge(arrDog);
     Console.WriteLine(result2);
     //creat frogs
     Console.WriteLine("--------------------------------------------------------");
     Console.WriteLine("Frog");
     Frog frog1 = new Frog("frogOne", 1, 'f');
     Frog frog2 = new Frog("frogTom", 22, 'm');
     Frog frog3 = new Frog("frog3", 1, 'm');
     Frog[] arrFrog = new Frog[3];
     arrFrog[0] = frog1;
     arrFrog[1] = frog2;
     arrFrog[2] = frog3;
     //avarage age for 3 cats
     double result3 = Animal.AvarageAge(arrDog);
     Console.WriteLine(result3);
     //creat kitten
     Console.WriteLine("--------------------------------------------------------");
     Console.WriteLine("Kitten");
     Kitten kitten1 = new Kitten("frogOne", 13);
     Kitten kitten2 = new Kitten("frogTom", 22);
     Kitten kitten3 = new Kitten("frog3", 19);
     Kitten[] arrKitten =new  Kitten[3];
     arrKitten[0] = kitten1;
     arrKitten[1] = kitten2;
     arrKitten[2] = kitten3;
     //avarage age for 3 cats
     double result4 = Animal.AvarageAge(arrDog);
     Console.WriteLine(result4);
     //creat tomcat
     Console.WriteLine("--------------------------------------------------------");
     Console.WriteLine("Tomcat");
     Tomcat tomcat1 = new Tomcat("frogOne", 13);
     Tomcat tomcat2 = new Tomcat("frogTom", 2);
     Tomcat tomcat3 = new Tomcat("frog3", 1);
     Tomcat[] arrTomcat = new Tomcat[3];
     arrTomcat[0] = tomcat1;
     arrTomcat[1] = tomcat2;
     arrTomcat[2] = tomcat3;
     //avarage age for 3 cats
     double result5 = Animal.AvarageAge(arrDog);
     Console.WriteLine(result5);
     //sounds
     tomcat1.Say("mqu mqu");
 }
示例#52
0
 public void TestAnimalConstructor6_ThrowsException()
 {
     Animal tomcat = new Tomcat(null, 8);
 }
示例#53
0
        public void TestAnimalConstructor5()
        {
            Animal tomcat = new Tomcat("Hoho", 8);

            Assert.AreEqual(true, tomcat.IsMale);
        }
示例#54
0
    static void Main()
    {
        Animals[] someNoise = new Animals[]
        {
            new Dog(12, "Sharo", Sex.male),
            new Cat(5, "Tomi", Sex.male),
            new Frog(1, "Krqkla", Sex.female),
            new Kitten(0.6, "Siva", Sex.female),
            new Tomcat(0.2, "Drebko", Sex.male)
        };
        foreach(Animals animal in someNoise)
        {
            Console.Write("{0} says: ", animal.Name);
            animal.MakeSomeNoise();
        }

        Dog[] dogs = new Dog[]
        {
            new Dog(3, "Lasi", Sex.female),
            new Dog(1, "Sharo", Sex.male),
            new Dog(8, "Topcho", Sex.male),
            new Dog(4, "Djina", Sex.female)
        };
        double avg = Animals.AverageAge(dogs);
        Console.WriteLine("The average age of the dogs is {0:0.00}", avg);

        Cat[] cats = new Cat[]
        {
            new Cat(7, "Bella", Sex.female),
            new Cat(1, "Puhcho", Sex.male),
            new Cat(2, "Rijko", Sex.male),
            new Cat(3, "Siva", Sex.female)
        };
        avg = Animals.AverageAge(cats);
        Console.WriteLine("The average age of the cats is {0:0.00}", avg);

        Frog[] frogs = new Frog[]
        {
            new Frog(1.5, "Krastavelka", Sex.female),
            new Frog(2, "Zelenushko", Sex.male),
            new Frog(4.3, "Skoklio", Sex.male),
            new Frog(1, "Krqkla", Sex.female)
        };
        avg = Animals.AverageAge(frogs);
        Console.WriteLine("The average age of the frogs is {0:0.00}", avg);

        Kitten[] kittens = new Kitten[]
        {
            new Kitten(0.5, "Puhi", Sex.female),
            new Kitten(0.7, "Tigyrka", Sex.female),
            new Kitten(0.9, "Rijka", Sex.female),
            new Kitten(0.2, "Mynichka", Sex.female)
        };
        avg = Animals.AverageAge(kittens);
        Console.WriteLine("The average age of the kittens is {0:0.00}", avg);

        Tomcat[] tomcats = new Tomcat[]
        {
            new Tomcat(0.3, "Puhcho", Sex.male),
            new Tomcat(0.2, "Tigyr", Sex.male),
            new Tomcat(0.9, "Rijko", Sex.male),
            new Tomcat(1, "Chernyo", Sex.male)
        };
        avg = Animals.AverageAge(tomcats);
        Console.WriteLine("The average age of the tomcats is {0:0.00}", avg);
    }
示例#55
0
    static void Main()
    {
        // Produce sounds
        Kitten kitty = new Kitten("Pepa", 1);
        kitty.ProduceSound();

        Frog kyrmit = new Frog("Kyrmit", 3, Sex.Male);
        kyrmit.ProduceSound();

        Tomcat tom = new Tomcat("Tom", 8);
        tom.ProduceSound();

        Dog sharo = new Dog("Sharo", 15, Sex.Male);
        sharo.ProduceSound();

        Console.WriteLine();

        // Initializing list of Animals
        List<Animal> tomcats = new List<Animal>()
        {
            // Sex is always male
            new Tomcat("Tom", 8),
            new Tomcat("Bill", 9),
            new Tomcat("John", 7),
            new Tomcat("Gogo", 10),
            new Tomcat("Thomas", 11)
        };

        List<Animal> kittens = new List<Animal>()
        {
            // Sex is always female
            new Kitten("Kitty", 1),
            new Kitten("Kit", 2),
            new Kitten("Jitty", 1),
            new Kitten("Mitty", 1),
            new Kitten("Gitty", 2)
        };


        List<Animal> frogs = new List<Animal>()
        {
            new Frog("Kyrmit", 3, Sex.Male),
            new Frog("Maria", 2, Sex.Female),
            new Frog("Grigor", 3, Sex.Male),
            new Frog("Mara", 2, Sex.Female),
            new Frog("Mimi", 3, Sex.Female)
        };

        List<Animal> dogs = new List<Animal>()
        {
            new Dog("Sharo", 12, Sex.Male),
            new Dog("Billy", 14, Sex.Male),
            new Dog("Doggy", 11, Sex.Male),
            new Dog("Milka", 10, Sex.Female),
            new Dog("Mimi", 12, Sex.Female)
        };  

        // Calculating average age of animals
        double average = AverageAgeCalculator.CalculateAverageAge(tomcats);
        Console.WriteLine("Average Tomcat age is: {0}", average);

        average = AverageAgeCalculator.CalculateAverageAge(kittens);
        Console.WriteLine("Average Kitten age is: {0}", average);

        average = AverageAgeCalculator.CalculateAverageAge(frogs);
        Console.WriteLine("Average Frog age is: {0}", average);

        average = AverageAgeCalculator.CalculateAverageAge(dogs);
        Console.WriteLine("Average Dog age is: {0}", average);
    }
        /*
         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");
        }
示例#57
0
    public static void TestCreatures()
    {
        Dog charlie = new Dog("Charlie", 4, true);
        Console.WriteLine(charlie);
        charlie.ProduceSound();

        Console.WriteLine();

        Frog quackster = new Frog("Rab", 1, false);
        Console.WriteLine(quackster);
        quackster.ProduceSound();

        Console.WriteLine();

        Cat miew = new Cat("Dangleton", 3, false);
        Console.WriteLine(miew);
        miew.ProduceSound();

        Console.WriteLine();

        Kitten kitty = new Kitten("KittyCat", 3);
        Console.WriteLine(kitty);
        kitty.ProduceSound();

        Console.WriteLine();

        Tomcat tom = new Tomcat("Tom", 2);
        Console.WriteLine(tom);
        tom.ProduceSound();
    }
    // generates random animals
    public static Animal GetRandomAnimal()
    {
        seed = rnd.Next(1, 5);

        switch (seed)
        {
            case 1:
                Sex s = sexes[rnd.Next(0, sexes.Length)]; // get random sex for the animal
                // give the animal a name corresponding to its sex
                string name = s == Sex.Male ? names[rnd.Next(0, names.Length)] : femNames[rnd.Next(0, femNames.Length)];
                // create new animal
                var doge = new Dog(name, s, (uint)rnd.Next(0, maxAge));
                // add his years in the years array
                animalsCountAndAges[0] += doge.Age;
                animalsCountAndAges[1]++;
                // return the new animal
                return doge;

            case 2:
                Sex frogS = sexes[rnd.Next(0, sexes.Length)];
                string frogName = frogS == Sex.Male ? names[rnd.Next(0, names.Length)] : femNames[rnd.Next(0, femNames.Length)];

                var froge = new Frog(frogName, frogS, (uint)rnd.Next(0, maxAge));

                animalsCountAndAges[2] += froge.Age;
                animalsCountAndAges[3]++;

                return froge;

            case 3:
                var kitty = new Kitten(femNames[rnd.Next(0, femNames.Length)], (uint)rnd.Next(0, maxAge));

                animalsCountAndAges[4] += kitty.Age;
                animalsCountAndAges[5]++;

                return kitty;

            case 4:
                var tommy = new Tomcat(names[rnd.Next(0, names.Length)], (uint)rnd.Next(0, maxAge));

                animalsCountAndAges[6] += tommy.Age;
                animalsCountAndAges[7]++;

                return tommy;

            default: throw new ArgumentException("Invalid seed");
        }
    }