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

Alexander V
Alexander V
87 Points

This is worded so strange...

I dont even know what they want me to do

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

        public readonly int ReactionTime;

        public int reactionTime(){
            ReactionTime = ReactionTime;
        }

        public Frog(int tongueLength)
        {
            TongueLength = tongueLength;
        }

        public bool EatFly(int distanceToFly)
        {
            return TongueLength >= distanceToFly;
        }
    }
}

1 Answer

Michael Davis
PLUS
Michael Davis
Courses Plus Student 12,508 Points

Add another public readonly integer field to the Frog class named ReactionTime.

namespace Treehouse.CodeChallenges
{
    class Frog
    {
        public readonly int TongueLength;
        public readonly int ReactionTime;  // Added a new public, readonly integer named 'ReactionTime'

        public Frog(int tongueLength)
        {
            TongueLength = tongueLength;
        }

        public bool EatFly(int distanceToFly)
        {
            return TongueLength >= distanceToFly;
        }
    }
}

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

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

        // Added a 2nd parameter to the constructor named 'reactionTime'
        // Initialized 'ReactionTime' to equal the parameter 'reactionTime'
        public Frog(int tongueLength, int reactionTime)
        {
            TongueLength = tongueLength;
            ReactionTime = reactionTime;
        }

        public bool EatFly(int distanceToFly)
        {
            return TongueLength >= distanceToFly;
        }
    }
}

Add another method named EatFly that takes two integer parameters and returns a boolean value. Name the parameters distanceToFly and flyReactionTime. Return true if the frog’s tongue is longer or equal to than the distance to the fly and its reaction time is faster or equal to than the fly’s reaction time.

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

        public Frog(int tongueLength, int reactionTime)
        {
            TongueLength = tongueLength;
            ReactionTime = reactionTime;
        }

        public bool EatFly(int distanceToFly)
        {
            return TongueLength >= distanceToFly;
        }

        // Overloaded Method taking two parameters, `distanceToFly` and `flyReactionTime`
        public bool EatFly(int distanceToFly, int flyReactionTime)
        {
            // If TongueLength is more than, or equal to, distance to the Fly, AND
            // If the frog's ReactionTime is less than, or equal to, the flyReactionTime
            // return true.
            return TongueLength >= distanceToFly && ReactionTime <= flyReactionTime;
        }
    }
}

Thank you for breaking this down. I, too, was greatly confused by the wording of the original challenge. The original wording implies there should be a constructor of name "reactionTime" instead of directing you to add a 2nd parameter named "reactionTime" to the constructor ("Frog") with existing parameter "tongueLength." Understand these are supposed to be challenging, but this seems unnecessarily obtuse.