Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C# C# Objects Methods Method Overloading

Initialize and adding parameter c#

The question I have to answer is the following

---Add a second parameter to the constructor named reactionTime after the existing parameter and use it to initialize the value of the ReactionTime field.---

I already asked for help on this question but the answer I was given didn't work and didn't make sense to me. They said I had to create another Boolean method for 'EatFly' but I wasn't sure why. Any help would be great.

Frog.cs
namespace Treehouse.CodeChallenges
{
    class Frog
    {
        public readonly int TongueLength;
        public readonly int ReactionTime;
        public Frog(int tongueLength)

        {
            TongueLength = tongueLength;
        }

        public bool EatFly(int distanceToFly)

        {
            return TongueLength >= distanceToFly;
        }

        public bool EatFly(int distanceToFly, int flyReactionTime)
        {
           return TongueLength >= distanceToFly && ReactionTime <= flyReactionTime;
        }

    }

1 Answer

Sam Baines
Sam Baines
4,315 Points

Hi Paul - you need to do some revision on what a constructor class is and then this will make simple sense - the code below is correct for the challenge (I just completed the second part):

namespace Treehouse.CodeChallenges
{
    class Frog
    {
        public readonly int TongueLength;
        public readonly int ReactionTime;


        //Below is the constructor
        public Frog(int tongueLength, int reactionTime) //The second parameter is placed inside the brackets
        {
            TongueLength = tongueLength;
            ReactionTime = reactionTime; //This is how to initilize the parameter to ReactionTime field
        }

        public bool EatFly(int distanceToFly)
        {
            return TongueLength >= distanceToFly;
        }
    }
}
Sam Baines
Sam Baines
4,315 Points

Part of the problem is that the wording of the challenge is a bit confusing - it makes it look like the constructor is called reactionTime when infact it is not and that the parameter that needs to be added to the constructor is called reactionTime instead.