Jake Cube Jake Cube - 1 month ago 9
C# Question

C# OOP Print Result

I'm new to the c# in visual studio, i'm trying to use OOP that print the result
I want to print the result as:


Hello. I am a cocker spaniel. Woof! I'm (Big or Small), Colour is
white, I have 4 legs.


Since the value i set is 10 so it will always be small...

Here is my code:

public Dog()
{
barkSound = "Woof!";
breed = "cocker spaniel";
dogHeight = 10;
dogColour = "white";
noOfLEgs = 4;
}

public string GetSpeech()
{
dogSpeech = "Hello. I am a " + breed + ". " + barkSound + " I'm "
+ dogHeight + ", Colour is " + dogColour + ", I have " + noOfLEgs +" legs" ;
return dogSpeech;
}

private bool IsBig(int dogHeight)
{
if (dogHeight < 50)
{
return false;
} else
{
return true;
}

}


Should i modify the "IsBig" Method or others better way to print the result as expect?
Wish that i could stick with boolean.
Thank You

Answer

There are two ways you can do this.

You can either rename IsBig to GetSizeDescription and return a string:

private string GetSizeDescription() {
    if (dogHeight < 50)
    {
        return "Small";
    } else
    {
        return "Big";
    }
}

Usage:

public string GetSpeech()
{
    dogSpeech = "Hello. I am a " + breed + ". " + barkSound + " I'm " 
                + GetSizeDescription() + ", Colour is " + dogColour + ", I have " + noOfLEgs +" legs" ;
    return dogSpeech;
}

or, you can keep the IsBig method unchanged, and change the GetSpeech method to this:

public string GetSpeech()
{
    dogSpeech = "Hello. I am a " + breed + ". " + barkSound + " I'm " 
                + (IsBig() ? "Big" : "Small") + ", Colour is " + dogColour + ", I have " + noOfLEgs +" legs" ;
    return dogSpeech;
}