Loading...

Във форума е въведено ограничение, което позволява на потребителите единствено да разглеждат публикуваните въпроси.

PetroslavGochev avatar PetroslavGochev 13 Точки

06. Animals - Inheritance - Exercise

Здравейте група, имам проблем със следната задача от C# OOP Inheritance - Exercise, в джъдж ми дава 80/100 точки, като ми гърми първият тест с Incorrect answer,бих се радвал ако някой успее да ми каже къде точно греша, 

прилагам линк към кода на задачата ми: 
https://github.com/PetroslavGochev/CSharpAdvancedModule/tree/main/C%23OOP/01.Inheritance/Inheritance%20-%20Exercise/Animals

Ето и условието на задачата : 

1.Animals

NOTE: You need a public class StartUp.

Create a hierarchy of Animals. Your program should have three different animals – Dog, Frog and Cat. Deeper in the hierarchy you should have two additional classes – Kitten and Tomcat. Kittens are female and Tomcats are male. All types of animals should be able to produce some kind of sound - ProduceSound(). For example, the dog should be able to bark. Your task is to model the hierarchy and test its functionality. Create an animal of each kind and make them all produce sound.

You will be given some lines of input. Each two lines will represent an animal. On the first line will be the type of animal and on the second – the name, the age and the gender. When the command "Beast!" is given, stop the input and print all the animals in the format shown below.

Output

  • Print the information for each animal on three lines. On the first line, print: "{AnimalType}"
  • On the second line print: "{Name} {Age} {Gender}"
  • On the third line print the sounds it produces: "{ProduceSound()}"

Constraints

  • Each Animal should have a name, an age and a gender
  • All input values should not be blank (e.g. name, age and so on…)
  • If you receive an input for the gender of a Tomcat or a Kitten, ignore it but create the animal
  • If the input is invalid for one of the properties, throw an exception with message: "Invalid input!"
  • Each animal should have the functionality to ProduceSound()
  • Here is the type of sound each animal should produce:
    • Dog: "Woof!"
    • Cat: "Meow meow"
    • Frog: "Ribbit"
    • Kittens: "Meow"
    • Tomcat: "MEOW"

Examples

Input

Output

Cat

Tom 12 Male

Dog

Sharo 132 Male

Beast!

Cat

Tom 12 Male

Meow meow

Dog

Sharo 132 Male

Woof!

Frog

Kermit 12 Male

Beast!

Frog

Kermit 12 Male

Ribbit

Frog

Sashko -2 Male

Frog

Sashko 2 Male

Beast!

Invalid input!

Frog

Sashko 2 Male

Ribbit

 

Благодаря  предварително :) !

Тагове:
0
C# OOP Advanced
Axiomatik avatar Axiomatik 2422 Точки

From what I remember Kitten and Tomcat have to take up three arguments, while the validation in the bass class checks the gender for all animal types:

Kitten:

using System;
namespace Animals
{
    public class Kitten : Cat
    {
        public Kitten(string name, int age, string gender)
            : base(name, age, gender)
        {
        }

        public override string ProduceSound()
        {
            return "Meow";
        }
    }
}

Tomcat:

using System;
namespace Animals
{
    public class Tomcat : Cat
    {
        public Tomcat(string name, int age, string gender)
            : base(name, age, gender)
        {
        }

        public override string ProduceSound()
        {
            return "MEOW";
        }
    }
}

Animal:

using System;
using System.Text;

namespace Animals
{
    public abstract class Animal
    {
        private string name;

        private int age;

        private string gender;

        public Animal(string name, int age, string gender)
        {
            this.Name = name;
            this.Age = age;
            this.Gender = gender;
        }

        public string Name
        {
            get
            {
                return this.name;
            }
            set
            {
                if (char.IsDigit(value[0]) == true)
                {
                    throw new ArgumentException("Invalid input!");
                }
                this.name = value;
            }
        }

        public int Age
        {
            get
            {
                return this.age;
            }
            set
            {
                if (value <= 0)
                {
                    throw new ArgumentException("Invalid input!");
                }
                this.age = value;
            }
        }

        public string Gender
        {
            get
            {
                return this.gender;
            }
            set
            {
                if (value == "Male" || value == "Female")
                {
                    this.gender = value;
                }
                else
                {
                    throw new ArgumentException("Invalid input!");
                }
            }
        }

        public virtual string ProduceSound()
        {
            return string.Empty;
        }

        public override string ToString()
        {
            var stringBuilder = new StringBuilder();
            stringBuilder.AppendLine(this.GetType().Name);
            stringBuilder.AppendLine($"{this.Name} {this.Age} {this.Gender}");
            stringBuilder.AppendLine(this.ProduceSound());

            return stringBuilder.ToString().TrimEnd();
        }
    }
}

Best,

0
Можем ли да използваме бисквитки?
Ние използваме бисквитки и подобни технологии, за да предоставим нашите услуги. Можете да се съгласите с всички или част от тях.
Назад
Функционални
Използваме бисквитки и подобни технологии, за да предоставим нашите услуги. Използваме „сесийни“ бисквитки, за да Ви идентифицираме временно. Те се пазят само по време на активната употреба на услугите ни. След излизане от приложението, затваряне на браузъра или мобилното устройство, данните се трият. Използваме бисквитки, за да предоставим опцията „Запомни Ме“, която Ви позволява да използвате нашите услуги без да предоставяте потребителско име и парола. Допълнително е възможно да използваме бисквитки за да съхраняваме различни малки настройки, като избор на езика, позиции на менюта и персонализирано съдържание. Използваме бисквитки и за измерване на маркетинговите ни усилия.
Рекламни
Използваме бисквитки, за да измерваме маркетинг ефективността ни, броене на посещения, както и за проследяването дали дадено електронно писмо е било отворено.