예제 #1
0
    static void Main(string[] args)
    {
        //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).

        //Make some animals and test their properties
        Kitten kitty = new Kitten(3, "Kitty");//the sex is aways female
        Console.WriteLine(kitty.Name + "-" + kitty.Age + "-" + kitty.sex);
        kitty.ProduceSound();
        Tomkat tom = new Tomkat(5, "Tom");//the sex is aways male
        Console.WriteLine(tom.Name + "-" + tom.Age + "-" + tom.sex);
        tom.ProduceSound();
        Dog doggy = new Dog(8, "Doggy", Sex.male);
        Console.WriteLine(doggy.Name + "-" + doggy.Age + "-" + doggy.sex);
        doggy.ProduceSound();
        Frog froggy = new Frog(1, "Froggy", Sex.female);
        Console.WriteLine(froggy.Name + "-" + froggy.Age + "-" + froggy.sex);
        froggy.ProduceSound();

        //Make array with diferent animals and calculate the average age for every animal type in the array
        Animal[] animals = {kitty,tom,froggy,doggy,
                                   new Kitten(4,"Keit"),
                                   new Tomkat(5,"Tomas"),
                                   new Dog(11,"Rex",Sex.male),
                                   new Frog(3,"Curmit",Sex.male)};
        CalculateEveryAnimalAverageAge(animals);
    }
예제 #2
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());
     }
 }
예제 #3
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();
    }
예제 #4
0
        static void Main()
        {
            Kitten violeta = new Kitten("Violeta", 24, 'F');
            violeta.ProduceSound();
            Cat violetka = new Cat("Vili", 24, 'F');
            violetka.ProduceSound();
            Dog ivan = new Dog("Ivan", 23, 'M');
            ivan.ProduceSound();

            List<Animal> animals = new List<Animal>
            {
                new Dog("Ivan", 2, 'M'),
                new Dog("Rex", 5, 'M'),
                new Dog("Laiza", 1, 'F'),
                new Frog("Jaba", 3, 'F'),
                new Frog("Chervena jaba", 15, 'M'),
                new Frog("Kiorava jaba", 7, 'F'),
                new Cat("Sivushka", 4 , 'F'),
                new Cat("Felix", 8, 'M'),
                new Tomcats("Gosho", 2, 'M'),
                new Kitten("Kitty", 1, 'F')
            };

            var cats =
                from animal in animals
                where animal is Cat
                select animal;

            Console.WriteLine("Average cats age is: {0:F2}", ((double)cats.Sum(c => c.Age) / cats.Count()));

            var dogs =
                from animal in animals
                where animal is Dog
                select animal;

            Console.WriteLine("Average dogs age is: {0:F2}", ((double)dogs.Sum(d => d.Age) / dogs.Count()));

            var frogs =
                from animal in animals
                where animal is Frog
                select animal;

            Console.WriteLine("Average cats age is: {0:F2}", ((double)frogs.Sum(f => f.Age) / frogs.Count()));
            Console.WriteLine();

            foreach (var animal in animals)
            {
                animal.ProduceSound();
            }
        }
예제 #5
0
파일: Animal.cs 프로젝트: xmdn/shelter
        public void CheckAndAddAnimal()
        {
            if (TypeOfAnimal == "Dog")
            {
                Dog someDog = new Dog(TypeOfAnimal, Name, Age, Gender);
                someDog.Display();
                someDog.ProduceSound();
            }

            if (TypeOfAnimal == "Cat")
            {
                Cat someCat = new Cat(TypeOfAnimal, Name, Age, Gender);
                someCat.Display();
                someCat.ProduceSound();
            }

            if (TypeOfAnimal == "Frog")
            {
                Frog someFrog = new Frog(TypeOfAnimal, Name, Age, Gender);
                someFrog.Display();
                someFrog.ProduceSound();
            }

            if (TypeOfAnimal == "Kitten")
            {
                Kitten someKitten = new Kitten(TypeOfAnimal, Name, Age, Gender);
                someKitten.Display();
                someKitten.ProduceSound();
            }

            if (TypeOfAnimal == "Tomcat")
            {
                Tomcat someTomcat = new Tomcat(TypeOfAnimal, Name, Age, Gender);
                someTomcat.Display();
                someTomcat.ProduceSound();
            }

            if (TypeOfAnimal == "FuturePet")
            {
                FuturePet someFuturePet = new FuturePet(TypeOfAnimal, Name, Age, Gender);
                someFuturePet.Display();
                someFuturePet.ProduceSound();
            }
        }
        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));
        }
        static void Main(string[] args)
        {
            var book = new GoldenEditionBook("Design...", "Anton", 10);

            Console.WriteLine(book);

            Cat    cat    = new Cat(true, "Mitzi", 4);
            Dog    dog    = new Dog(true, "Scharo", 6);
            Kitten kitten = new Kitten(false, "Moni", 2);
            Tomcat tomcat = new Tomcat(true, "Bojo", 3);

            cat.ProduceSound();
            Console.WriteLine(cat);
            dog.ProduceSound();
            Console.WriteLine(dog);
            kitten.ProduceSound();
            Console.WriteLine(kitten);
            tomcat.ProduceSound();
            Console.WriteLine(tomcat);
        }
