Професионална програма
Loading...
+ Нов въпрос
PetroslavGochev avatar PetroslavGochev 12 Точки

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 1244 Точки

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