예제 #8
0
    static void Main(string[] args)
    {
        //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).


        //Make some animals and test their properties
        Kitten kitty = new Kitten(3, "Kitty");//the sex is aways female

        Console.WriteLine(kitty.Name + "-" + kitty.Age + "-" + kitty.sex);
        kitty.ProduceSound();
        Tomkat tom = new Tomkat(5, "Tom");//the sex is aways male

        Console.WriteLine(tom.Name + "-" + tom.Age + "-" + tom.sex);
        tom.ProduceSound();
        Dog doggy = new Dog(8, "Doggy", Sex.male);

        Console.WriteLine(doggy.Name + "-" + doggy.Age + "-" + doggy.sex);
        doggy.ProduceSound();
        Frog froggy = new Frog(1, "Froggy", Sex.female);

        Console.WriteLine(froggy.Name + "-" + froggy.Age + "-" + froggy.sex);
        froggy.ProduceSound();

        //Make array with diferent animals and calculate the average age for every animal type in the array
        Animal[] animals = { kitty,        tom,      froggy,    doggy,
                             new Kitten(4, "Keit"),
                             new Tomkat(5, "Tomas"),
                             new Dog(11,   "Rex",    Sex.male),
                             new Frog(3,   "Curmit", Sex.male) };
        CalculateEveryAnimalAverageAge(animals);
    }
예제 #9
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);
    }
예제 #10
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();
    }
    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();
    }
예제 #12
0
    static void Main()
    {
        //Printing our beloved pets on the console:
        Dog charlie = new Dog("Charlie", 6, Gender.Male);

        Console.WriteLine(charlie);
        Cat jessica = new Cat("Jessica", 10, Gender.Female);

        Console.WriteLine(jessica);
        Frog chocho = new Frog("Chocho", 4, Gender.Male);

        Console.WriteLine(chocho);
        Kitten kitty = new Kitten("Kitty", 1);

        Console.WriteLine(kitty);
        Tomcat tommy = new Tomcat("Tommy", 7);

        Console.WriteLine(tommy);

        //Producing sounds:
        Console.WriteLine("\nAnimal concert:");
        kitty.ProduceSound();
        charlie.ProduceSound();
        jessica.ProduceSound();
        chocho.ProduceSound();
        tommy.ProduceSound();
        Console.WriteLine();

        //Creating arrays of different kind of animals and calculating their average age:
        Dog[] dogs =
        {
            new Dog("Roshko", 5, Gender.Male),
            new Dog("Rexy",   4, Gender.Male)
        };

        Kitten[] kittens =
        {
            new Kitten("Shaki", 3),
            new Kitten("Marka", 14)
        };
        Tomcat[] tomcats =
        {
            new Tomcat("Gufi",    10),
            new Tomcat("Krumcho", 5)
        };
        Frog[] frogs =
        {
            new Frog("Kiki",    4, Gender.Female),
            new Frog("Shishka", 8, Gender.Female)
        };

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

        animals.Add(charlie);
        animals.Add(jessica);
        animals.Add(chocho);
        animals.Add(tommy);
        animals.Add(kitty);
        animals.AddRange(dogs);
        animals.AddRange(kittens);
        animals.AddRange(tomcats);
        animals.AddRange(frogs);

        var groupedAnimals = from animal in animals
                             group animal by(animal is Cat) ? typeof(Cat) : animal.GetType() into g
                             select new { AnimalType = g.Key, AverageAge = g.ToList().Average(an => an.Age) };

        foreach (var groupedAnimal in groupedAnimals)
        {
            Console.WriteLine("Group: {0} --> Average age: {1:N2} years", groupedAnimal.AnimalType, groupedAnimal.AverageAge);
        }
    }
예제 #13
0
    static void Main(string[] args)
    {
        string command;

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

                string[] secondLine =
                    Console.ReadLine()
                    .Split()
                    .ToArray();

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

                if (input.Length == 1)
                {
                    string classToMake = input[0];

                    if (classToMake == "Dog")
                    {
                        Dog dog = new Dog(name, age, gender);
                        Console.WriteLine(classToMake);
                        Console.WriteLine(dog);
                        Console.WriteLine(dog.ProduceSound());
                    }
                    else if (classToMake == "Frog")
                    {
                        Frog frog = new Frog(name, age, gender);
                        Console.WriteLine(classToMake);
                        Console.WriteLine(frog);
                        Console.WriteLine(frog.ProduceSound());
                    }
                    else if (classToMake == "Cat")
                    {
                        Cat cat = new Cat(name, age, gender);
                        Console.WriteLine(classToMake);
                        Console.WriteLine(cat);
                        Console.WriteLine(cat.ProduceSound());
                    }
                    else if (classToMake == "Kitten")
                    {
                        Kitten kitten = new Kitten(name, age);
                        Console.WriteLine(classToMake);
                        Console.WriteLine(kitten);
                        Console.WriteLine(kitten.ProduceSound());
                    }
                    else if (classToMake == "Tomcat")
                    {
                        Tomcat tomcat = new Tomcat(name, age);
                        Console.WriteLine(classToMake);
                        Console.WriteLine(tomcat);
                        Console.WriteLine(tomcat.ProduceSound());
                    }
                    else
                    {
                        throw new ArgumentException("Invalid input!");
                    }
                }
            }
            catch (Exception e) {
                Console.WriteLine(e.Message);
            }
        }
    }